Learning Terraform with Real World Scenarios

Linu Bajy
4 min readDec 3, 2022

--

Hi Everyone,

Terraform is an IaC tool that is hot in the DevOps market. You can learn about the basics of it in my previous article.

Continuing with the series , I have created a Repo in GitHub which contains some Real World Examples that can help you understand better. These examples can be run in your local system even if you do not have a Provider account, strictly for learning purpose alone.

Repo : https://github.com/linubajy/Terraform

Example 1

Terraform example file — map.tf — that selects the AMI based on region. The region region is given by the user during run time. The AMI selected is displayed to the user as the Output.

provider "aws" {
region = var.region
skip_credentials_validation = true
skip_requesting_account_id = true
skip_metadata_api_check = true
access_key = "mock"
secret_key = "mock"
}

resource "aws_instance" "myinstance" {
ami = var.ami_mapping[var.region]
instance_type = "t2.micro"
}

variable "ami_mapping" {
type = map
default = {
"us-west-1" = "ami-001"
"us-west-2" = "ami-002"
}
}

variable region {}

output "ami_name" {
value = aws_instance.myinstance.ami
}

Output for map.tf

map.tf (1)
map.tf(2)

Example 2

Terraform example file — count.tf — creates a cloudwatch alarm for 3 different Environments . Here we make use of list to store the Environment names , which uses count.index to iterate over the list.

count is a keyword in terraform that specifies the number of the resources it wants to create .It has an attribute called index, whose value begins at 0. Hence count.index is the iterator used to fetch values from the list.

provider "aws" {
region = "eu-west-1"
skip_credentials_validation = true
skip_requesting_account_id = true
skip_metadata_api_check = true
access_key = "mock"
secret_key = "mock"
}

variable "environment" {
type = list
default = ["dev","staging","prod"]

}




resource "aws_cloudwatch_metric_alarm" "cwalarm" {
alarm_name = "${var.environment[count.index]}-Environment-Alarm"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
period = "120"
statistic = "Average"
threshold = "80"
alarm_description = "This metric monitors ec2 cpu utilization"
count = 3
}

Output for count.tf

count.tf(1)
count.tf(2)
count.tf(3)

Example 3

Terraform example file — count_index.tf — creates 3 queues for each of the Environments . Here , initially I used a single resource having a count value of 3 . But here , since there are 3 different environments that we need to iterate as well as 3 queues in each of the environments, the count value at different iterations can create a conflict and will not come out with the expected naming conventions . Hence we use 3 different resources for each environment , which we can identify from its local name. And the value of the count is used in the naming of the resources and it comes out as expected.

Since the count.index begins at value 0 , we have given ‘ + 1 ‘ so that it starts from 1 and names appropriately.

Laziness is always a motivation for me to automate things. Since there were 3 different queues, and I only wanted to know the names of the queues that was going to be created. Hence , I made use of output block .

In the output block, we use a for loop that displays the value of name of the queues that it would create. The iterator ‘i’ would loop through the names of the resource (here queue) and store the name of each of the resource in q.name which would be displayed at the output.

provider "aws" {
region = "eu-west-1"
skip_credentials_validation = true
skip_requesting_account_id = true
skip_metadata_api_check = true
access_key = "mock"
secret_key = "mock"

}


variable "environment" {
type = list
default = ["staging","preprod", "prod"]
}


resource "aws_sqs_queue" "terraform_queue_staging" {
name = "${var.environment[0]}-${count.index + 1 }-queue"
message_retention_seconds = 86400
count = 3
tags = {
Environment = var.environment[0]
}
}



resource "aws_sqs_queue" "terraform_queue_preprod" {
name = "${var.environment[1]}-${count.index + 1 }-queue"
message_retention_seconds = 86400
count = 3
tags = {
Environment = var.environment[1]
}
}

resource "aws_sqs_queue" "terraform_queue_prod" {
name = "${var.environment[2]}-${count.index + 1 }-queue"
message_retention_seconds = 86400
count = 3
tags = {
Environment = var.environment[2]
}
}

output "staging_queues" {
value = {
for i, q in aws_sqs_queue.terraform_queue_staging :i+1 => q.name
}
}

output "preprod_queues" {
value = {
for i, q in aws_sqs_queue.terraform_queue_preprod :i+1 => q.name
}

}

output "prod_queues" {
value = {
for i, q in aws_sqs_queue.terraform_queue_prod :i+1 => q.name
}
}

Output for count_index.tf

count_index(1)

I hope this article gives you inspiration to try out terraform and play with it! More cool examples on the way! :)

--

--

Linu Bajy
Linu Bajy

Written by Linu Bajy

Enthusiastic Learner . DevOps Professional .

No responses yet