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 as server 0 , server 1
When each instance is created, it has its 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 = 2
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
tags = {
Name = "Server ${count.index}"
}
}
Let's do it practically!
First, we will learn what is the meaning of the code
The "terraform" block is the first portion of the code. Here, we define the Terraform version and the necessary providers. In this scenario, we state that the AWS provider is required and that it should be sourced from the "hashicorp/aws" module with a version more than or equal to 4.16.
Following that, we define the "provider" section, which defines the AWS provider's setup. In this situation, the area is specified as "us-east-1"
The "aws_instance" resource block is responsible for the creation of EC2 instances. Using the "count" meta-argument, two instances are created, each with the provided AMI and instance type. The "tags" block is used to give each instance a name and a unique index based on the count.
Run terraform init to initialize the Terraform project.
terraform validate
terraform plan
terraform apply - Run terraform apply to create 2 instances
EC2 instances are successfully created!
Now let's destroy them and move forward with another few interesting examples
Instances are terminated!
Now let's destroy this and move forward with another last interesting example - Run terraform destroy
for_each
Use InstanceName with AMI-id
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.
Here we are launching the same instances did before just that instance name will be displayed with its ami id
#define provider
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">=1.2.0"
}
#define AWS region
provider "aws" {
region = "us-east-1"
}
#define ami Ids
locals {
ami_ids = toset([
"ami-0230bd60aa48260c6",
"ami-0fc5d935ebf8bc3bc",
])
}
#create 2 instances t2.micro with above ami ids
#using for_each to create 2 instances
resource "aws_instance" "server" {
for_each = local.ami_ids
ami = each.key
instance_type = "t2.micro"
tags = {
Name = "Server ${each.key}"
}
}
As performed before we will be writing above code in main.tf and following terraform validate, terraform plan, terraform apply
for_each
Use InstanceName with Key Value - Linux & Ubuntu
Here we are launching the same instances did before just that instance name will be displayed with keys as Server Linux and Server Ubuntu
#define provider
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">=1.2.0"
}
#define AWS region
provider "aws" {
region = "us-east-1"
}
#Multiple key value iteration
#linux & ubuntu key will be displayed when instance will get created
locals {
ami_ids = {
"linux" :"ami-0230bd60aa48260c6",
"ubuntu": "ami-0fc5d935ebf8bc3bc",
}
}
resource "aws_instance" "server" {
for_each = local.ami_ids
ami = each.value
instance_type = "t2.micro"
tags = {
Name = "Server ${each.key}"
}
}
Now you must understand that I will be going to update main.tf with the above code and following the same terraform validate, terraform plan, terraform apply to reveal magic of instance creation in a moment!
See in the AWS console!
Now its the time to run terraform destroy this setup as well and appreciate today's learnings!
Conclusion:
By leveraging the for_each
meta-argument in Terraform, you can dynamically create AWS instances tailored to different environments using local AMI IDs. This approach enhances code reusability, scalability, and maintainability, as changes to the AMIs can be easily managed within the local block.
Terraform's flexibility and powerful meta-arguments make it a go-to choice for managing infrastructure, providing developers with the tools they need to create, modify, and scale resources effortlessly.
Thanks for spending your valuable time in learning to enhance your career!😃🙏
Follow me on
Hashnode: https://kshitijaa.hashnode.dev/
LinkedIn: https://www.linkedin.com/in/kshitija-bartakke-malwade-39678b141