This project automates the deployment of an AWS EC2 instance using Terraform and a User Data script that installs Java, Maven, clones a Spring Boot app from GitHub, builds it, and runs it automatically.
Objective:
Automate the entire workflow of provisioning infrastructure and deploying an application on AWS EC2 using Terraform.
Includes:
- Multi-environment setup (Dev & Prod)
- EC2 instance provisioning
- User data automation (Java + Maven installation)
- Application auto-deployment from GitHub
- Clean destroy with
terraform destroy
Before running this project, make sure you have:
- AWS Account (with access key and secret key)
- AWS CLI installed and configured
aws configure
Terraform installed
terraform -version git --version
π Project Structure Tech_eazy_devops/ β βββ main.tf # EC2 instance and provider configuration βββ variables.tf # Variables used in Terraform βββ outputs.tf # Output values (e.g., public IP) βββ user_data.sh # Script that runs on EC2 startup βββ dev_config.tfvars # Dev environment config βββ prod_config.tfvars # Prod environment config βββ README.md # Documentation
βοΈ Step-by-Step Deployment Guide 1οΈβ£ Clone the Repository git clone https://github.com//Tech_eazy_devops.git cd Tech_eazy_devops
2οΈβ£ Configure AWS Credentials Run: aws configure
Enter:
AWS Access Key ID
AWS Secret Access Key
Default region (e.g. ap-south-1)
Output format: json
3οΈβ£ Initialize Terraform terraform init
This downloads the AWS provider and sets up your local environment.
4οΈβ£ Select Environment For Dev: terraform plan -var-file="dev_config.tfvars" terraform apply -var-file="dev_config.tfvars" -auto-approve
For Prod: terraform plan -var-file="prod_config.tfvars" terraform apply -var-file="prod_config.tfvars" -auto-approve
5οΈβ£ Wait for EC2 Instance to Launch Terraform will print the Public IP after creation, e.g.: Outputs:
public_ip = "13.232.xxx.xxx"
6οΈβ£ Test the Application Once the instance is up, open your browser and visit: http://<PUBLIC_IP>:8080
You should see your Spring Boot application running π
7οΈβ£ (Optional) Connect via SSH ssh -i "terra-key-ec2.pem" ubuntu@<PUBLIC_IP>
You can check running processes: ps aux | grep java
8οΈβ£ Destroy the Infrastructure To avoid extra AWS charges: terraform destroy -var-file="dev_config.tfvars" -auto-approve
π§ Notes
Make sure your security group allows inbound rules for:
Port 22 (SSH)
Port 8080 (HTTP)
The user_data.sh script handles:
System update
Java & Maven installation
App clone + build + run
π user_data.sh Breakdown #!/bin/bash sudo apt update -y sudo apt install openjdk-21-jdk -y sudo apt install maven -y
cd /home/ubuntu git clone https://github.com/Trainings-TechEazy/test-repo-for-devops.git cd test-repo-for-devops
mvn clean package nohup java -jar target/techeazy-devops-0.0.1-SNAPSHOT.jar > app.log 2>&1 &
π¨βπ» Author Praveen (Tech Eazy DevOps) DevOps & Cloud Enthusiast | AWS | Terraform | CI/CD | Automation π GitHub Profile π LinkedIn
π End of Deployment Guide
This setup provides a fully automated EC2 deployment pipeline using Terraform β ideal for demonstrating real-world DevOps automation.
Would you like me to include a βmulti-environment folder structureβ version (like /env/dev/ and /env/prod/) inside the README for extra professionalism?
Thatβs often asked in real DevOps project submissions.