DevOps(Day-69): Meta-Arguments in Terraform

DevOps(Day-69): Meta-Arguments in Terraform

TABLE OF CONTENTS

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.

Write about meta-arguments and their use in Terraform.

Meta-arguments in Terraform refer to a set of special arguments that can be used to define how a particular resource or module behaves within a Terraform configuration. These arguments provide additional configuration options and enable fine-grained control over the behaviour of resources or modules.

Meta-arguments are prefixed with the meta keyword and are specified within a resource or module block. They allow you to modify the behaviour of the resource or module at a higher level, influencing how it is processed during Terraform execution.

Here are a few common meta-arguments used in Terraform:

  1. meta: The meta meta-argument allows you to attach arbitrary metadata to a resource or module block. It can be used for documenting the purpose or any other information related to the resource or module.

  2. depends_on: The depends_on meta-argument specifies explicit dependencies between resources. By declaring dependencies, you can control the order in which resources are created or updated. Terraform ensures that resources with dependencies are created or updated in the correct order.

  3. count: The count meta-argument allows you to create multiple instances of a resource or module based on a count expression. It enables the creation of multiple similar resources without duplicating the entire block. The count expression can be a number or a list, providing flexibility in defining resource instances.

  4. provider: The provider meta-argument lets you specify a different provider configuration for a resource or module block. It allows you to override the default provider configuration and use a different provider for specific instances. This is useful when you have multiple providers for different environments or regions.

  5. for_each: The for_each meta-argument is similar to count, but instead of using a numeric expression, it takes a map or set of strings as input. It allows you to create multiple instances of a resource or module based on the keys or values of the map or set. This is particularly helpful when you want to create resources dynamically based on input data.

Meta-arguments provide powerful capabilities to customize and control the behavior of Terraform resources and modules. They allow you to express complex configurations and handle dynamic scenarios effectively. By leveraging meta-arguments, you can ensure that your infrastructure is managed and provisioned in a flexible and scalable manner.

Task-01: Meta-Arguments HandsOn

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

    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.

  • Previously, we spun up EC2 instances. In the same resource block, we can add a count key-value pair to spin multiple instances. Create a count_main.tf file containing providers and resources.

  • Initialise the terraform file with terraform init command.

  • Now, use terraform apply the command to create the instances.

  • Navigate top the EC2 management console to view the instances.

    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.

  • Create a for_each terraform file and include the string values you want to spin up the servers with.

 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-0889a44b331db0194",
  "ami-007855ac798b5175e",
  ])
  }

  resource "aws_instance" "server" {
  for_each = local.ami_ids
  ami = each.key
  instance_type = "t2.micro"
  tags = {
  Name = "Server ${each.key}"
  }
  }
  • With terraform apply new instances will be created on the key name that was passed.

  • Navigate to the console to check the newly spun instances.

Thanks for reading my article. Have a nice day.

WRITTEN BY Biswaraj Sahoo --AWS Community Builder | DevOps Engineer | Docker | Linux | Jenkins | AWS | Git | Terraform | Docker | kubernetes

Empowering communities via open source and education. Connect with me over linktree: linktr.ee/biswaraj333