Launching an EC2 instance using Terraform

Hi there! I'm Dinesh, a passionate Cloud and DevOps enthusiast. I love to dive into the latest new technologies and sharing my journey through blog.
Search for a command to run...

Hi there! I'm Dinesh, a passionate Cloud and DevOps enthusiast. I love to dive into the latest new technologies and sharing my journey through blog.
No comments yet. Be the first to comment.
1. Difference between Docker and Kubernetes Docker → Builds and runs containers.Kubernetes → Orchestrates containers across multiple nodes. Key points: Docker = container runtime. Kubernetes = container orchestration tool. Kubernetes provides auto...
In this session, we learn how to monitor a Kubernetes cluster using Prometheus and Grafana.This is not just theory — there is a GitHub repository containing all installation commands and demo steps.The repo will also be enhanced later with advanced K...
1. What is a ConfigMap in Kubernetes? A ConfigMap is used to store non-sensitive configuration data that your application needs — such as: Database port Connection type Any general configuration values In normal applications (non-Kubernetes), de...
Kubernetes normally supports built-in resources like: Deployment Service Pod ConfigMap Secret Ingress These are called native resources. Sometimes companies (Istio, ArgoCD, Prometheus Operator, Kyverno, etc.) want to add new features that Kub...
1. Why Kubernetes Services Are Needed When a Pod is created in Kubernetes, it receives a dynamic IP address.If the Pod dies and restarts, its IP changes.So other Pods (like checkout → payments) cannot rely on Pod IP because it changes, creating issue...
Terraform is a powerful tool for automating the creation and management of cloud infrastructure. In this guide, we'll walk through the process of launching an EC2 instance on AWS using Terraform, including setting up necessary IAM roles and configuring resources.
Step 1: Create an IAM Role with Required Permissions
Navigate to the IAM Dashboard > Click on IAM > Click Create role.

Choose AWS service

Select EC2 as the service that will use this role > Click Next.

In the Attach permissions policies section, search for vpc > Select AmazonVPCFullAccess

Search for ec2 > Select AmazonEC2FullAccess

Click Next

Provide a Role name > Terraform-ec2-vpc-role

Click Create role

The IAM role is now created.

Select the EC2 instance to which you want to attach the IAM role > click on the Actions button > Under the Security section, select Modify IAM role

In the Modify IAM role dialog, select the IAM role you created from the drop-down menu. > Click on Update IAM role to attach the role to the instance

Step 2: Set Up Your Terraform Project
Create a directory for your Terraform project

Inside this directory, create a file named main.tf. This file will contain the configuration for your AWS EC2 instance.

Copy the blow content and paste it in to the main.tf file
vi main.tf
# Configure the AWS Provider
provider "aws" {
region = "ap-south-1" # Choose your desired region
}
# Create a VPC
resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/16"
}
# Create a Subnet
resource "aws_subnet" "my_subnet" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.1.0/24"
}
# Create an Internet Gateway
resource "aws_internet_gateway" "my_igw" {
vpc_id = aws_vpc.my_vpc.id
}
# Create a Route Table
resource "aws_route_table" "my_route_table" {
vpc_id = aws_vpc.my_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.my_igw.id
}
}
# Associate the Route Table with the Subnet
resource "aws_route_table_association" "my_route_table_assoc" {
subnet_id = aws_subnet.my_subnet.id
route_table_id = aws_route_table.my_route_table.id
}
# Create a Security Group
resource "aws_security_group" "my_sg" {
vpc_id = aws_vpc.my_vpc.id
ingress {
from_port = 22
to_port = 22
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"]
}
}
# Create an EC2 Instance
resource "aws_instance" "Ansible" {
ami = "ami-0c2af51e265bd5e0e" # Replace with a valid AMI ID for ap-south-1
instance_type = "t2.micro"
subnet_id = aws_subnet.my_subnet.id
security_groups = [aws_security_group.my_sg.id]
tags = {
Name = "Ansible VM"
}
# Optional: Add an SSH key pair for access
key_name = "key" # Replace with your actual key pair name
}

Step 3: Initialize and Apply Terraform Configuration
Before applying your configuration, need to initialize the Terraform project:
Run the following command to initialize your Terraform project:
terraform init

Preview Infrastructure Changes:
Use the terraform plan command to preview the changes Terraform will make:
terraform plan

Apply the configuration to create the resources, run the following command:
terraform apply
Terraform will show you a plan of the resources it will create.

If everything looks good, type yes to proceed

Terraform will then provision the resources

Step 4: Verify Resource Creation
After the apply process is complete, verify the new EC2 instance and VPC in the AWS Management Console.


Step 5: Clean Up Resources
To remove the resources created by Terraform, use the terraform destroy command:
terraform destroy
All the resources created by Terraform are now deleted.
