Search code examples
amazon-web-servicesterraformterraform-provider-awsiaasaws-config

Can depends_on in terraform be set to a file path?


I am trying to break down my main.tf file . So I have set aws config via terraform, created the configuration recorder and set the delivery channel to a s3 bucket created in the same main.tf file. Now for the AWS config rules, I have created a separate file viz config-rule.tf. As known , every aws_config_config_rule that we create has a depends_on clause where in we call the dependent resource, which in this case being aws_config_configuration_recorder. So my question is can I interpolate the depends_on clause to something like :

    resource "aws_config_config_rule" "s3_bucket_server_side_encryption_enabled" {
    name = "s3_bucket_server_side_encryption_enabled"

    source {
    owner             = "AWS"
    source_identifier = "S3_BUCKET_SERVER_SIDE_ENCRYPTION_ENABLED"
    }

    depends_on = ["${file("aws-config-setup.tf")}"]
    }

Considering I move my aws config setup from my main.tf file to a new file called aws-config-setup.tf.


Solution

  • If I'm reading your question correctly, you shouldn't need to make any changes for this to work, assuming you didn't move code to its own module (a separate directory).

    When terraform executes in a particular directory it takes all files into account, basically treating them all as one terraform file.

    So, in general, if you had a main.tf that looks like the following

    resource "some_resource" "resource_1" {
      # ...
    }
    
    resource "some_resource" "resource_2" {
      # ...
    
      depends_on = [some_resource.resource_1]
    }
    

    and you decided to split these out into the following files

    file1.tf

    resource "some_resource" "resource_1" {
      # ...
    }
    

    file2.tf

    resource "some_resource" "resource_2" {
      # ...
    
      depends_on = [some_resource.resource_1]
    }
    

    if terraform is run in the same directory, it will evaluate the main.tf scenario exactly the same as the multi-file scenario.