CREATING MULTIPLE EC2 INSTANCES HAVING DIFFERENT CONFIGURATION USING TERRAFORM
CREATING MULTIPLE EC2 INSTANCES HAVING DIFFERENT CONFIGURATION USING TERRAFORM
Creating Multiple EC2 Instances with Different Configurations Using Terraform (Using count and for_each)
Introduction:
This documentation provides a step-by-step guide on how to create multiple EC2 instances with different configurations using Terraform. We will leverage the count and for_each feature in Terraform along with separate variable and configuration files for better organization and flexibility. By following this guide, you will be able to create and manage multiple EC2 instances with unique configurations using Terraform.
Prerequisites: Before proceeding with the steps outlined in this documentation, make sure you have the following prerequisites in place:
1. An AWS account: You will need an active AWS account with appropriate permissions to create EC2 instances.
2. Terraform installed: Ensure that Terraform is installed on your local machine. You can download the latest version of Terraform from the official website (https://www.terraform.io/downloads.html) and follow the installation instructions provided.
Step 1: Configure AWS Credentials To allow Terraform to interact with your AWS account, you need to configure your AWS access credentials. Follow these steps to set up your credentials:
- Open the AWS Management Console and navigate to the IAM service.
- Create a new IAM user or use an existing one. Assign the necessary permissions for creating EC2 instances and related resources. Note down the Access Key ID and Secret Access Key associated with the IAM user.
- On your local machine, open a terminal or command prompt, and execute the following command to configure the AWS credentials: $ aws configure
- Enter your Access Key ID, Secret Access Key, default region (e.g., us-east-1), and preferred output format (e.g., json) when prompted.
Step 2: Create a Variables File Create a new file named variables.tf in your Terraform project directory. This file will contain the variable definitions for the EC2 instance configurations.
variables.tf content:
variable “configuration” {
description = “The total configuration, List of Objects/Dictionary”
default = [{}]
}
We define a variable named instance_configs of type map(object ({})) to hold the configurations of our instances. Each configuration includes the ami (Amazon Machine Image), instance_type, and key_name values.
Step 3: Create a Configuration File Create a new file named dev.tfvars in your Terraform project directory. This file will contain the actual instance configurations.
dev.tfvars content:
configuration = [
{
“application_name” : “Instance-one”,
“ami” : “ami-09e67e426f25ce0d7”,
“no_of_instances” : “2”,
“instance_type” : “t2.medium”,
},
{
“application_name” : “Instance-two”,
“ami” : “ami-0747bdcabd34c712a”,
“instance_type” : “t2.micro”,
“no_of_instances” : “1”,
},
{
“application_name” : “Instance-three”,
“ami” : “ami-0747bdcabd34c712a”,
“instance_type” : “t2.micro”,
“no_of_instances” : “3”
}
]
We define the instance configurations using the configuration variable. Each configuration includes a unique name (e.g., Instance-one, Instance-two, Instance-three) and the respective values for ami, instance_type and no_of_instances (count)
Step 4: Create a Configuration File for Resources Create a new file named main.tf in your Terraform project directory. This file will contain the main configuration and resource definitions for creating the EC2 instances.
main.tf content:
terraform {
required_providers {
aws = {
source = “hashicorp/aws”
version = “~> 4.16”
}
}
required_version = “>= 1.2.0”
}
provider “aws” {
region = “us-east-1”
}
locals {
serverconfig = [
for server in var.configuration : [
for i in range(1, server.no_of_instances+1) : {
instance_name = “${server.application_name}-${i}”
instance_type = server.instance_type
ami = server.ami
}
]
]
}
// We need to Flatten it before using it
locals {
instances = flatten(local.serverconfig)
}
resource “aws_instance” “web” {
for_each = {for server in local.instances: server.instance_name => server}
ami = each.value.ami
instance_type = each.value.instance_type
tags = {
Name = “${each.value.instance_name}”
}
}
We define the aws_instance resource block with the for_each parameter set to var.configuration. This will dynamically create multiple EC2 instances based on the configurations defined in dev.tfvars. The ami and instance_type values are extracted from each configuration using each.value and applied to the instances. Additionally, a unique tag is set for each instance using each.key.
Step 5: Initialize Terraform Before you can use Terraform, you need to initialize the project. Execute the following command in the terminal or command prompt:
$ terraform init
This command will initialize the Terraform project and download the necessary provider plugins.
Step 6: Apply the Changes To create the multiple EC2 instances with different configurations, execute the following command:
$ terraform apply -var-file=dev.tfvars
Terraform will prompt for confirmation. Type yes and press Enter to proceed. It will then create the specified EC2 instances with their respective configurations.
Step 7: Verify the EC2 Instances Once the apply command completes, Terraform will display the outputs and details of the created resources.
You can also verify the creation of the EC2 instances in the AWS Management Console:
1. Open the AWS Management Console and navigate to the EC2 service.
2. Click on “Instances” in the sidebar to view the list of instances.
3. Verify that the EC2 instances with the specified Name tags (e.g., “Instance-one”, “Instance-two”, “Instance-three”) are present and have the desired configurations.
Congratulations! You have successfully created multiple EC2 instances with different configurations using Terraform’s count and for_each feature. By separating the configurations into a variables file (variables.tf) and a configuration file (dev.tfvars), and using the main.tf file for defining resources, you can achieve better organization and flexibility in managing your infrastructure.
Cleanup: To avoid incurring unnecessary costs, consider cleaning up the resources created by Terraform after you have finished using them. Execute the following command to destroy the created resources:
$ terraform destroy -var-file=dev.tfvars
Terraform will prompt for confirmation. Type yes and press Enter to proceed with the destruction of the resources.
Please note that the destroy command permanently removes the resources, so exercise caution when using it.
Conclusion:
In this documentation, you learned how to create multiple EC2 instances with different configurations using Terraform’s count and for_each features, along with separate variable and configuration files for better organization and flexibility. By leveraging these features and separating the configurations, you can dynamically create and manage multiple instances with ease and consistency. Terraform’s declarative approach ensures reproducibility and scalability in your infrastructure deployments.
Documented by: Brishav U. Kuikel
https://www.linkedin.com/in/brishav-u-kuikel-b2b78025a/
2023 July 22