Skip to content

Latest commit

 

History

History
561 lines (428 loc) · 12.1 KB

File metadata and controls

561 lines (428 loc) · 12.1 KB

🚀 Fullstack Node.js ECS Fargate Deployment Guide

Prerequisites:

  • You must have AWS CLI configured (aws configure).
  • Docker must be installed and running.
  • ✅ Docker on Amazon Linux 2023 (AL2023)

1. Install Docker

Amazon Linux 2023 uses dnf instead of yum. Run the following commands:

sudo dnf update -y
sudo dnf install -y docker

2. Start and Enable Docker Service

Start the Docker service:

sudo systemctl start docker

Enable it to start on boot:

sudo systemctl enable docker

3. Add EC2 User to Docker Group

Allow the ec2-user to run Docker commands without sudo:

sudo usermod -aG docker ec2-user

Then either:

  • Reboot:

    sudo reboot
  • Or log out and log back in for the group change to take effect.

4. Verify Docker is Working

Check Docker info:

docker info

You should see both Client and Server info without errors.


🔧 If Docker Daemon is Not Running

Start it manually:

sudo systemctl start docker

Check its status:

sudo systemctl status docker

If it says active (running), everything is working correctly.


✅ Optional: Enable Docker on Boot

To ensure Docker starts automatically after a reboot:

sudo systemctl enable docker
  • Your IAM user/role must have permission to use ECR (like AmazonEC2ContainerRegistryFullAccess or similar).

Step 1: Grant Permissions to Terraform

  1. Navigate to IAM (Identity and Access Management) in your AWS Console.
  2. Go to Users → Click Create User.
  3. Set User Name as ecs-docker.
  4. Click Next → Select Set PermissionsPermission Options.
  5. Choose Attach Policies Directly → Search and select AmazonEC2ContainerRegistryFullAccess.
  6. Click Next → Click Create User.
  7. Open the ecs-docker user profile.
  8. Go to Security CredentialsAccess KeyCreate Access Key.
  9. Choose Use Case → Select CLI.
  10. Confirm: "I understand the recommendation and want to proceed".
  11. Click NextCreate Access Key.
  12. Download the .csv file containing access credentials.

Step 2: Push Code to GitHub using VS Code

  1. 🖥️ Open VS Code Terminal:

    aws configure
  2. Enter your credentials:

    aws_access_key_id     = YOUR_ACCESS_KEY
    aws_secret_access_key = YOUR_SECRET_KEY
    region                = us-east-1
    output                = table
    
  3. Verify the configuration:

    aws configure list
    aws sts get-caller-identity
  4. Clone the project repository:

    cd ~/Downloads
    mkdir Fullstack-nodejs-ecs-fargate-deployment
    cd Fullstack-nodejs-ecs-fargate-deployment
    git clone https://github.com/arumullayaswanth/Fullstack-nodejs-ecs-fargate-deployment.git
    cd Fullstack-nodejs-ecs-fargate-deployment
    ls

Step 3: Build Docker Images

Update Frontend API Endpoint

Go to Fullstack-nodejs-ecs-fargate-deployment/client/src/pages /config.js and update your API base URL:

// const API_BASE_URL = "http://localhost:8800";
const API_BASE_URL = "http://aluru.site";
export default API_BASE_URL;
// const API_BASE_URL = process.env.REACT_APP_API_BASE_URL || "http://backend";
// export default API_BASE_URL;
// const API_BASE_URL = "REACT_APP_API_BASE_URL_PLACEHOLDER";

🔍 AWS Console Steps to View ECR Push Commands

  1. Go to the AWS Management Console
  2. In the search bar at the top, type: Elastic Container Registry
  3. Click on Elastic Container Registry
  4. Select Private registry
  5. Click on Private repositories
  6. Click Create repository
  7. Under General settings, enter the repository name:
    backend
    
  8. Click Create repository
  9. After creation, open the backend repository
  10. Click View push commands

