Contents

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.

1
2
3
4
resource "aws_instance" "instance"{
    count = 5
    region = var.region
}

You can also specify a name argument like shown below. Count argument starts from 0.

1
2
3
4
5
resource "aws_instance" "instance"{
    count = 3
    name = "DevInstance.${count.index}"
    region = var.region
}

This will result in creating instances with names

1
2
3
DevInstance.0
DevInstance.1
DevInstance.2

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:

1
2
3
4
variable "Instance" {
    type = list
    default = ["Dev","Test", "Prod"]
}

Now when you run the above code

1
2
3
4
5
resource "aws_instance" "instance"{
    count = 3
    name = var.Instance[count.index]
    region = var.region
}

This will deploy instances as:

1
2
3
InstanceDev
InstanceTest
InstanceProd

When to use for_each instead of count

Tip
If your instances are almost identical, 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.

1
2
3
variable deploy {
    default = 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.

1
2
3
4
5
6
resource "aws_instance" "TestVM" {
  ami                    = data.aws_ami.aws-linux.id
  instance_type          = "t2.micro"
  subnet_id              = aws_subnet.HomeLabSubnet.id
  count = var.deploy ? 1:0
}

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.