Contents

Terraform Depends On

Terraform depends on parameter explanation with examples

Website Visitors:

Terraform is an open source tool for managing infrastructure as code. It allows you to change resources without having to manage them by hand. But what if you want to wait for a resource deployment until some other resource is deployed? This is possible by using depends on parameter in the Terraform script. In this tutorial, we will explain you what terraform depends on is all about.

Terraform depends on…

In the context of Terraform, which is an infrastructure as code (IaC) tool used to provision and manage infrastructure resources, dependencies refer to the relationships between different resources or components in your infrastructure. Terraform uses these dependencies to determine the order in which resources are created, updated, or destroyed. There are two types of dependencies in Terraform: explicit dependencies and implicit dependencies.

Explicit Dependency

  • An explicit dependency is one that you define explicitly in your Terraform configuration. You use the “depends_on” attribute to specify explicit dependencies between resources. When you declare a dependency using “depends_on,” you are telling Terraform that one resource depends on the successful creation or update of another resource before it can be created or updated itself.

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
resource "aws_instance" "web_server" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

resource "aws_security_group" "web_sg" {
  # ...

  depends_on = [aws_instance.web_server]
}

In this example, the “aws_security_group” resource depends on the “aws_instance” resource, so Terraform will ensure that the instance is created or updated before creating or updating the security group.

Implicit Dependency

  • An implicit dependency is one that Terraform infers based on resource references in your configuration. Terraform analyzes the configuration to determine the order in which resources should be created or updated, even if you don’t explicitly specify dependencies using “depends_on.” Implicit dependencies are based on how resources are connected in your configuration, and Terraform automatically figures out the correct order.

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "subnet_a" {
vpc_id                  = aws_vpc.my_vpc.id
cidr_block              = "10.0.1.0/24"
availability_zone       = "us-east-1a"
}

resource "aws_subnet" "subnet_b" {
vpc_id                  = aws_vpc.my_vpc.id
cidr_block              = "10.0.2.0/24"
availability_zone       = "us-east-1b"
}

resource "aws_security_group" "web_sg" {
name        = "web-sg"
description = "Security group for web servers"

# You don't specify a "depends_on" here, but Terraform infers dependencies
}

resource "aws_instance" "web_server_a" {
ami           = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
subnet_id     = aws_subnet.subnet_a.id

# Terraform infers the dependency on aws_subnet.subnet_a
}

resource "aws_instance" "web_server_b" {
ami           = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
subnet_id     = aws_subnet.subnet_b.id

# Terraform infers the dependency on aws_subnet.subnet_b
}

In this example, we have a VPC, two subnets, security group, and two EC2 instances. Even though we haven’t explicitly specified dependencies using “depends_on,” Terraform can infer dependencies based on the resource references in the configuration:

  1. The aws_subnet.subnet_a resource references aws_vpc.my_vpc.id, so Terraform knows that the VPC (aws_vpc.my_vpc) must be created before the subnet (aws_subnet.subnet_a) to get its ID.

  2. Similarly, the aws_subnet.subnet_b resource references aws_vpc.my_vpc.id, indicating a dependency on the VPC.

  3. Each aws_instance resource references its respective subnet (aws_subnet.subnet_a.id and aws_subnet.subnet_b.id), implying that the subnets must be created before the instances that depend on them. Terraform will automatically establish the correct order of provisioning based on these implicit dependencies, ensuring that VPC, subnets, and instances are created in the correct sequence.

Terraform uses these dependencies to create a dependency graph, ensuring that resources are provisioned or updated in the correct order, and that changes are applied safely and efficiently. While explicit dependencies can be useful for cases where the implicit ordering is not clear or needs to be enforced, Terraform generally does a good job of handling implicit dependencies based on the resource relationships you define in your configuration.

Suggested article

If you’d like to continue reading, checkout our other article Terraform Definitions here. All other DevOps categories are listed here. Have a look at the posts as per your topic of interest.

Conclusion

In this tutorial, we’ve explained what is depends on argument in Terraform and its basic usage. We’ve also demonstrated examples for all the topics that are discussed.

If you know any other ways to pause a resource deployment until other dependent resources are deployed, let us know in the comments section below.

Your inbox needs more DevOps articles.

Subscribe to get our latest content by email.