<?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[Anshu's Blog]]></title><description><![CDATA[DevOps Enthusiast]]></description><link>https://anshumandubey.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Thu, 10 Sep 2026 19:31:04 GMT</lastBuildDate><atom:link href="https://anshumandubey.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How to Set Up EC2 Infrastructure Using Terraform (IAC) 🎉]]></title><description><![CDATA[Introduction
In today's cloud-centric landscape, provisioning and managing infrastructure efficiently are paramount. Amazon EC2 (Elastic Compute Cloud) offers scalable virtual servers on AWS, empowering businesses to deploy applications quickly and s...]]></description><link>https://anshumandubey.hashnode.dev/how-to-set-up-ec2-infrastructure-using-terraform-iac</link><guid isPermaLink="true">https://anshumandubey.hashnode.dev/how-to-set-up-ec2-infrastructure-using-terraform-iac</guid><category><![CDATA[Terraform]]></category><category><![CDATA[#IaC]]></category><category><![CDATA[AWS]]></category><category><![CDATA[ec2]]></category><dc:creator><![CDATA[Anshuman Dubey]]></dc:creator><pubDate>Wed, 22 May 2024 12:34:04 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-introduction">Introduction</h2>
<p>In today's cloud-centric landscape, provisioning and managing infrastructure efficiently are paramount. Amazon EC2 (Elastic Compute Cloud) offers scalable virtual servers on AWS, empowering businesses to deploy applications quickly and securely. Terraform complements this by enabling Infrastructure as Code (IAC), allowing users to define and manage infrastructure in a declarative manner.</p>
<h3 id="heading-why-terraform"><strong>Why Terraform? 🛠️</strong></h3>
<p>Terraform offers a plethora of benefits:</p>
<ul>
<li><p><strong>Declarative Configuration</strong>: Describe infrastructure's desired state effortlessly, ensuring easy understanding and maintenance.</p>
</li>
<li><p><strong>Infrastructure as Code (IAC)</strong>: Treat infrastructure like software, enabling version control, collaboration, and automated deployments.</p>
</li>
<li><p><strong>Multi-Cloud Support</strong>: Provision and manage resources across various cloud providers and on-premises environments seamlessly.</p>
</li>
</ul>
<h3 id="heading-key-components"><strong>Key Components 📦</strong></h3>
<p>Setting up EC2 instances involves several crucial components:</p>
<ul>
<li><p><strong>EC2 Instances</strong>: Virtual servers in the AWS cloud, available in various configurations to meet diverse application needs.</p>
</li>
<li><p><strong>Security Groups</strong>: Firewall rules controlling inbound and outbound traffic to EC2 instances.</p>
</li>
<li><p><strong>Key Pairs</strong>: SSH key pairs for secure access to EC2 instances.</p>
</li>
<li><p><strong>Networking</strong>: Configuration of VPC (Virtual Private Cloud), subnets, and internet gateways for network connectivity.</p>
</li>
</ul>
<h3 id="heading-lets-get-started"><strong>Let's Get Started! 🚀</strong></h3>
<h4 id="heading-prerequisites"><strong>Prerequisites:</strong></h4>
<p>Before diving in, ensure you have:</p>
<ul>
<li><p>An AWS account with appropriate permissions to create EC2 instances.</p>
</li>
<li><p>Terraform installed on your machine. Follow this blog to install terraform <a target="_blank" href="https://anshudubey344.hashnode.dev/installation-of-nginx-in-docker-by-using-terraform"><code>Terraform Install</code></a></p>
</li>
<li><p>Configure AWS CLI</p>
</li>
</ul>
<h3 id="heading-go-with-code">Go With Code</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1716375882099/e6c1257e-ff55-4a18-9c37-f42247d77362.png" alt class="image--center mx-auto" /></p>
<p>create <strong><em>terraform.tf</em></strong> this main terraform configuration file.</p>
<pre><code class="lang-plaintext">terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~&gt; 5.0"
    }
  }
}
</code></pre>
<p>create <strong><em>provider.tf</em></strong> file for provider configuration of AWS</p>
<pre><code class="lang-plaintext">provider "aws" {
       region = "us-east-1"
}
</code></pre>
<p>create <strong><em>ec2.tf</em></strong> Configuration file for <mark>Creating AWS EC2 Instance</mark></p>
<pre><code class="lang-plaintext">resource "aws_default_vpc" "anshu_vpc"{
}

