DEV Community

Srinivasulu Paranduru for AWS Community Builders

Posted on • Edited on

Terraform : Count,count.index,conditional expressions

1.Overview of Count:
The count parameter on resources can simplify configurations and let you scale resources by simply incrementing a number.

Let’s assume, you need to create two EC2 instances. One of the common approaches is to define two separate resource blocks for aws_instance.



resource "aws_instance" "myfirstec2" {
    # Changed AMI id based on the region
    ami = "ami-0721c9af7b9b75114" 
    instance_type = "t2.micro"
}


resource "aws_instance" "mysecondec2" {
    # Changed AMI id based on the region 
    ami = "ami-0721c9af7b9b75114" 
    instance_type = "t2.micro"
}




Enter fullscreen mode Exit fullscreen mode

With the count parameter, we can simply specify the count value and the resource can be scaled accordingly.




resource "aws_instance" "myfirstec2" {
    # Changed AMI id based on the region
    ami           = "ami-0721c9af7b9b75114" 
    instance_type = "t2.micro"
    count         = 2
}


Enter fullscreen mode Exit fullscreen mode

2.Count Index : In resource blocks where the count is set, an additional count object is available in expressions, so you can modify the configuration of each instance.

This object has one attribute:

count.index — The distinct index number (starting with 0) corresponding to this instance.
Some of the challenges with Count Parameter:

With the below code, terraform will create 2 IAM users. But the problem is that all will have the same name.



resource "aws_iam_user" "iam_user"{
    name = "financeuser"
    count = 2
}


Enter fullscreen mode Exit fullscreen mode

count.index allows us to fetch the index of each iteration in the loop.



resource "aws_iam_user" "iam_user" {
    name = "financeuser.${count.index}"
    count = 2
}


Enter fullscreen mode Exit fullscreen mode

3.Conditional Expressions:
A conditional expression uses the value of a bool expression to select one of two values.

Syntax of Conditional expression:

condition ? true_val : false_val

If the condition is true then the result is true_val. If the condition is false then the result is false_val.

Let’s assume that there are two resource blocks as part of terraform configuration.

Depending on the variable value, one of the resource blocks will run.
If the variable is_test is true then dev environment
is_test is false then prod environment

Image description



#Conditional.tf

provider "aws" {
  region     = "us-west-2"
  access_key = "YOUR-ACCESS-KEY"
  secret_key = "YOUR-SECRET-KEY"
}

variable "istest" {}

resource "aws_instance" "dev" {
   ami = "ami-082b5a644766e0e6f"
   instance_type = "t2.micro"
   count = var.istest == true ? 3 : 0
}

resource "aws_instance" "prod" {
   ami = "ami-082b5a644766e0e6f"
   instance_type = "t2.large"
   count = var.istest == false ? 1 : 0
}
    Terraform.tfvars

istest =true


Enter fullscreen mode Exit fullscreen mode


#terraform.tfvars
istest =true



Enter fullscreen mode Exit fullscreen mode

Conclusion:
Discussed about data types and variables, variable files in terraform.

If you like my blogs, please like and any suggestion give comments and follow me in dev.to and linked in https://www.linkedin.com/in/srinivasuluparanduru/ for more updates on terraform certification

Top comments (0)