Lesson 1 of 2 Resource Dependencies and Lifecycle Rules

Implicit and Explicit Dependencies

Terraform must create, update and destroy resources in the right order. This lesson explains how it infers order from references and when to declare a dependency explicitly with depends_on.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain how references create implicit dependencies.
  • Identify situations that need depends_on and its drawbacks.

Implicit dependencies

Whenever an argument of one block refers to another block, Terraform records an implicit dependency. In the following configuration, the instance refers to the subnet and the subnet to the VPC, so Terraform creates the VPC first, then the subnet, then the instance, and destroys them in the reverse order.

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "app" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.1.0/24"
}

resource "aws_instance" "app" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t3.micro"
  subnet_id     = aws_subnet.app.id
}

Implicit dependencies are the preferred way to express order, because the reference itself shows why the order matters, and Terraform keeps the graph correct as the configuration changes. Resources with no path between them in the graph, such as two unrelated buckets, are created in parallel.

Put in order

Destroy order

Exam skills 4.6.1

Arrange the order in which terraform destroy removes resources linked by references.

Log in to try this activity Interactive activities are free with an account.

Hidden dependencies and depends_on

Sometimes a resource relies on another without using any of its values. The classic example is permissions: an instance uses an instance profile, and the application on it calls Amazon S3 at start-up, so the IAM policy that grants S3 access must exist first. The instance references the profile, but nothing references the policy attachment, so Terraform could create them in any order. depends_on declares the dependency explicitly.

resource "aws_iam_role_policy" "s3_read" {
  role   = aws_iam_role.app.id
  policy = data.aws_iam_policy_document.s3_read.json
}

resource "aws_instance" "app" {
  ami                  = data.aws_ami.ubuntu.id
  instance_type        = "t3.micro"
  iam_instance_profile = aws_iam_instance_profile.app.name

  depends_on = [aws_iam_role_policy.s3_read]
}

The value is a list of references to whole objects, written without quotes, such as resources, data sources or modules; attributes are not allowed. depends_on can be set on resources, data sources, module blocks and outputs. On a module block, it makes every object in the module wait for the dependency.

The following diagram shows a hidden dependency and the depends_on that adds it.

Why the Lambda function is invoked before its permissions exist. The function and the policy attachment both reference the role, but nothing connects the function to the attachment, so Terraform creates them in parallel. depends_on adds the missing edge, so the attachment finishes first; a reference would be preferable, but the function has no argument that takes the attachment's values.
The resource names are illustrative.
Spot the bug

A dependency that is never declared

Exam skills 4.6.1 · 4.6.2

Find the problems in how this configuration expresses its dependencies.

Log in to try this activity Interactive activities are free with an account.

The cost of explicit dependencies

HashiCorp recommends depends_on only as a last resort. Explicit dependencies make Terraform more conservative: a data source that depends on a resource with pending changes is read during apply instead of plan, so its values and everything derived from them become (known after apply), and the plan becomes less informative. A module-level depends_on applies to every resource in the module, which can serialise work unnecessarily.

Implicit dependency
Created byA reference in an argument
Shows whyYes, through the value used
Effect on plansMinimal
Choose when Always, where a value is used
depends_on
Created byAn explicit list of objects
Shows whyOnly through a comment
Effect on plansCan defer data source reads and add unknown values
Choose when Only for dependencies no reference can express
Worked example

A race at start-up

A Lambda function's first invocations fail with access denied errors after every fresh deployment, then work a minute later. The function references its role, but the policy is attached with a separate aws_iam_role_policy_attachment resource that nothing references.

The decisive fact is that Terraform has no edge between the function and the attachment.

Terraform created the function and the attachment in parallel, so the function could be invoked before its permissions existed. Adding depends_on = [aws_iam_role_policy_attachment.lambda_logs] to the function makes Terraform finish the attachment first. A reference would be preferable, but the function has no argument that takes the attachment's values, so depends_on is the right tool.

Check yourself

What form does the value of depends_on take?

Show answer

A list of references to whole resources, data sources or modules, without quotes, such as [aws_iam_role_policy.app].

Exam Tips

Remember for the exam

  • References create implicit dependencies; prefer them.
  • depends_on takes a list of whole objects, without quotes.
  • depends_on works on resources, data sources, modules and outputs.
  • Explicit dependencies can make plans less precise.