Depends_on for module
Problem
Possible workarounds For module to module dependencies, this workaround by @phinze may help. Original problem This issue was promoted by this question on Google Groups. Terraform version: `Terraform v0.3.7` I have two terraform modules for creating a digital ocean VM and DNS records that are kept purposely modular so they can be reused by others in my organisation. I want to add a series of provisioners using local_exec after a VM has been created and DNS records made. Attempted solution I tried adding a provisioner directly to my terraform file (i.e. not in a resource) which gave an error. I then tried using the `null_resource` which worked but was executed at the wrong time as it didn't know to wait for the other modules to execute first. I then tried adding a `depends_on` attribute to the null resource using a reference to a module but this doesn't seem to be supported using this syntax: [code block] Expected result Either a way for a resource to depend on a module as a dependency or a way to "inject" (for lack of a better word) some provisioners for a resource into a module without having to make a custom version of that module (I realise that might be a separate issue but it would solve my original problem). Terraform config used [code block]
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Implement Module Dependency Management in Terraform
Terraform modules do not inherently support direct dependencies between modules. The `depends_on` attribute cannot reference modules directly, leading to execution order issues when using provisioners like `local_exec` that need to run after resources from other modules are created.
Awaiting Verification
Be the first to verify this fix
- 1
Create a Null Resource with Explicit Dependencies
Define a `null_resource` that will serve as a placeholder to run your provisioners. Use the `depends_on` attribute to reference the specific resources created by the modules instead of the modules themselves.
hclresource "null_resource" "provisioner" { depends_on = [module.vm, module.dns] provisioner "local-exec" { command = "echo Provisioning after VM and DNS are created" } } - 2
Modify Module Outputs
Ensure that your VM and DNS modules output the necessary resource IDs or attributes that the `null_resource` can depend on. This will allow you to create a direct dependency on the resources created by the modules.
hcloutput "vm_id" { value = aws_instance.my_vm.id } output "dns_record_id" { value = aws_route53_record.my_dns.id } - 3
Reference Module Outputs in Null Resource
Update the `depends_on` attribute in your `null_resource` to reference the outputs from the VM and DNS modules, ensuring that the provisioners execute only after the required resources are created.
hclresource "null_resource" "provisioner" { depends_on = [module.vm.vm_id, module.dns.dns_record_id] provisioner "local-exec" { command = "echo Provisioning after VM and DNS are created" } } - 4
Test the Configuration
Run `terraform apply` to ensure that the `null_resource` executes after the VM and DNS resources are created. Check the output logs to confirm that the provisioners are executed in the correct order.
bashterraform apply
Validation
Confirm that the `null_resource` runs its provisioners only after the VM and DNS records are successfully created. Check the output logs for the expected provisioning messages.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep