Day 69 - Meta-Arguments in Terraform   (Feb 3, 2024)

Day 69 - Meta-Arguments in Terraform (Feb 3, 2024)

🙏 Introduction:

In this blog, we will learn Meta-Arguments in Terraform. Meta-Arguments is a critical component that significantly enhances the flexibility and modularity of Terraform configurations.

🔶What is Meta-Arguments?

  • 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-argument accepts a whole number and creates the number of instances of the resource specified.

  • When each instance is created, it has its own distinct infrastructure object associated with it, so each can be managed separately. When the configuration is applied, each object can be created, destroyed, or updated as appropriate.

terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
        }
    }
required_version = ">= 1.2.0"
}
provider "aws" {
region = "us-east-1"
    }
resource "aws_instance" "server" {
count = 4
ami = "ami-08c40ec9ead489470"
instance_type = "t2.micro"
tags = {
Name = "Server ${count.index}"
    }
}

🔶for_each

  • Like the count argument, the for_each meta-argument 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-0c7217cdde317cfec",
"ami-0005e0cfe09cc9050",
    ])
}
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-0c7217cdde317cfec",
"ubuntu": "ami-0005e0cfe09cc9050",
    }
}
resource "aws_instance" "server" {
for_each = local.ami_ids
ami = each.value
instance_type = "t2.micro"
tags = {
Name = "Server ${each.key}"
    }
}

🎯Task: 1

Create the above Infrastructure as code and demonstrate the use of Count and for_each

Meta argument: count

Terraform block 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 sourced from the "hashicorp/aws" module with a version greater than or equal to 4.16

Provider block we specifies the configuration for the AWS provider. In this case, we are specifying the region as us-east-1

Resource block which creates the EC2 instances, count meta-argument is used to create four 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.

terraform init

terraform plan

terraform apply

New EC2 instances successfully created

Meta argument: for_each

terraform init

terraform plan

terraform apply

New instances created

With multiple key-value iteration

terraform init

terraform plan

terraform apply

New instances created