This project demonstrates how to provision a fully configured AWS EC2 instance using Terraform.
- EC2 Instance (t3.micro)
- Security Group (SSH & HTTP access)
- Key Pair (SSH login)
- EBS Storage (10GB)
- Nginx Web Server (auto installed)
- AWS Account
- AWS CLI configured (
aws configure) - Terraform installed
- Git installed
provider "aws" {
region = "ap-south-1"
}
resource "aws_key_pair" "deployer" {
key_name = "terraform-key"
public_key = file("my-key.pub")
}
resource "aws_security_group" "ec2_sg" {
name = "ec2-security-group"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
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"]
}
}
resource "aws_instance" "my_ec2" {
ami = "ami-0e12ffc2dd465f6e4"
instance_type = "t3.micro"
key_name = aws_key_pair.deployer.key_name
security_groups = [aws_security_group.ec2_sg.name]
root_block_device {
volume_size = 10
volume_type = "gp2"
}
user_data = <<-EOF
#!/bin/bash
yum update -y
yum install nginx -y
systemctl start nginx
systemctl enable nginx
EOF
tags = {
Name = "Terraform-Full-EC2"
}
}1. Initialize Terraform
terraform init2. Check Plan
terraform plan3. Apply Configuration
terraform applyType yes when prompted.
ssh -i my-key ec2-user@<your-public-ip>Open in browser: http://<your-public-ip>
Nginx page will be visible.
terraform destroyterraform-ec2/
├── main.tf
├── .gitignore
├── screenshots/
├── my-key.pub
└── README.md
- Private key (
my-key) is not uploaded .gitignoreprotects sensitive files
- Terraform basics
- AWS EC2 provisioning
- Security groups
- Infrastructure as Code
- Automation using
user_data
Ayush Nath Motichur