You will now see the Docker commands required to authenticate, build, tag, and push your image.

🛠️ Backend Image

  1. Navigate to the backend directory:

    cd backend/
  2. Authenticate Docker with ECR:

    aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 421954350274.dkr.ecr.us-east-1.amazonaws.com
  3. Build the Docker image:

    docker build -t backend .
  4. Tag the image:

    docker tag backend:latest 421954350274.dkr.ecr.us-east-1.amazonaws.com/backend:latest
  5. Push the image:

    docker push 421954350274.dkr.ecr.us-east-1.amazonaws.com/backend:latest

🖼️ Frontend Image

🔍 AWS Console Steps to View ECR Push Commands

  1. Go to the AWS Management Console
  2. In the search bar at the top, type: Elastic Container Registry
  3. Click on Elastic Container Registry
  4. Select Private registry
  5. Click on Private repositories
  6. Click Create repository
  7. Under General settings, enter the repository name:
    frontend
    
  8. Click Create repository
  9. After creation, open the frontend repository
  10. Click View push commands

You will now see the Docker commands required to authenticate, build, tag, and push your image.

  1. Navigate to the frontend directory:

    cd ../client/
  2. Authenticate Docker with ECR:

    aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 421954350274.dkr.ecr.us-east-1.amazonaws.com
  3. Build the Docker image:

    docker build -t frontend .
  4. Tag the image:

    docker tag frontend:latest 421954350274.dkr.ecr.us-east-1.amazonaws.com/frontend:latest
  5. Push the image:

    docker push 421954350274.dkr.ecr.us-east-1.amazonaws.com/frontend:latest

Step 4: Verify Image URLs

  1. Go to AWS Console → Amazon Elastic Container Registry.
  2. Select backend repository → Copy the image URI.
  3. Select frontend repository → Copy the image URI.

Step 5: Update Terraform Task Definitions

Backend Task Definition

  1. Go to VS Code → Open terraform-ecs-fargate-fullstack-app → Open backend-task-server.tf

  2. Update your image in the container_definitions block:

    container_definitions = jsonencode([
      {
        name      = "backend"
        image     = "421954350274.dkr.ecr.us-east-1.amazonaws.com/backend:latest"  # Replace with your backend image
        cpu       = 256
        memory    = 512
        essential = true
      }
    ])

Frontend Task Definition

  1. Go to VS Code → Open terraform-ecs-fargate-fullstack-app → Open frontend-task-server.tf

  2. Update your image in the container_definitions block:

    container_definitions = jsonencode([
      {
        name      = "frontend"
        image     = "421954350274.dkr.ecr.us-east-1.amazonaws.com/frontend:latest"  # Replace with your frontend image
        cpu       = 256
        memory    = 512
        essential = true
      }
    ])

✅ Now your ECS Task Definitions are configured to use the latest images from Amazon ECR!

Fullstack Node.js ECS Fargate Deployment – Step-by-Step Guide

🏗️ VPC in Terraform

  1. Open VS Code Terminal and run:
ll
cd terraform-ecs-fargate-fullstack-app/
ls
cd vpa-network
ls
terraform init
terraform validate
terraform plan
terraform apply --auto-approve
terraform show
terraform state list
terraform apply --auto-approve
terraform show
terraform state list

🛢️ RDS in Terraform

  1. Navigate to RDS Directory:
cd ..
ls
cd rds
terraform init
terraform validate
terraform plan
terraform apply --auto-approve
terraform show
terraform state list
terraform apply --auto-approve
terraform show
terraform state list

🖥️ Create EC2 to Connect Private RDS

my challenge is my database created a new database now I am going to create insider database some existing records.

whenever i access a frontend and existing records you can see first then you can add your record in this case database inside you need to run some script to create existing data but the database if you want to connect extremal by using workbench and my database is private here.