resource "aws_security_group" "example_security_group" {
  name        = "Anshu SG"
  description = "This is the security group for EC2 instance"
  vpc_id      = aws_default_vpc.anshu_vpc.id  # Replace with your VPC ID

  // Define ingress rules (inbound traffic)
  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]  # Allow SSH access from anywhere
  }

  // Define egress rules (outbound traffic)
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]  # Allow all outbound traffic
  }
}

resource "aws_instance" "anshu_instance" {

        ami = var.ami
        instance_type = var.instance_type
        security_groups =[aws_security_group.example_security_group.name]
        key_name = var.key_pair

        tags = {
                Name = "terraform_instance"
          }

        ebs_block_device {
                device_name = "/dev/sdf"  // Device name (e.g., /dev/sdf)
                volume_size = var.volume_size  // Size of the volume in GB
                volume_type = "gp2"  // Volume type
                encrypted   = true  // Whether the volume should be encrypted
                }
}
</code></pre>
<p>Create <strong><em>variable.tf</em></strong></p>
<pre><code class="lang-plaintext">variable "key_pair" {
  description = "Please mentioned your key pair "
}

variable "instance_type" {
  description = "Type of EC2 instance"
}

variable "ami" {
  description = "AMI ID for the EC2 instance"

}
variable "volume_size" {
  description = "Size of the root volume in GB"

}
</code></pre>
<p>create <strong><em>terraform.tfvars</em></strong> this is <mark>default variable file of Terraform.</mark></p>
<pre><code class="lang-plaintext">ami = "ami-080e1f13689e07408"  #change with your AMI

volume_size = "8"   #SIZE of Volume

instance_type = "t2.micro"  

key_pair = "anshu"  #change with your Key Pair
</code></pre>
<p>create <strong><em>output.tf</em></strong> file to show the output after creating <mark>the infrastructure like public_ip</mark></p>
<pre><code class="lang-plaintext">output "public_ip" {
  description = "The public IP address of the created EC2 instance"
  value       = aws_instance.anshu_instance.public_ip
}
</code></pre>
<p><strong><mark>After Creating all these files make sure you configure AWS on your Machine with Permissions.</mark></strong></p>
<h3 id="heading-initializing-your-terraform-project"><strong>Initializing Your Terraform Project</strong></h3>
<p>Before you can deploy your EC2 infrastructure using the Terraform configuration, you need to initialize your Terraform project.</p>
<pre><code class="lang-bash">terraform init
</code></pre>
<h3 id="heading-validating-your-terraform-configuration"><strong>Validating Your Terraform Configuration</strong></h3>
<p>The next step is to ensure that your Terraform configuration is syntactically correct and internally consistent. This can be done using</p>
<pre><code class="lang-bash">terraform validate
</code></pre>
<h3 id="heading-planning-your-terraform-deployment"><strong>Planning Your Terraform Deployment</strong></h3>
<p>Next step is to create an execution plan. Command shows you what actions Terraform will take to achieve the desired state described in your configuration files.</p>
<pre><code class="lang-bash">terraform plan
</code></pre>
<h3 id="heading-applying-your-terraform-configuration"><strong>Applying Your Terraform Configuration</strong></h3>
<p>The <code>terraform apply</code> command executes the actions proposed in the plan, creating or updating the resources defined in your Terraform configuration files.</p>
<pre><code class="lang-bash">terraform apply
</code></pre>
<h3 id="heading-conclusion"><strong>Conclusion 🎉</strong></h3>
<p>Armed with this comprehensive guide, you're now equipped to embark on an exhilarating journey to deploy EC2 instances on AWS using Terraform. 🌈 This approach fosters consistency, scalability, and automation, empowering your organization to thrive and conquer the dynamic cloud landscape with confidence and flair. 🚀💻✨</p>
<hr />
<p>If you have any questions or queries, don't hesitate to ask. I'm here to help and provide answers to any uncertainties you may have. Feel free to reach out anytime!</p>
<p>Thank You</p>
<hr />
<p>Happy Learning</p>
<p>Keep Exploring</p>
<p><a class="user-mention" href="https://hashnode.com/@anshudubey344">Anshuman Dubey</a></p>
]]></content:encoded></item></channel></rss>