Terraform AWS EC2 Deployment – Beginner Project
This project demonstrates how to deploy a simple EC2 instance (t2.micro) on AWS using Terraform.
It covers the fundamentals of Infrastructure as Code (IaC) — providers, resources, variables, outputs, security groups, and state management.
🚀 Project Features
AWS Provider configuration
EC2 instance deployment
Security Group creation
SSH Key Pair for authentication
Variables & Outputs
Terraform workflow (init → plan → apply → destroy)
🏗 Architecture
Terraform → AWS Provider → EC2 Instance (t2.micro)
└── Security Group (SSH 22)
└── Key Pair
✅ TASK 1 — Create a Simple EC2 Instance (FULL GUIDE)
Directory Structure:
terraform-ec2/
├── main.tf
├── variables.tf
├── outputs.tf
└── terraform.tfvars
STEP 1 — Install Terraform
(If already installed, skip)
terraform -v
STEP 2 — Create a Working Directory
mkdir terraform-ec2
cd terraform-ec2
STEP 3 — Configure AWS Provider (main.tf)
Create main.tf:
provider "aws" {
region = var.aws_region
}
# Key pair
resource "aws_key_pair" "my_key" {
key_name = "terraform-key"
public_key = file(var.public_key_path)
}
# Security group
resource "aws_security_group" "ec2_sg" {
name = "ec2_sg"
description = "Allow SSH inbound traffic"
ingress {
description = "SSH"
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"]
}
}
# EC2 instance
resource "aws_instance" "my_ec2" {
ami = var.ami_id
instance_type = "t2.micro"
key_name = aws_key_pair.my_key.key_name
vpc_security_group_ids = [aws_security_group.ec2_sg.id]
tags = {
Name = "terraform-ec2"
}
}
STEP 4 — Create Variables File (variables.tf)
variable "aws_region" {
description = "AWS region"
type = string
}
variable "ami_id" {
description = "AMI ID for EC2"
type = string
}
variable "public_key_path" {
description = "Path to your SSH public key"
type = string
}
STEP 5 — Add Outputs File (outputs.tf)
output "instance_public_ip" {
value = aws_instance.my_ec2.public_ip
}
output "instance_id" {
value = aws_instance.my_ec2.id
}
STEP 6 — Add Values (terraform.tfvars)
aws_region = "ap-south-1"
ami_id = "ami-0f5ee92e2d63afc18" # Amazon Linux 2 (Mumbai)
public_key_path = "~/.ssh/id_rsa.pub"
Use the correct AMI for your region.
You can get it from AWS Console → EC2 → Images → AMI.
STEP 7 — Create SSH Key (if you don’t have)
Run:
ssh-keygen -t rsa -b 4096
Press enter 3 times.
Your key will be created:
~/.ssh/id_rsa
~/.ssh/id_rsa.pub
STEP 8 — Initialize Terraform
terraform init
This downloads AWS provider plugins.
STEP 9 — Validate Config
terraform validate
STEP 10 — Preview Changes
terraform plan
STEP 11 — Apply (Create the EC2)
terraform apply
Type yes.
🎉 RESULT
Terraform creates:
✔ Key pair
✔ Security group
✔ EC2 instance (t2.micro)
You will see outputs like:
instance_public_ip = "13.x.x.x"
instance_id = "i-0abcd1234efg"
STEP 12 — Connect to your EC2 instance
ssh -i ~/.ssh/id_rsa ec2-user@<PUBLIC-IP>
Amazon Linux 2 uses ec2-user.
STEP 13 — Destroy Everything
When done:
terraform destroy
Type yes.
Top comments (0)