This Terraform project provisions a complete AWS infrastructure setup including a VPC, subnet, security group, and EC2 instance for hosting a web server.
This project creates a basic AWS infrastructure stack that includes:
- A Virtual Private Cloud (VPC)
- A public subnet in a specific availability zone
- A security group configured for web traffic
- An EC2 instance to host a web server
Before using this project, ensure you have the following installed and configured:
- Terraform (version 1.0 or later)
- AWS CLI configured with appropriate credentials
- AWS Account with necessary permissions to create VPC, EC2, and security group resources
- SSH Key Pair named
aws-keyin theus-east-1region
Your AWS credentials must have permissions to:
- Create and manage VPCs
- Create and manage subnets
- Create and manage security groups
- Launch EC2 instances
- Create and manage tags
Terraform_Project/
├── main.tf # Main infrastructure resources
├── provider.tf # AWS provider configuration
├── outputs.tf # Output values
└── README.md # Project documentation
- CIDR Block:
10.0.0.0/16 - Name:
main_vpc - Provides an isolated network environment for your AWS resources
- CIDR Block:
10.0.1.0/24 - Availability Zone:
us-east-1a - Name:
main_subnet - Subnet within the VPC for hosting EC2 instances
- Name:
web_sg - Description: Web security group
- Inbound Rules:
- SSH (port 22) from anywhere (
0.0.0.0/0) - HTTP (port 80) from anywhere (
0.0.0.0/0)
- SSH (port 22) from anywhere (
- Outbound Rules:
- All traffic allowed to anywhere
- Name:
web_sg
- Instance Type:
t3.micro - AMI:
ami-0cae6d6fe6048ca2c(Amazon Linux 2) - Key Pair:
aws-key(must exist in AWS) - Name:
web_server - Deployed in the subnet with the web security group attached
The project uses the AWS provider version ~> 5.0 and is configured for the us-east-1 region. You can modify the region in provider.tf if needed.
- Region:
us-east-1(configurable inprovider.tf) - AMI:
ami-0cae6d6fe6048ca2c(Amazon Linux 2 in us-east-1) - Key Pair:
aws-key(must be created in AWS Console or via AWS CLI) - Instance Type:
t3.micro(eligible for AWS Free Tier)
-
Clone or navigate to the project directory:
cd Terraform_Project -
Initialize Terraform:
terraform init
-
Review the execution plan:
terraform plan
-
Apply the configuration:
terraform apply
Type
yeswhen prompted to confirm the deployment.
After deployment, Terraform will output the public IP address of the EC2 instance. You can:
-
SSH into the instance:
ssh -i /path/to/aws-key.pem ec2-user@<public_ip>
-
Access via HTTP: Open
http://<public_ip>in your web browser (after configuring a web server on the instance)
The project outputs the following information:
web_server_public_ip: The public IP address of the EC2 instance
To view outputs after deployment:
terraform output0.0.0.0/0). For production environments, consider:
- Restricting SSH access to specific IP addresses
- Using HTTPS instead of HTTP
- Implementing additional security layers
The EC2 instance requires an SSH key pair named aws-key in the us-east-1 region. If you don't have this key pair:
-
Create via AWS Console:
- Navigate to EC2 → Key Pairs
- Create a new key pair named
aws-key - Download the private key file
-
Create via AWS CLI:
aws ec2 create-key-pair --key-name aws-key --region us-east-1 --query 'KeyMaterial' --output text > aws-key.pem chmod 400 aws-key.pem
The AMI ID ami-0cae6d6fe6048ca2c is specific to the us-east-1 region. If you change the region, you'll need to update the AMI ID to match the target region.
To tear down all resources created by this project:
terraform destroyType yes when prompted to confirm the destruction.
-
Key Pair Not Found:
- Ensure the key pair
aws-keyexists in theus-east-1region - Verify the key pair name matches exactly
- Ensure the key pair
-
AMI Not Found:
- The AMI ID may be outdated or region-specific
- Find the correct AMI ID for your region using:
aws ec2 describe-images --owners amazon --filters "Name=name,Values=amzn2-ami-hvm-*-x86_64-gp2" --query 'Images[*].[ImageId,Name]' --output table
-
Insufficient Permissions:
- Verify your AWS credentials have the necessary IAM permissions
- Check AWS CloudTrail for permission denied errors
-
Region Mismatch:
- Ensure the provider region matches where you want to deploy resources
- Verify the AMI exists in the target region
The resources created by this project are eligible for the AWS Free Tier (for new AWS accounts):
- t3.micro instance: Free for 750 hours/month (first 12 months)
- VPC and Subnet: Free
- Security Group: Free
- Data Transfer: Charges may apply after free tier limits
For existing accounts or extended usage, approximate costs:
- t3.micro:
$0.0104/hour ($7.50/month if running 24/7) - Data Transfer: Varies by usage
This is a personal project, but suggestions and improvements are welcome.
This project is provided as-is for educational and development purposes.