MultiCloud Classroom notes 23/mar/2026

AWS EC2 + NGINX + Auto Scaling Setup

1. Create Security Group

aws ec2 create-security-group \
  --group-name web-server \
  --description "open ports ssh,http & https" \
  --vpc-id vpc-063acb2d29664ae86

2. Allow Inbound Traffic

SSH (Port 22)

aws ec2 authorize-security-group-ingress \
  --group-id sg-05d9e5feb84460622 \
  --protocol tcp \
  --port 22 \
  --cidr 0.0.0.0/0

SSH, HTTP, HTTPS

aws ec2 authorize-security-group-ingress \
  --group-id sg-0d378b60329356027 \
  --ip-permissions \
  'IpProtocol=tcp,FromPort=22,ToPort=22,IpRanges=[{CidrIp=0.0.0.0/0}]' \
  'IpProtocol=tcp,FromPort=80,ToPort=80,IpRanges=[{CidrIp=0.0.0.0/0}]' \
  'IpProtocol=tcp,FromPort=443,ToPort=443,IpRanges=[{CidrIp=0.0.0.0/0}]'

3. Launch EC2 Instance

aws ec2 run-instances \
  --image-id ami-0e0416d387552f0b1 \
  --count 1 \
  --instance-type t3.micro \
  --key-name local \
  --security-group-ids sg-0d378b60329356027 \
  --subnet-id subnet-0544cf9a2c7effd59 \
  --user-data file://my_script.txt

4. User Data Script (Install NGINX)

#!/bin/bash
dnf update -y
dnf install nginx -y || yum install nginx -y
systemctl start nginx
systemctl enable nginx

5. Create AMI from Instance

aws ec2 create-image \
  --instance-id i-0e32b8211c7ae4cd0 \
  --name "nginx" \
  --description "An AMI for my server"

Output:

ImageId: ami-05216ca929d320b7e

6. Create Launch Template

Export Template Data

aws ec2 get-launch-template-data \
  --instance-id i-0e32b8211c7ae4cd0 \
  --query 'LaunchTemplateData' > nginx-template.json

Create Launch Template

aws ec2 create-launch-template \
  --launch-template-name nginx-web-server \
  --launch-template-data file://nginx-template.json

7. Test Launch Template

aws ec2 run-instances \
  --launch-template LaunchTemplateId=lt-0918af49894c338d8,Version=1

8. Create Auto Scaling Group (ASG)

aws autoscaling create-auto-scaling-group \
  --auto-scaling-group-name nginx-asg \
  --launch-template LaunchTemplateId=lt-003399204fac5b805,Version=1 \
  --min-size 1 \
  --max-size 3 \
  --desired-capacity 1 \
  --vpc-zone-identifier "subnet-0544cf9a2c7effd59"

9. Create Scaling Policy (CPU ≥ 60%)

Target Tracking Scaling Policy

aws autoscaling put-scaling-policy \
  --auto-scaling-group-name nginx-asg \
  --policy-name cpu-60-target \
  --policy-type TargetTrackingScaling \
  --target-tracking-configuration '{
    "PredefinedMetricSpecification": {
      "PredefinedMetricType": "ASGAverageCPUUtilization"
    },
    "TargetValue": 60.0
  }'

10. Verify ASG

aws autoscaling describe-auto-scaling-groups \
  --auto-scaling-group-names nginx-asg

Notes

  • Ensure IAM role has EC2 + AutoScaling permissions
  • Replace subnet, AMI, and SG IDs as needed
  • Open only required ports for production (avoid 0.0.0.0/0 for SSH)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Social Media Icons Powered by Acurax Web Design Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube