<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Untitled Publication]]></title><description><![CDATA[Untitled Publication]]></description><link>https://prasanna-s.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Tue, 08 Sep 2026 13:23:17 GMT</lastBuildDate><atom:link href="https://prasanna-s.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Building Resilient Infrastructure: Deploying EC2 Instances Across Multiple Availability Zones using Terraform]]></title><description><![CDATA[Introduction
Deploying EC2 Instances in Multiple Availability Zones with a Load Balancer Using Terraform
Deploying EC2 instances across multiple Availability Zones (AZs) is a key practice for ensuring high availability and fault tolerance of your app...]]></description><link>https://prasanna-s.hashnode.dev/building-resilient-infrastructure-deploying-ec2-instances-across-multiple-availability-zones-using-terraform</link><guid isPermaLink="true">https://prasanna-s.hashnode.dev/building-resilient-infrastructure-deploying-ec2-instances-across-multiple-availability-zones-using-terraform</guid><category><![CDATA[#CloudInfrastructureAutomatio]]></category><category><![CDATA[#LoadBalancerConfiguration]]></category><category><![CDATA[#MultiAvailabilityZoneDeployment]]></category><category><![CDATA[high availability]]></category><category><![CDATA[#InfrastructureAsCode]]></category><dc:creator><![CDATA[Prasanna]]></dc:creator><pubDate>Wed, 31 Jul 2024 18:40:03 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1722449393057/e4d5e727-2ff2-4573-a634-331ad9d9f927.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-introduction"><strong>Introduction</strong></h3>
<p>Deploying EC2 Instances in Multiple Availability Zones with a Load Balancer Using Terraform</p>
<p>Deploying EC2 instances across multiple Availability Zones (AZs) is a key practice for ensuring high availability and fault tolerance of your applications on AWS. Using Terraform, an infrastructure-as-code (IaC) tool, you can automate this process and manage your infrastructure efficiently. In this blog, we will walk you through the steps to deploy EC2 instances in multiple AZs and set up a load balancer to distribute traffic among them.</p>
<h3 id="heading-prerequisites">Prerequisites</h3>
<h3 id="heading-before-we-begin-make-sure-you-have-the-following">Before we begin, make sure you have the following:</h3>
<p><strong>1.AWS Account</strong>: An active AWS account.</p>
<p><strong>2.Terraform</strong>: Installed on your local machine. You can download it from <a target="_blank" href="https://www.terraform.io/downloads.html">here</a>.</p>
<p><strong>3.AWS CLI</strong>: Installed and configured with your AWS credentials. You can download it from <a target="_blank" href="https://aws.amazon.com/cli/">here</a>.</p>
<h3 id="heading-step-1-set-up-your-terraform-project">Step 1: Set Up Your Terraform Project</h3>
<p>First, create a directory for your Terraform project. Inside this directory, create a new file called <a target="_blank" href="http://main.tf"><code>main.tf</code></a>.</p>
<pre><code class="lang-bash">mkdir terraform-ec2-multi-az
<span class="hljs-built_in">cd</span> terraform-ec2-multi-az
touch main.tf
</code></pre>
<h3 id="heading-step-2-define-the-provider-and-variables">Step 2: Define the Provider and Variables</h3>
<p>In your <a target="_blank" href="http://main.tf"><code>main.tf</code></a>, start by defining the AWS provider and necessary variables.</p>
<pre><code class="lang-plaintext">provider "aws" {
  region = "us-west-2"  # Replace with your preferred region
}

variable "instance_count" {
  description = "Number of EC2 instances"
  default     = 2
}

variable "instance_type" {
  description = "Type of EC2 instance"
  default     = "t2.micro"
}

variable "vpc_id" {
  description = "VPC ID where instances will be deployed"
}

variable "subnet_ids" {
  description = "List of Subnet IDs in different AZs"
  type        = list(string)
}

variable "ami_id" {
  description = "AMI ID for the EC2 instances"
}
</code></pre>
<h3 id="heading-step-3-create-a-security-group">Step 3: Create a Security Group</h3>
<p>Create a security group to allow necessary traffic to your EC2 instances.</p>
<pre><code class="lang-plaintext">resource "aws_security_group" "web_sg" {
  name        = "web_sg"
  description = "Allow web traffic"
  vpc_id      = var.vpc_id

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}
</code></pre>
<h3 id="heading-step-4-launch-ec2-instances">Step 4: Launch EC2 Instances</h3>
<p>Use a <code>for_each</code> loop to deploy EC2 instances across multiple AZs.</p>
<pre><code class="lang-plaintext">resource "aws_instance" "web" {
  count = var.instance_count

  ami                         = var.ami_id
  instance_type               = var.instance_type
  subnet_id                   = element(var.subnet_ids, count.index)
  security_groups             = [aws_security_group.web_sg.name]

  tags = {
    Name = "web-instance-${count.index + 1}"
  }
}
</code></pre>
<h3 id="heading-step-5-create-a-load-balancer">Step 5: Create a Load Balancer</h3>
<h3 id="heading-set-up-an-elastic-load-balancer-elb-to-distribute-traffic-among-your-ec2-instances">Set up an Elastic Load Balancer (ELB) to distribute traffic among your EC2 instances.</h3>
<pre><code class="lang-plaintext">resource "aws_elb" "web_elb" {
  name               = "web-load-balancer"
  availability_zones = var.subnet_ids

  listener {
    instance_port     = 80
    instance_protocol = "HTTP"
    lb_port           = 80
    lb_protocol       = "HTTP"
  }

  health_check {
    target              = "HTTP:80/"
    interval            = 30
    timeout             = 5
    healthy_threshold   = 2
    unhealthy_threshold = 2
  }

  instances = [for instance in aws_instance.web : instance.id]

  tags = {
    Name = "web-elb"
  }
}
</code></pre>
<h3 id="heading-step-6-apply-the-configuration">Step 6: Apply the Configuration</h3>
<p>Initialize Terraform, review the execution plan, and apply the configuration to create your resources.</p>
<pre><code class="lang-bash">terraform init
terraform plan
terraform apply
</code></pre>
<h3 id="heading-conclusion">Conclusion</h3>
<p>By following these steps, you have successfully deployed EC2 instances in multiple Availability Zones and set up a load balancer using Terraform. This setup ensures high availability and fault tolerance for your application, providing a resilient architecture on AWS.</p>
<p>Terraform makes infrastructure management more predictable and less error-prone by allowing you to define your infrastructure as code. Keep exploring Terraform’s capabilities to manage your AWS infrastructure more efficiently.</p>
<p>Feel free to expand this setup with additional features like auto-scaling, advanced monitoring, and integrating with other AWS services.</p>
<p>-Hope you like this blog! Happy Learning!</p>
]]></content:encoded></item></channel></rss>