Dependencies in Terraform Graph

Terraform Dependencies, Graph Theory and Lists

1780 VIEWS

· ·

This blog provides some basic examples of dealing with dependencies in Terraform and the graph theory behind it and also some examples of utilising the new variable types made available in Terraform 0.7.


This latest release to the repository includes some examples of forcing ordering of resource creation using modular dependencies, the graph theory that demonstrates how Terraform is viewing those dependencies and also an example of lists usage, one of the new first class data types released as part of Terraform 0.7.

To force a modular dependency using variables you should first add a dummy variable to the module you wish to be dependent on another, like below for example with the ‘dependency’ variable:

With dummy ‘dependency’ variable in place it means that a variable can be passed to the module, even if it won’t be used as part of a resource. Using this dummy variable during the modular declaration we can use an output variable from another module (see below), this implicitly forces the dependent module to wait for the other to be created first as it has to wait for the output to become available.

You need to ensure that you have assigned an output within the module you wish for another module to be dependent on!

With these variable dependencies in place it forms a work flow as follows:

From this image you can see that because of the variable dependencies out original ‘demo-node’ module (which includes the ‘demo-sg’ that is part of the module, hence why the instance is dependent on the security group because the security group is created before it, ready for the instance to be attached to) is now dependent on the ‘demo-node-dependency’ module. All of which of dependent on the AWS provider as they are all resources of the AWS provider.

In order to pass a list of ports into a firewall resource for Google Cloud Platform I opted in to using one of the new first class data types, the list (the map data type is also now available), which proved to be much less complex than trying to use the split/join syntax that had to be used previously.

Simply declare your array list in your ‘terraform.tfvars

Pass it through to the module as a list

Ensure that the variable is declared as a list within the module

And use the variable as part of the resource that expects an series of values as the parameter

That’s it for now guys, I hope all of this has been helpful in your Terraform endeavours, feel free to drop me an email/comment on this post should you have any questions.

Do you think you can beat this Sweet post?

If so, you may have what it takes to become a Sweetcode contributor... Learn More.

Jordan Taylor is a DevOps Practitioner. His goal is to learn every DevOps tool and technology, developing an arsenal of knowledge that covers every aspect of the DevOps space. With a specialization in automation, configuration management, cloud orchestration and CI/CD, Jordan is always looking to implement forward-thinking ideas that result in ultimate efficiency and value, while up-skilling and enabling those around him in the technologies used to innovate. Jordan's current favorite tools are Terraform, Docker and Vault.


Discussion

Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu