Day 69 - Meta-Arguments in Terraform

Day 69 - Meta-Arguments in Terraform

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.

eg.

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

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 sourced 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 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.

Run terraform init to initialize the Terraform project.

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

Meta argument - for_each

No alt text provided for this image

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-argument 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
No alt text provided for this image

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 different AMIs. In this case, we use a map with two key-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 (i.e., the AMI ID), and set the "ami" attribute to it. We also use the "each.key" variable to access the current key in the map (i.e., the AMI name), 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
No alt text provided for this image

Two new instances created using 'for_each' meta argument

Write about meta-arguments and its use in Terraform.

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

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

Another common use of meta-arguments is to control the order in which resources are created. The "count" and "for_each" meta-arguments can be used to create multiple instances of a resource, and the "lifecycle" meta-argument 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 behavior of resources in a flexible and granular way. By using meta-arguments effectively, Terraform users can create more robust and scalable infrastructure as code.



Thanks For Reading!!!!

要查看或添加评论,请登录

Anup D Ghattikar的更多文章

  • Django Rest Framework

    Django Rest Framework

    Introduction to Django Rest Framework Django REST framework (DRF) is a powerful and elegant toolkit built on top of the…

    11 条评论
  • Docker Tool Mastering

    Docker Tool Mastering

    What is Docker? Docker is a platform and toolset that simplifies the process of developing, deploying, and running…

    4 条评论
  • Devop's Bits & Byte's - By Anup Ghattikar

    Devop's Bits & Byte's - By Anup Ghattikar

    Medium Link: https://medium.com/@aghattikar82/devops-bits-bytes-by-anup-ghattikar-9a8c6b788ec9 HashNode Link:…

    10 条评论
  • Kubernetes Troubleshooting

    Kubernetes Troubleshooting

    Cluster Management Display endpoint information about the master and services in the cluster kubectl cluster-info…

    3 条评论
  • Kubernetes Cluster Maintenance

    Kubernetes Cluster Maintenance

    Kubernetes Cluster Upgrade Upgrade master Upgrading the control plane consist of the following steps: Upgrade kubeadm…

    2 条评论
  • Day 05:Kubernetes Storage Kubernetes Security

    Day 05:Kubernetes Storage Kubernetes Security

    Persistent Volumes Managing storage is a distinct problem from managing compute instances. The PersistentVolume…

  • Kubernetes services and service discovery

    Kubernetes services and service discovery

    Kubernetes is a powerful platform for deploying, scaling, and managing containerized applications. However, once you…

  • Day 03:Kubernetes Workloads

    Day 03:Kubernetes Workloads

    Kubernetes Deployment with YAML YAML (which stands for YAML Ain’t Markup Language) is a language used to provide…

    2 条评论
  • Kubernetes Networking

    Kubernetes Networking

    Topics Are: Services, Ingress, Network Policies, DNS, CNI Plugins Services Services provide a way to expose a set of…

    7 条评论
  • Day 70 - Terraform Modules

    Day 70 - Terraform Modules

    Modules are containers for multiple resources that are used together. A module consists of a collection of .

    2 条评论

社区洞察

其他会员也浏览了