Search code examples
terraformconsul

Execution order inside Terraform modules


I had an issue with the execution order of modules in terraform scripts. I have raised an issue with the source repo. https://github.com/hashicorp/terraform/issues/18143

Can anyone please help me with this issue here or on GitHub?


Solution

  • The execution does not wait for the completion of the "vpc" module, but only for the availability of the value "module.vpc.vpc_id". For this it is enough to execute the aws_vpc resource. Therefore, you are not actually telling TerraForm to also wait for the consul_keys resource.

    To fix this you have to add a dependency from the consul_keys resource to your other modules. This works either by :

    1. Using a value exported by consul_keys in your other modules (either datacenter or var.name>)
    2. Dumping the resources that depend on consul_keys in the same file.

    Sadly there is no nice solution for this at the moment, but module dependencies are being worked on.

    EDIT: As an example of dumping all resources in the same file:

    This does not work because there are no module dependencies:

    module "vpc" {
        ...
    }
    
    module "other" {
     depends_on=["module.vpc"]
    }
    

    vpc module file:

    resource "aws_instance" "vpc_res1" {
        ...
    }
    
    resource "consul_keys" "my_keys" {
        ...
    }
    

    other module file:

    resource "aws_instance" "other_res1" {
        ...
    }
    
    resource "aws_instance" "other_res2" {
        ...
    }
    

    Putting everything in the same file works. You can also keep the "vpc_res1" resource in a separate module:

    resource "consul_keys" "my_keys" {
        ...
    }
    
    resource "aws_instance" "other_res1" {
        depends_on = ["consul_keys.my_keys"]
    }
    
    resource "aws_instance" "other_res2" {
        depends_on = ["consul_keys.my_keys"]
    }