Depends_on

In this lesson, we'll discuss the depends_on attribute in detail.

We'll cover the following

Depends_on#

We have seen thus far that, where possible, you should tell Terraform about dependencies by referring to one resource in the one that depends on it. The depends_on attribute offers a way to specify a dependency by hand. This can be useful if there is a hidden dependency that Terraform would otherwise not know about.

Example#

For example, if you are creating an S3 bucket and an IAM policy that allows writing to the S3 bucket, then you should get the ARN of the S3 bucket from the property aws_s3_bucket.arn of the resource. In that case, Terraform will internally build a graph that the S3 bucket needs to be created before the IAM role. There are some circumstances where there is a dependency that you cannot model like this, though.

In those cases, you can explicitly tell Terraform that a resource depends on another resource by using the depends_on attribute and then using the full path to the resource.

Project example#

Let’s look at the example:

Depends_on Terraform project example

In the above example, the aws_iam_role_policy.policy is specified as a dependency of the instance. This could be because the instance will use the role as part of booting up and the role needs to give the instance access to all of S3.

Lifecycle
🎉Quiz
Mark as Completed
Report an Issue