Count and Conditionals
Basic usage of Terraform count and conditionals
Website Visitors:Terraform is a tool which allows you to create infrastructure resources like networks, subnets, security groups, etc. In this tutorial, we’ll show you how to start using Terraform’s count and conditionals.
Count
By default using a resource block you can deploy only one infrastructure object. If you want to deploy multiple instances with any name, how will you do it with terraform? Answer is using the count argument.
Count argument allows you to deploy number of resources by specifying the count
value in the resource block. Below example will create 5 instances in your AWS environment.
|
|
You can also specify a name argument like shown below. Count argument starts from 0.
|
|
This will result in creating instances with names
|
|
If you specify a variable with list items for name, and specify a count value, all your instances will be deployed with the names as shown below:
|
|
Now when you run the above code
|
|
This will deploy instances as:
|
|
When to use for_each
instead of count
count
is appropriate. If some of their arguments need distinct values that can’t be directly derived from an integer, it’s safer to use for_each
.Before for_each
was available, it was common to derive count
from the length of a list and use count.index
to look up the original list value.
Conditional Expressions
Conditional expressions are typical if else statements in other programming languages. You will test a condition first, if it is true, a series of steps are performed. If false, other actions are performed. In terraform conditional expressions are declared as:
condition ? true : false
Example on using conditional expressions. Terraform should only deploy instance if you set the deploy variable to true. So, create a variable called deploy and set the default value to false.
|
|
In your terraform file, add a condition to check if the variable deploy is set to true. Only then it will create instances in your environment. In below example, if deploy value is set to true, it will deploy one instance. If it is false, it will deploy 0 instances which is none.
|
|
In order to deploy instances, you have to set the deploy variable value to true. Only then instances will be deployed.
Suggested Article
Now that you know how to use count and conditionals in Terraform, continue reading on Terraform ForEach Loop here. All other DevOps categories are listed here: DevOps Tools. Have a look at the posts as per your topic of interest.
Conclusion
In this tutorial, we’ve explained how to use count and conditionals in Terraform and its basic usage. We’ve also demonstrated examples for all the topics that are discussed. We hope you have learned something new in this article.
After reading this post, will you deploy multiple instances using count or one by one, in your environment? Let us know in the comments section below.
Your inbox needs more DevOps articles.
Subscribe to get our latest content by email.