Automated provisioning of a production-ready EC2 web server on AWS using Terraform — with networking, security, and storage fully configured as code.
- Overview
- Architecture
- Resources Provisioned
- Prerequisites
- Project Structure
- Getting Started
- Configuration Reference
- Security Notes
- Contributing
This project uses Terraform to provision a complete, ready-to-use EC2 web server on AWS us-west-2 (Oregon). Everything is defined as Infrastructure as Code (IaC) — no manual console clicks required.
| Property | Value |
|---|---|
| ☁️ Cloud Provider | Amazon Web Services (AWS) |
| 🌍 Region | us-west-2 (Oregon) |
| 🖥️ Instance Type | t3.micro |
| 💾 OS / AMI | Ubuntu (ami-0d13e2317a7e75c95) |
| 💽 Storage | 8 GB gp3 EBS Volume |
| 🔐 Access | SSH (Port 22) + HTTP (Port 80) |
┌─────────────────────────────────────────────────────┐
│ AWS Cloud (us-west-2) │
│ │
│ ┌──────────────────────────────────────────────┐ │
│ │ Default VPC │ │
│ │ │ │
│ │ ┌─────────────────────────────────────┐ │ │
│ │ │ Security Group │ │ │
│ │ │ ✅ Inbound → Port 22 (SSH) │ │ │
│ │ │ ✅ Inbound → Port 80 (HTTP) │ │ │
│ │ │ ✅ Outbound → All Traffic │ │ │
│ │ │ │ │ │
│ │ │ ┌─────────────────────────────┐ │ │ │
│ │ │ │ EC2 Instance │ │ │ │
│ │ │ │ t3.micro | Ubuntu │ │ │ │
│ │ │ │ 8 GB gp3 EBS Volume │ │ │ │
│ │ │ │ Key Pair: deployer-key │ │ │ │
│ │ │ └─────────────────────────────┘ │ │ │
│ │ └─────────────────────────────────────┘ │ │
│ └──────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────┘
▲ ▲
SSH (Port 22) HTTP (Port 80)
Developer Browser
| # | Resource | Name | Purpose |
|---|---|---|---|
| 1 | aws_key_pair |
deployer-key |
SSH key for secure instance access |
| 2 | aws_default_vpc |
Default VPC |
Networking foundation |
| 3 | aws_security_group |
web-security-group |
Firewall rules (SSH + HTTP) |
| 4 | aws_instance |
Terraform-Web-Server |
The EC2 web server instance |
Before you begin, make sure you have the following installed and configured:
- Terraform
>= 1.0 - AWS CLI configured with valid credentials
- An AWS account with EC2 permissions
- An SSH key pair (public key ready to paste)
# Verify Terraform installation
terraform --version
# Verify AWS CLI is configured
aws sts get-caller-identityterraform-aws-ec2/
│
├── main.tf # Core infrastructure (VPC, SG, EC2, Key Pair)
├── README.md # You are here 📍
└── .gitignore # Ignore .tfstate and sensitive files
💡 Tip: For larger projects, consider splitting into
variables.tf,outputs.tf, andproviders.tf.
Follow these steps to deploy the infrastructure:
git clone https://github.com/Muhammadgi/terraform-aws-ec2
cd terraform-aws-ec2Open main.tf and replace the placeholder public key with your own:
resource "aws_key_pair" "deployer" {
key_name = "deployer-key"
public_key = "ssh-ed25519 YOUR_ACTUAL_PUBLIC_KEY_HERE"
}🔑 Generate a new key pair with:
ssh-keygen -t ed25519 -C "your-email@example.com"
terraform initterraform planterraform applyType yes when prompted to confirm.
Once provisioned, connect via SSH:
ssh -i ~/.ssh/your-private-key ubuntu@<EC2_PUBLIC_IP>📌 Find your EC2 public IP in the AWS Console or add an
outputblock tomain.tf.
terraform destroy| Parameter | Value | Description |
|---|---|---|
region |
us-west-2 |
AWS region |
ami |
ami-0d13e2317a7e75c95 |
Ubuntu AMI (us-west-2) |
instance_type |
t3.micro |
Compute size (~$0.0104/hr) |
volume_size |
8 GB |
Root EBS volume |
volume_type |
gp3 |
General Purpose SSD (latest gen) |
ssh_port |
22 |
Remote access |
http_port |
80 |
Web traffic |
⚠️ This configuration opens SSH and HTTP to0.0.0.0/0(all IPs). This is fine for testing, but for production environments, consider the following:
- 🔒 Restrict SSH to your specific IP:
cidr_blocks = ["YOUR_IP/32"] - 🔒 Add HTTPS (Port 443) with an SSL certificate (AWS ACM + ALB)
- 🔒 Use a private subnet with a bastion host for production workloads
- 🔒 Never commit real private keys or AWS credentials to Git
- 🔒 Add a
.gitignoreto exclude*.tfstate,*.tfstate.backup, and.terraform/
Recommended .gitignore:
.terraform/
*.tfstate
*.tfstate.backup
*.tfvars
.terraform.lock.hcl
Contributions are welcome! Feel free to:
- Fork the repository
- Create a feature branch (
git checkout -b feature/my-improvement) - Commit your changes (
git commit -m 'Add some improvement') - Push to the branch (
git push origin feature/my-improvement) - Open a Pull Request
Made with ❤️ using Terraform & AWS
⭐ Star this repo if you found it helpful! ⭐