github.com/hugorut/terraform@v1.1.3/website/docs/language/meta-arguments/depends_on.mdx (about) 1 --- 2 page_title: The depends_on Meta-Argument - Configuration Language 3 description: >- 4 The depends_on meta-argument allows you to handle hidden resource or module 5 dependencies. 6 --- 7 8 # The `depends_on` Meta-Argument 9 10 -> **Version note:** Module support for `depends_on` was added in Terraform 0.13, and 11 previous versions can only use it with resources. 12 13 Use the `depends_on` meta-argument to handle hidden resource or module dependencies that 14 Terraform can't automatically infer. 15 16 Explicitly specifying a dependency is only necessary when a resource or module relies on 17 some other resource's behavior but _doesn't_ access any of that resource's data 18 in its arguments. 19 20 This argument is available in `module` blocks and in all `resource` blocks, 21 regardless of resource type. For example: 22 23 ```hcl 24 resource "aws_iam_role" "example" { 25 name = "example" 26 27 # assume_role_policy is omitted for brevity in this example. See the 28 # documentation for aws_iam_role for a complete example. 29 assume_role_policy = "..." 30 } 31 32 resource "aws_iam_instance_profile" "example" { 33 # Because this expression refers to the role, Terraform can infer 34 # automatically that the role must be created first. 35 role = aws_iam_role.example.name 36 } 37 38 resource "aws_iam_role_policy" "example" { 39 name = "example" 40 role = aws_iam_role.example.name 41 policy = jsonencode({ 42 "Statement" = [{ 43 # This policy allows software running on the EC2 instance to 44 # access the S3 API. 45 "Action" = "s3:*", 46 "Effect" = "Allow", 47 }], 48 }) 49 } 50 51 resource "aws_instance" "example" { 52 ami = "ami-a1b2c3d4" 53 instance_type = "t2.micro" 54 55 # Terraform can infer from this that the instance profile must 56 # be created before the EC2 instance. 57 iam_instance_profile = aws_iam_instance_profile.example 58 59 # However, if software running in this EC2 instance needs access 60 # to the S3 API in order to boot properly, there is also a "hidden" 61 # dependency on the aws_iam_role_policy that Terraform cannot 62 # automatically infer, so it must be declared explicitly: 63 depends_on = [ 64 aws_iam_role_policy.example, 65 ] 66 } 67 ``` 68 69 The `depends_on` meta-argument, if present, must be a list of references 70 to other resources or child modules in the same calling module. 71 Arbitrary expressions are not allowed in the `depends_on` argument value, 72 because its value must be known before Terraform knows resource relationships 73 and thus before it can safely evaluate expressions. 74 75 The `depends_on` argument should be used only as a last resort. When using it, 76 always include a comment explaining why it is being used, to help future 77 maintainers understand the purpose of the additional dependency.