top of page

Terraform Conditionals and Loops: Some Terraform hacks that you should know

Updated: Jul 15


""

Did you ever find yourself coding Terraform and suddenly have to create multiple resources dynamically or based on a condition without knowing how? This TeraTip brings you the answers to some of those questions. Let’s get started!



Conditionals

Our first mission is to learn how to create a Terraform resource conditionally. Let’s say you want to create an Autoscaling Group conditionally based on a variable (for example, indicating if the ASG is enabled or if the environment is production, etc). For this we should use the ternary operator available on Terraform. The ternary operator looks like this:


  • condition ? this if the condition is true : this if the condition is false


With this expression we’re able to set attributes conditionally or even create resources conditionally. How would you use this for creating a resource conditionally? Take a look at the following:

""

As you can see, we have the special count attribute on top of the resource, and its value is defined based on the var.asg_is_enabled variable (which is a bool by the way). So, if the asg_is_enabled variable is true, we’re going to create the AutoScaling Group, but, if it’s false we aren’t going to create the ASG. Take a look at line 6. I left an example on how you can use conditionals for setting the attributes of the objects dynamically.



Terraform’s for_each meta-argument

When working with big projects we have to create a lot of resources using Terraform and a lot of them are created multiple times but with a different configuration for each one. One way of doing this is by creating the same resources multiple times but with different names.


However, this reduces the maintainability of the code, making it more difficult to read, and more easy to make a mistake (for example you have to change all the EC2 Instances resources but you miss one).

For this, Terraform gives us the for_each attribute. The for_each attribute creates an X amount of resources based on an argument, and it's commonly used for creating multiple resources with different configurations. Check out the example below.


""

In the example above we have 3 instances being created, each one with its own configuration. Right now it looks very small, but imagine if you have to configure A LOT more parameters for the instances or even if you have to create 10 instances instead of 3.


The code gets big very quickly, and this is something that negatively affects the readability and maintainability of the code. On the other hand, when using for_each we’ll ALWAYS have the same amount of lines,and instead we’ll add more instances just adding more objects to our var.instances variable.

What we’re doing is defining a var.instances variable as an object list. This object list contains one object per instance that we want to create, and each object contains the particular configuration for the instance.


This is what your instances variable will look like. In the future if you want to add a new instance, just add a new object to the list along with its attributes and Terraform will handle the creation of this new instance ;)

""

Hope you liked this TeraTip! Stay tuned for more Terraform Tricks that will make your life easier.

""



Juan Wiggenhauser

Cloud Engineer

Teracloud





If you want to know more about Terraform, we suggest going check How to Restore a Previous Version of Terraform State

If you'd like to learn more about our #TeraTips or our blog's content, check out more of our blog entries created to help you navigate your journey in the Cloud.







Buscar por tags
bottom of page