DEV Community

Akshay Rao
Akshay Rao

Posted on

How does Terraform handle resource dependencies and provisioning order?

Hi, this Akshay Rao. Lets us look how does Terraform handle resource dependencies and provisioning order.

Terraform uses an implicit and explicit dependency paradigm to manage resource dependencies and provisioning order. This model enables Terraform to comprehend the connections between resources and guarantees that they are provisioned in the proper sequence to meet these requirements.

  • Implicit Dependency Model: Based on the configuration code, Terraform automatically ascertains the interdependence between resources. When one resource refers to the properties of another, Terraform creates an implicit dependency between them. For instance, Terraform will automatically recognize the dependency between an EC2 instance and a security group if you create an AWS EC2 instance and assign it to a certain security group. As a result, Terraform will make sure the security group is established first.
  • Explicit Dependency Model: There are occasions when you must provide explicit dependencies that Terraform cannot deduce from the configuration on its own. The depends_on parameter can be used in this situation. Even if there are no direct references between two resources in the configuration, the depends_on parameter specifies that they are interdependent. When resources lack direct attribute references to one another yet are logically dependent on one another.

An illustration of how resource dependencies operate in a Terraform configuration is given below:

resource "aws_security_group" "sg" {
  name_prefix = "sg_"
}

resource "aws_instance" "instance" {
  ami           = "ami-400Odg3r354efd"
  instance_type = "t2.micro"
  security_groups = [
    aws_security_group.web_sg.id,
  ]

}

resource "aws_s3_bucket" "tf_bucket" {
  bucket = "my-tf-bucket"
  acl    = "private"

}
Enter fullscreen mode Exit fullscreen mode

In this example, the aws_instance resource is dependent on the aws_security_group resource because the security group ID is referenced in the security_groups parameter. Terraform understands that the security group must be created before the EC2 instance.

However, because the aws_s3_bucket resource contains no references to other resources, it has no implicit dependencies. If you need to construct the S3 bucket after the EC2 instance and security group are created, you can use the depends_on argument:

resource "aws_s3_bucket" "tf_bucket" {
  bucket = "my-tf-bucket"
  acl    = "private"

  depends_on = [
    aws_instance.instance,
    aws_security_group.sg,
  ]
}
Enter fullscreen mode Exit fullscreen mode

Using the depends_on parameter, you explicitly specify that the aws_s3_bucket resource is dependent on both the aws_instance.instance and the aws_security_group.sg resources, ensuring proper provisioning sequence.

Keep in mind that, while depends_on aids in ordering, it does not impose rigid resource sequencing. Terraform prioritizes creating resources concurrently and resolving dependencies over waiting for each resource to be built sequentially.

Top comments (0)