This project demonstrates how to use Terraform to define, provision, and manage AWS infrastructure using Infrastructure as Code (IaC). It covers core Terraform functionalities such as initializing, applying, and managing state files, as well as deploying an Ubuntu EC2 instance with networking configurations.
- Terraform: Automates AWS infrastructure provisioning.
- Infrastructure as Code (IaC): Manages cloud resources via code.
- State Management: Tracks deployed resources.
- EC2 Deployment: Launches an AWS instance with Terraform.
- Networking: Configures VPC, subnets, route tables, and security groups.
🔧 Prerequisites
- Install Terraform:
brew install terraform(Mac) orchoco install terraform(Windows). - Ensure you have an AWS account and AWS CLI configured:
aws configure - Set up an SSH key pair in AWS to access EC2 instances.
🚀 Essential Terraform Commands
| Action | Command | Description |
|---|---|---|
| Initialize Terraform | terraform init |
Initializes Terraform in the working directory. |
| Preview Changes | terraform plan |
Shows the changes Terraform will apply before execution. |
| Apply Changes | terraform apply |
Creates or updates AWS infrastructure as defined in the code. |
| Destroy Infrastructure | terraform destroy |
Deletes all Terraform-managed resources. |
| List Tracked Resources | terraform state list |
Displays all resources tracked in the Terraform state file. |
| Show Details of a Resource | terraform state show aws_instance.web-server |
Shows the current state and attributes of a specific resource. |
| Rename/Move a Resource | terraform state mv aws_instance.old-name aws_instance.new-name |
Renames or moves a resource within the Terraform state. |
| Remove a Resource Without Deleting It | terraform state rm aws_s3_bucket.my_bucket |
Stops tracking a resource in Terraform without destroying it. |
Terraform Output Variables
Retrieve EC2 Public IP
Add the following to main.tf to output the EC2 public IP:
output "instance_public_ip" {
description = "Public IP of the EC2 instance"
value = aws_instance.web-server.public_ip
}
Run this command to display the value:
terraform output instance_public_ip
resource "aws_instance" "web-server-instance" {
ami = "ami-04b4f1a9cf54c11d0"
instance_type = "t2.micro"
availability_zone = "us-east-1a"
key_name = "terraform-main-key"
network_interface {
device_index = 0
network_interface_id = aws_network_interface.web-server-nic.id
}
user_data = <<-EOF
#!/bin/bash
sudo apt update -y
sudo apt install apache2 -y
sudo systemctl start apache2
sudo bash -c 'echo "Your very first web server" > /var/www/html/index.html'
EOF
tags = {
Name = "web-server"
}
}
It is recommended to use the same Availability Zone (AZ) as the subnet.
- VPC: Defines a private network in AWS.
- Subnet: Divides a VPC into isolated networks.
- Internet Gateway: Allows outbound internet traffic from the VPC.
- Route Table: Controls traffic routing within the VPC.
- Security Groups: Control inbound/outbound traffic to instances.
ssh -i "your-key.pem" ubuntu@
- 1️⃣ Download and install PuTTY.
- 2️⃣ Open PuTTYgen → Load your
.pemkey → Save as.ppk. - 3️⃣ Open PuTTY → Enter Hostname:
ubuntu@34.232.56.18. - 4️⃣ Under SSH → Auth → Select your
.ppkkey. - 5️⃣ Click **Open** to connect.
Inspired by this tutorial: https://www.youtube.com/watch?v=SLB_c_ayRMo

