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.
- 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
Ensure the following tools are installed on your system:
- Node.js
- Terraform CLI (v1.2.0+)
- AWS CLI
- AWS Account with appropriate IAM permissions
git clone https://github.com/padmashri23/CI-CD_Integration_with_Crypto_Price_Tracking_app.git
- Navigate to the AWS Console > IAM > Users > Create user
- Enter user name (e.g.,
ec2-terraform
) and proceed - Attach the following policies:
AmazonEC2FullAccess
AdministratorAccess
- Complete creation and generate access keys
- Choose "CLI" as the usage type and download the CSV credentials file
- 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
- 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/dist/* /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.
- 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! π
terraform destroy
π Make sure to install AWS CLI and Terraform CLI before beginning your setup.