Internal Load Balancing and Auto Scaling in AWS

1. App Tier AMI

  1. Navigate to Instances in the EC2 dashboard.
  2. Select the app tier instance you created.
  3. Under Actions, select Image and TemplatesCreate Image.
  4. Provide a name and description, then click Create Image.
  5. Monitor the image creation progress under AMIs in the left-hand navigation panel.

2. Create a Target Group

  1. Navigate to Target Groups under Load Balancing in the EC2 dashboard.
  2. Click Create Target Group.
  3. Set the target type to Instances.
  4. Provide a name.
  5. Set protocol to HTTP and port to 4000 (Node.js app port).
  6. Select the appropriate VPC.
  7. Change the health check path to /health.
  8. Click Next, skip registering targets, and create the target group.

3. Create an Internal Load Balancer

  1. Navigate to Load Balancers under Load Balancing in the EC2 dashboard.
  2. Click Create Load Balancer.
  3. Select Application Load Balancer and click Create.
  4. Provide a name and select internal.
  5. Configure the VPC and private subnets.
  6. Select the security group created for the internal ALB.
  7. Set listener to HTTP (port 80).
  8. Select the previously created target group.
  9. Click Create Load Balancer.

4. Create a Launch Template

  1. Navigate to Launch Templates under Instances in the EC2 dashboard.
  2. Click Create Launch Template.
  3. Provide a name.
  4. Select the app tier AMI created earlier.
  5. Choose Instance Type as t2.micro.
  6. Skip Key Pair and Network Settings.
  7. Assign the correct security group for the app tier.
  8. Under Advanced Details, set the same IAM instance profile as used in other EC2 instances.
  9. Click Create Launch Template.

5. Configure Auto Scaling

  1. Navigate to Auto Scaling Groups under Auto Scaling in the EC2 dashboard.
  2. Click Create Auto Scaling Group.
  3. Provide a name.
  4. Select the Launch Template created earlier and click Next.
  5. Configure VPC and private instance subnets.
  6. Attach the Auto Scaling Group to the previously created Load Balancer.
  7. Set desired, minimum, and maximum capacity to 2.
  8. Click Skip, review settings, and click Create Auto Scaling Group.

6. Update Configuration File

  1. Open application-code/nginx.conf.
  2. Replace [INTERNAL-LOADBALANCER-DNS] with your internal load balancer’s DNS.
  3. Upload nginx.conf and application-code/web-tier folder to the S3 bucket.

7. Web Instance Deployment

  1. Navigate to Instances in the EC2 dashboard.
  2. Click Launch Instances.
  3. Select Amazon Linux 2 AMI.
  4. Choose t2.micro instance type.
  5. Skip the key pair.
  6. Configure Network, Subnet, and IAM Role (select a private subnet for the web layer).
  7. Select the web tier security group.
  8. Choose the correct IAM role for web-tier access.
  9. Click Launch Instance.

8. Connect to the Instance

  1. Go to Instances in the EC2 dashboard.
  2. Select the running instance and click Connect.
  3. Choose the Session Manager tab and click Connect.
  4. Test connectivity with:
sudo -su ec2-user
   ping 8.8.8.8

9. Configure Web Instance

Install NVM and Node.js

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
source ~/.bashrc
nvm install 16
nvm use 16

Download Web Tier Code from S3

cd ~/
aws s3 cp s3://BUCKET_NAME/web-tier/ web-tier --recursive

Build React App

cd ~/web-tier
npm install
npm run build

10. Install and Configure NGINX

Install NGINX

sudo amazon-linux-extras install nginx1 -y

Replace Nginx Configuration

cd /etc/nginx
sudo rm nginx.conf
sudo aws s3 cp s3://BUCKET_NAME/nginx.conf .

Restart and Configure NGINX

sudo service nginx restart
chmod -R 755 /home/ec2-user
sudo chkconfig nginx on

This guide provides a structured and detailed approach to setting up internal load balancing and auto-scaling in AWS, ensuring a smooth and scalable deployment.

PART-6