in this case same network i am going to create one ec2 instance to connect rds and insert data

  1. Launch EC2 Instance:

    • Go to EC2 → Launch Instance
    • Name: bastion
    • AMI: Amazon Linux
    • Keypair: Your selected key
    • VPC: ecs-vpc
    • Subnet: ecs-public1
    • Security Group: terraform-98765432
    • Launch
  2. Connect and Configure EC2:

sudo -i
sudo yum install git -y
sudo yum install docker -y
sudo usermod -aG docker ec2-user

newgrp docker
sudo systemctl start docker
sudo systemctl status docker
sudo systemctl enable docker

sudo chmod 777 /var/run/docker.sock
yum install mariadb105-server -y
sudo systemctl start mariadb
sudo systemctl enable mariadb



git clone https://github.com/arumullayaswanth/Fullstack-nodejs-ecs-fargate-deployment.git
cd backend/
mysql -h <your-rds-endpoint> -u admin -p < test.sql

(eg: mysql -h book-rds.c0n8k0a0swtz.us-east-1.rds.amazonaws.com -u admin -Yaswanth123reddy < test.sql)
  • SHOW DATABASES;
  • USE test;
  • SHOW TABLES;
  • EXIT;

🔄 Update ECS Task Definition with RDS Endpoint

  1. Backend Task Update (backend-task-server.tf):

Come back to VS code then change the ecs-task directory. And add rds endpoint near the db

environment = [
        { name = "DB_HOST", value = "book-rds.c0n8k0a0swtz.us-east-1.rds.amazonaws.com" },   // replace your databasw end point
        { name = "PORT", value = "3306" },
        { name = "DB_USERNAME", value = "admin" },
        { name = "DB_PASSWORD", value = "Yaswanth123reddy" }
      ]


🧱 ECS Task Terraform Deployment

cd ..
ls
cd ecs-task
terraform init
terraform validate
terraform plan
terraform apply --auto-approve
terraform show
terraform state list
terraform apply --auto-approve
terraform show
terraform state list

🌐 Configure Route 53 Hosted Zone

  1. Create Hosted Zone:

    • Domain: aluru.site
    • Type: Public Hosted Zone
  2. Update Hostinger Nameservers:

    • Paste the 4 NS records from Route 53:
      • ns-865.awsdns-84.net
      • ns-1995.awsdns-97.co.uk
      • ns-1418.awsdns-59.org
      • ns-265.awsdns-73.com
  3. Create A Record in Route 53:

    • Type: A - IPv4 address
    • Alias: Yes
    • Alias target: Choose Application and Classic Load Balancer
    • Region: US East (N. Virginia)
    • Alias target value: dualstack.backend-1016048026.us-east-1.elb.amazonaws.com (replace you backend load balances)
    • Click Create record

🔐 Request HTTPS Certificate with ACM

Path: AWS Certificate Manager → Request Certificat

  1. Select: Request a public certificate
  2. Click Next
  3. Fully qualified domain name: aluru.site
  4. Validation method: DNS validation (recommended)
  5. Click Request

*** Step 7: Validate Domain in Route 53***

Path: AWS Certificate Manager → Certificates → Your Certificate ID

  1. Under domain, click Create DNS record in Amazon Route 53
  2. Select your hosted zone: aluru.site
  3. Click Create record
  4. Wait a few minutes for validation to complete

➕ Add HTTPS Listener to Load Balancer

  1. Go to EC2 → Load Balancers → frontend-alb → Listeners.
  2. Add:
    • Protocol: HTTPS
    • Port: 443
    • Action: Forward to web target group
    • Security policy: ELBSecurityPolicy-2021-06 (or latest)
    • Select ACM Certificate : Select the one for aluru.site
    • Click Add

🌍 Access Your Application

go to Google and search this website http://aluru.site and access the application now

Visit: http://aluru.site


💣 Destroy All Terraform Resources

terraform destroy -auto-approve