DevOps Day 69- Meta-Arguments in Terraform

DevOps Day 69- Meta-Arguments in Terraform

Day 69:#90DaysOfDevOpsChallenge

Table of contents

Dear Learners, In today's topic we will explain the Meta Arguments in Terraform.

Table of Contents

A) Count

B) for_each

C) Task-01 Meta-Argument-count

D Meta arguments-for_each

When you define a resource block in Terraform by default this specifies one resource that will be created. To manage several of the same resources,you can use either count or for_each which removes the need to write a separate block of code for each one. Using these options reduces overhead and makes your code neater.

Count is what is known as a meta-argument defined by the Terraform language. Meta arguments help achieve certain requirements within the resource block

Count

The Count meta-arguments accepts a whole number and creates the number of instances of the resource specified.

for Ex- Follow the Syntax

terraform {
        required_providers {
        aws = {
                source = "hashicorp/aws"
                version = "~> 4.16"
        }
}

required_version = ">= 1.2.0"
}

provider "aws" {
        region = "ap-south-1"
}

resource "aws_instance" "server" {
        count = 4
        ami = "ami-02eb7a4783e7e9317"
        instance_type = "t2.micro"

        tags = {
                Name = "Server ${count.index}"
}
}

for_each

Like the count argument the for_each meta-arguments creates multiple instances of a module or resource block. However, instead of specifying the number of resources the for_each meta-argument accepts a map or a set of strings. This is useful when multiple resources are required that have different values. Consider our Active Directory groups example with each group requiring a different owner.

terraform {

required_providers {

aws = {

source = "hashicorp/aws"

version = "~> 4.16"

}

}

required_version = ">= 1.2.0"

}



provider "aws" {

region = "us-east-1"

}



locals {

ami_ids = toset([

"ami-0b0dcb5067f052a63",

"ami-08c40ec9ead489470",

])

}



resource "aws_instance" "server" {

for_each = local.ami_ids



ami = each.key

instance_type = "t2.micro"

tags = {

Name = "Server ${each.key}"

}

}



Multiple key value iteration

locals {

ami_ids = {

"linux" :"ami-0b0dcb5067f052a63",

"ubuntu": "ami-08c40ec9ead489470",

}

}



resource "aws_instance" "server" {

for_each = local.ami_ids



ami = each.value

instance_type = "t2.micro"



tags = {

Name = "Server ${each.key}"

}

}

Task-01

Create the above infrastructure as code and demonstrate the use of count and for_each

Meta argument- count

No alt text provided for this image

Create the above infrastructure as code and demonstrate the use of count and for_each Diagram

The first section of the code is the "terraform" block. Here we specify the required version of Terraform as well as the required providers. In this case we are specifying that we require the AWS provider and that it should be sources from the "hashicorp/aws" module with a version greater than or equal to 4.16.

Next we define the "provider" block, which specifies the configuration for the AWS provider. In this case, we are specifying the region as ap-south-1.

The "aws_instance" resource block, which creates the EC2 instances. The "count" meta-argument is used to create 4 instances each with the specified AMI and instance type. The "tags" block is used to assign a name to each instance, with a unique index based on the Count.

Run the Terraform init to initialise the Terraform Project.

No alt text provided for this image

Run the Terraform init to initialise the Terraform Project Diagram.

Run terraform apply to create a 4 instances.

No alt text provided for this image

Run terraform apply to create a 4 instances Diagram

No alt text provided for this image

Run terraform apply to create a 4 instances Diagram 2

No alt text provided for this image

Run terraform apply to create a 4 instances Diagram 3

No alt text provided for this image

Run terraform apply to create a 4 instances Diagram 4

No alt text provided for this image

Run terraform apply to create a 4 instances Diagram 5

No alt text provided for this image

Run terraform apply to create a 4 instances Diagram 5

No alt text provided for this image

Run terraform apply to create a 4 instances Diagram 6

Four new EC2 instances successfully created.

No alt text provided for this image

Four new EC2 instances successfully created Diagram.

Meta arguments-for_each

No alt text provided for this image

Meta arguments-for_each main.tf file Diagram.

we define a "locals" block to create a set of AMI IDs for our instances. we use the "toset" function to convert an array of strings into a set of strings.

In the "resource" block we define the "aws_instance" resource and use the "for_each" meta-arguments to create an instance for each AMI ID in our "ami_ids" map. The "ami" attribute is set to the current key in the map (i.e the AMI ID) and the "instance_type attribute is set to "t2.micro". we also use the "tags"attribute to set a unique name for each instance using the current key in the map.

Run terraform apply

No alt text provided for this image

Run terraform apply Diagram.

No alt text provided for this image

Run terraform apply Diagram.

No alt text provided for this image

Run terraform apply Diagram.

Two new instances successfully created.

No alt text provided for this image

Two new instances successfully created AWS Diagram.

With multiple key-value iteration.

No alt text provided for this image

We use the "for_each" meta-argument with a map that has multiple key-value pairs to create instances with a different AMIs. In this case we use a map with 2key value pairs, where each key is a string that represents the name of the AMI and each value is the actual AMI ID.

Inside the "resource" block we use the "each.value" variable to access the current value in the map. and set the "ami" attribute to it. we also use the "each.key" variable to access the current key in the map and use it to set a unique name for each instance using the "tags" attribute.

using the "for_each" meta-argument with maps can be very useful for creating multiple instances with different configurations in a single block of code.

Run terraform apply

No alt text provided for this image

Run terraform apply Diagram

No alt text provided for this image

Run terraform apply Diagram 1

No alt text provided for this image

Run terraform apply Diagram 2

No alt text provided for this image

Run terraform apply Diagram 3

2 New instances created using "for_each" meta argument.

No alt text provided for this image

2 New instances created using "for_each" meta argument Diagram.

Write about meta-argument and its use in Terraform

Meta-arguments are special arguments in Terraform that are used to control how resource are created,updated or destroyed. They are not specific to any particular resource type,but rather provide a way to configure behaviour across all resources in a Terraform configurations.

The main use of meta-arguments in Terraform is to control the decencies between resource. For example the depends on meta-arguments can be used to specify that a resource depends on another resource. This can be useful when creating resources that require other resource to exist first,such as a load balancer that depends on an auto scaling group.

Another common use to meta-arguments is to control in which resources are created. The "Count" and "for_each" meta-arguments can be used to control when resources are created, updated or destroyed.

The "provider" meta-argument is also a very important meta-argument in Terraform. it is used to specify the provider that should be used to manage a particular resource. Providers are plugins that Terraform uses to manage resources, such as AWS,Google Cloud or Azure.

Overall, meta-arguments are a powerful feature of Terraform that can be used to control the behaviour of resources in a flexible and granular way. By using meta-arguments effectively, Terraform users can create more robust and scalable infrastructure as code.

Thank you for reading!! Hope you find this helpful.

#day69challenge#90daysofdevops

Always open to suggestions..!!

~ Manoj Bhamidipati 🙂