Effortless Multiple AWS EC2 Deployment with Terraform: Automate Your Infrastructure Today!

Deploying Multiple AWS EC2 instances with Terraform

Introduction 

Terraform is an open-source Infrastructure as Code (IaC) tool that uses a declarative configuration language to let users define and provision cloud infrastructure. This post explains how to set up security groups, deploy EC2 instances in AWS, and specify output values for obtaining instance information. 

Objectives:

  • Deploy EC2 instances in AWS. 
  • Set up security groups to control both outgoing and incoming traffic. 
  • Define output values to retrieve instance information effectively. 
  • Automate the deployment of infrastructure to improve management and scalability. 

Prerequisites:

Before proceeding, ensure you have: 

  • An AWS account 
  • Terraform installed on your system 
  • AWS CLI configured with appropriate credentials 

Note:-

Change the ami_id variable to match your required AMI. 
Modify the security group rules as per your requirements. 

ALSO READ:

Configuration Breakdown 

  1. Defining Variables

We define variables to parameterize our infrastructure, making it reusable and scalable. 

variable "ami_id" {
  type        = string
  default     = "ami-09c813fb71547fc4f"
  description = "This is ami id RHEL9"
}

variable "instance_name_loop" {
  type        = list(any)
  default     = ["mysql", "frontend", "backend"]
  description = "description"
}

variable "environment" {
  type        = string
  default     = "dev"
  description = "this is env variable"
}

variable "instance_type" {
  type        = string
  default     = "t2.micro"
  description = "This is instance type"
}

variable "ec2_tgsa" {
  type = map(any)
  default = {
    project     = "expenses"
    component   = "backend"
    environment = "dev"
    Name        = "expenses dev backend"
  }
  description = "description"
}

variable "from_port" {
  type        = number
  default     = 0
  description = "Starting port for inbound rules (0 allows all)"
}

variable "to_port" {
  type        = number
  default     = 0
  description = "Ending port for inbound rules (0 allows all)"
}

variable "protocol" {
  type        = string
  default     = "-1"
  description = "this is protocal allow all"
}

variable "cidr_blocks" {
  type        = list(string)
  default     = ["0.0.0.0/0"]
  description = "Allow all ip"
}
variable "sg_tags" {
  type = map(any)
  default = {
    Name = "allow_tl"
  }
  description = "security tags"
}


  1. Creating an EC2 Instance

The aws_instance resource defines three EC2 instances using the count parameter. Each instance is assigned a unique name from the instance_name_loop variable. 

resource "aws_instance" "foo" {
  count                  = 3
  ami                    = var.ami_id
  vpc_security_group_ids = [aws_security_group.allow_tl.id]
  instance_type          = var.environment == "prod" ? "t3.micro" : "t2.micro"
  tags = {
    Name = var.instance_name_loop[count.index]
  }
}
  1. Creating a Security Group

We define an AWS security group allowing SSH traffic and enabling outbound access. 

resource "aws_security_group" "allow_tl" {
  name        = "allow_tl"
  description = "Allow TLS inbound traffic and all outbound traffic"
  ingress {
    from_port   = var.from_port
    to_port     = var.to_port
    protocol    = var.protocol
    cidr_blocks = var.cidr_blocks
  }
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
  tags = var.sg_tags
}
  1. Defining Output Values

We use output variables to retrieve helpful information, such as instance IP addresses and security group IDs. 

#output "ec2_info" {
#  value       = aws_instance.foo
#  description = "This is output of the ec2 instance creation"
#}


output "security_group_id" {
  description = "value"
  value       = aws_security_group.allow_tl.id
}

output "instance_ip_mapping" {
  description = "value"
  value = {
    for instance, name in var.instance_name_loop :
    name => {
      private_ip = aws_instance.foo[instance].private_ip
      public_ip  = aws_instance.foo[instance].public_ip

    }
  }
}

Defining Provider

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "5.91.0"
    }
  }
}

provider "aws" {
  # Configuration options
  region = "us-east-1"
}

Deploying the Infrastructure 

  • Initialize : Prepares the working directory.


    terraform init 

  • Validate Configuration: Checks for syntax errors.


    terraform validate 

  • Plan Deployment: Preview the changes before applying.


    terraform plan 

  • Apply Configuration: Deploys the infrastructure.


    terraform apply -auto-approve 

  • Verify Output: Displays instance details.


    terraform output 

Example Screenshots:

EC2 Instances with Terraform


Deploying EC2 Instances with Terraform and Security Groups in AWS

Deploying EC2 Instances with Terraform and Security Groups in AWS

1 thought on “Effortless Multiple AWS EC2 Deployment with Terraform: Automate Your Infrastructure Today!”

Leave a Comment

Index