Day 69 - Meta-Arguments in Terraform ๐ŸŒŸ

Day 69 - Meta-Arguments in Terraform ๐ŸŒŸ

ยท

4 min read

Welcome to Day 69 of the #90DaysOfDevOpsChallenge! ๐ŸŽ‰ Today, we dive into Meta-Arguments in Terraform. Meta-arguments like count and for_each are powerful tools that can help you manage multiple resources efficiently. Let's break down these concepts and walk through a hands-on example on AWS. ๐Ÿš€

Understanding Meta-Arguments in Terraform ๐Ÿ› ๏ธ

When you define a resource block in Terraform, it specifies one resource by default. To manage several of the same resources without writing multiple blocks, you can use meta-arguments like count and for_each. These meta-arguments streamline your code and reduce overhead. ๐Ÿ“‰

Install Terraform:

sudo apt-get update
sudo apt-get install -y gnupg software-properties-common curl
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update
sudo apt-get install terraform
terraform -version

Install AWS CLI:

sudo apt-get update
sudo snap install aws-cli --classic
aws --version
aws configure

Count ๐Ÿงฎ

The count meta-argument accepts a whole number and creates that many instances of the specified resource. Each instance is managed independently. This is helpful for creating multiple identical resources. ๐Ÿ’ก

Example

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}"
  }
}

  1. Initialize Terraform ๐Ÿš€

     terraform init
    

  2. Validate the Configuration โœ…

     terraform validate
    

  3. Apply the Configuration โšก

     terraform apply
    

    Confirm with yes when prompted. ๐Ÿ‘

For_Each ๐Ÿ”„

The for_each meta-argument is used when you need to create multiple resources with different values. Instead of a number, it accepts a map or a set of strings, iterating over each key-value pair. ๐Ÿ”‘

Example with Set of Strings

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-08a0d1e16fc3f61ea",
    "ami-04b70fa74e45c3917",
  ])
}

resource "aws_instance" "server" {
  for_each = local.ami_ids

  ami           = each.key
  instance_type = "t2.micro"

  tags = {
    Name = "Server ${each.key}"
  }
}

Example with Map

locals {
  ami_ids = {
    "linux" : "ami-08a0d1e16fc3f61ea",
    "ubuntu": "ami-04b70fa74e45c3917",
  }
}

resource "aws_instance" "server" {
  for_each = local.ami_ids

  ami           = each.value
  instance_type = "t2.micro"

  tags = {
    Name = "Server ${each.key}"
  }
}

for_each.tf File

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

provider "aws" {
  region = "us-east-1"
}

# Define a local variable for count example
locals {
  ami_ids_set = toset([
    "ami-08a0d1e16fc3f61ea",
    "ami-04b70fa74e45c3917",
  ])
}

resource "aws_instance" "server_set" {
  for_each = local.ami_ids_set

  ami           = each.key
  instance_type = "t2.micro"

  tags = {
    Name = "Server ${each.key}"
  }
}

# Define a different local variable for for_each example
locals {
  ami_ids_map = {
    "linux" : "ami-08a0d1e16fc3f61ea",
    "ubuntu": "ami-04b70fa74e45c3917",
  }
}

resource "aws_instance" "server_map" {
  for_each = local.ami_ids_map

  ami           = each.value
  instance_type = "t2.micro"

  tags = {
    Name = "Server ${each.key}"
  }
}

  1. Initialize Terraform ๐Ÿš€

     terraform init
    

  2. Validate the Configuration โœ…

     terraform validate
    

  3. Apply the Configuration โšก

     terraform apply
    

    Confirm with yes when prompted. ๐Ÿ‘

  1. Clean Up ๐Ÿงน To avoid unnecessary charges, destroy the resources after verification:

     terraform destroy
    

    Confirm with yes when prompted. ๐Ÿ‘Œ

Write About Meta-Arguments and Their Use in Terraform ๐Ÿ“

Meta-Arguments Overview

Meta-arguments in Terraform, such as count and for_each, allow you to manage multiple instances of resources efficiently. They reduce code duplication and make your Terraform configurations more maintainable and scalable. ๐ŸŒฑ

  • Count: Best for creating multiple identical resources. Accepts a number and iterates that many times. ๐Ÿ”ข

  • For_Each: Suitable for creating multiple resources with different values. Accepts a set or map and iterates over each element. ๐Ÿ—‚๏ธ

Using these meta-arguments can significantly simplify your infrastructure as code, making it easier to read, write, and maintain. โœ๏ธ

Happy Learning! ๐ŸŒŸ

Embrace the power of Terraform meta-arguments and enhance your DevOps skills. Keep experimenting and happy coding! ๐Ÿ˜„

Did you find this article valuable?

Support Nilkanth Mistry by becoming a sponsor. Any amount is appreciated!

ย