This project focuses on automated deployment of a website using Terraform, AWS EC2, Nginx, and GitHub. The goal is to provision cloud infrastructure efficiently using Infrastructure as Code (IaC) and ensure continuous deployment through GitHub integration. This setup enables seamless website updates whenever changes are pushed to the GitHub repository.
Objective The primary objective of this project is to automate and streamline the process of deploying and maintaining a web server on AWS, making it efficient, scalable, and easy to manage. By utilizing Terraform, we eliminate manual configurations and ensure that infrastructure is replicable, version-controlled, and easily modifiable.
Key Components Terraform: Used to define and provision AWS resources such as EC2 instances and security groups. AWS EC2: Virtual server hosting the website. Nginx: Web server used to serve static content. GitHub: Centralized repository for website source code. Cron Jobs: Automates periodic updates by pulling the latest changes from GitHub.
Project Breakdown
1οΈβ£ Infrastructure as Code with Terraform The project leverages Terraform to provision an AWS EC2 instance and configure the necessary security groups. Using Terraformβs declarative syntax, we define infrastructure in code, making it easy to deploy, modify, and destroy AWS resources efficiently.
2οΈβ£ Web Server Setup with Nginx Upon launching the EC2 instance, a user data script automatically installs and starts Nginx, setting it up to serve a static website. Nginx is configured to host files from the /usr/share/nginx/html directory.
3οΈβ£ Automated Deployment from GitHub To ensure continuous deployment, the EC2 instance clones the website files from a GitHub repository upon initialization. Additionally, a cron job is set up to periodically check for updates, ensuring that any changes pushed to the GitHub repository are automatically pulled and reflected on the live website.
4οΈβ£ Security & Access Control The AWS security group configuration allows:
SSH (port 22): Restricted access to secure remote connections. HTTP (port 80): Open access for serving website traffic. 5οΈβ£ Continuous Integration & Deployment (CI/CD) By using cron jobs, the EC2 instance periodically runs:
bash Copy Edit cd /usr/share/nginx/html && git reset --hard origin/main && git pull origin main && sudo systemctl restart nginx This ensures that the website is always up-to-date with the latest changes from GitHub without manual intervention.
Use Cases & Benefits Automated Website Deployment: No manual intervention needed after setup. Scalability: Easily replicate infrastructure across multiple instances or environments. Version Control & Backup: All changes are tracked via GitHub, ensuring rollback capabilities. Infrastructure Consistency: Eliminates configuration drift with IaC principles. Cost Efficiency: Uses AWS Free Tier resources with minimal operational cost. Conclusion This project demonstrates modern DevOps best practices by automating website deployment using Terraform, AWS, and GitHub. By integrating Infrastructure as Code, automation, and version control, it ensures reliability, efficiency, and maintainability.
This approach is ideal for startups, developers, and businesses looking for a cost-effective and scalable web hosting solution while embracing cloud automation. π
- Infrastructure as Code (IaC) using Terraform
- Automated deployment of an AWS EC2 instance
- Nginx web server setup to serve static files
- GitHub integration to fetch website files automatically
- Cron job for continuous deployment (auto-updates on every GitHub push)
Ensure you have the following installed:
- Log in to AWS Management Console
- Navigate to EC2 Dashboard β Launch Instance
- Choose Amazon Linux 2 AMI
- Select t2.micro instance type (free tier eligible)
- Configure security group to allow SSH (22), HTTP (80)
- Create or select a key pair (download the
.pemfile) - Click Launch Instance
ssh -i "your-key.pem" ec2-user@<EC2-PUBLIC-IP>sudo yum update -y
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
sudo yum install -y terraform
terraform -v # Verify installationsudo yum install -y nginx git
sudo systemctl start nginx
sudo systemctl enable nginxcrontab -eAdd the following line:
*/5 * * * * cd /usr/share/nginx/html && git reset --hard origin/main && git pull origin main && sudo systemctl restart nginx
This ensures your website auto-updates every 5 minutes when you push changes to GitHub.
git initmkdir website && cd website
nano index.html # Add HTML content
nano styles.css # Add CSS styles
nano script.js # Add JavaScript functionalitygit add .
git commit -m "Initial commit for website"
git branch -M main
git remote add origin https://github.com/YOUR_GITHUB_USERNAME/YOUR_REPO_NAME.git
git push -u origin mainCreate a Terraform configuration file (main.tf) with the following:
provider "aws" {
region = "us-east-1"
}
resource "aws_security_group" "web_sg" {
name = "web-server-sg"
description = "Allow SSH and HTTP traffic"
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" "web_server" {
ami = "ami-0241b1d769b029352"
instance_type = "t2.micro"
key_name = "terraform-key"
vpc_security_group_ids = [aws_security_group.web_sg.id]
user_data = <<-EOF
#!/bin/bash
sudo yum update -y
sudo yum install -y nginx git
sudo systemctl start nginx
sudo systemctl enable nginx
sudo rm -rf /usr/share/nginx/html/*
sudo git clone https://github.com/YOUR_GITHUB_USERNAME/YOUR_REPO_NAME.git /usr/share/nginx/html
sudo chmod -R 755 /usr/share/nginx/html
sudo systemctl restart nginx
EOF
tags = {
Name = "DevOps-EC2-instance"
}
}
output "ec2_public_ip" {
description = "The public IP address of the EC2 instance"
value = aws_instance.web_server.public_ip
}Run the following Terraform commands:
terraform init
terraform apply -auto-approveOnce completed, Terraform will output the EC2 Public IP. Access your website at:
http://<EC2-PUBLIC-IP>
After deploying, verify that:
- Your website is accessible at:
http://<EC2-PUBLIC-IP> - Cron jobs are running:
sudo grep CRON /var/log/cron - Nginx is active:
sudo systemctl status nginx
To delete all AWS resources created by Terraform:
terraform destroy -auto-approve##Screenshots
This setup ensures your website automatically deploys on AWS with Terraform & Nginx, while staying up-to-date with GitHub auto-sync using cron jobs.




