CI/CD Integration with Crypto Price Tracking App
π Automatically Apply Terraform with AWS EC2 Instance
π Why Terraform?
Terraform is a powerful Infrastructure as Code (IaC) tool that enables efficient, scalable, and automated infrastructure management. Here's why it stands out:
β Key Benefits of Terraform Over Manual Setup
βοΈ Infrastructure is repeatable & consistent
βοΈ Fast, automated deployment
βοΈ Easily scales with configurations
βοΈ Changes are tracked in Git
βοΈ Easier rollback in case of issues
βοΈ Teams can collaborate via code
Using Terraform with your CI/CD pipeline ensures stability, visibility, and productivityβespecially when deploying modern apps like React.js on AWS EC2.
π Deploying a React.js App on AWS EC2 Using Terraform
π Architecture Diagram
βοΈ Steps
- Clone React.js App from GitHub
- Create an IAM User
- Configure AWS Profile and Set Up the AWS CLI
- Set Up Terraform Configuration
- Deploy the Infrastructure with Terraform
- Clean Up Resources
π€Έ Quick Start
Prerequisites
Ensure the following tools are installed on your system:
- Node.js
- Terraform CLI (v1.2.0+)
- AWS CLI
- AWS Account with appropriate IAM permissions
πΌ Cloning the Repository
git clone https://github.com/padmashri23/CI-CD_Integration_with_Crypto_Price_Tracking_app.git
β Step 1 - Create an IAM User and Access Key
- Navigate to the AWS Console > IAM > Users > Create user
- Enter user name (e.g.,
ec2-terraform) and proceed - Attach the following policies:
AmazonEC2FullAccessAdministratorAccess
- Complete creation and generate access keys
- Choose "CLI" as the usage type and download the CSV credentials file
β Step 2 - Configure AWS CLI
- Open your IDE and create a directory:
mkdir aws-ec2-terraform && cd aws-ec2-terraform
- Run AWS configuration:
aws configure
-
Input your credentials and region when prompted
-
Verify configuration:
aws configure list
β Step 3 - Set Up Terraform Configuration
- Create a Terraform configuration file:
touch main.tf
- Add the following content to
main.tf:
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "react_app" {
ami = "ami-xxxxxxxxxxxx" # Update with valid Amazon Linux 2 AMI ID
instance_type = "t2.micro"
security_groups = [aws_security_group.react_sg.name]
user_data = <<-EOF
#!/bin/bash
sudo yum update -y
sudo amazon-linux-extras enable nginx1
sudo yum install -y nginx
sudo systemctl start nginx
sudo systemctl enable nginx
curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -
sudo yum install -y nodejs
sudo yum install -y git
git clone https://github.com/yourusername/your-react-app.git /home/ec2-user/react-app
cd /home/ec2-user/react-app
npm install && npm run build
sudo rm -rf /usr/share/nginx/html/*
sudo cp -r /home/ec2-user/react-app/build/* /usr/share/nginx/html/
sudo systemctl restart nginx
EOF
tags = {
Name = "ReactAppServer"
}
}
resource "aws_security_group" "react_sg" {
name = "react_app_sg"
description = "Allow HTTP and SSH access"
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"]
}
}
Note: Replace https://github.com/yourusername/your-react-app.git with your repository URL.
β Step 4 - Deploy Infrastructure
- Initialize Terraform:
terraform init
- Plan and Apply:
terraform apply -auto-approve
- Access the App:
Visit your EC2 public IP in a browser:
http://your-ec2-public-ip
You should see your React.js app live! π
π³οΈ Clean Up Resources
terraform destroy
π Final Results
App Running on:
π Reference Tutorial
π Make sure to install AWS CLI and Terraform CLI before beginning your setup.