Simple Guide to Setting Up EC2 with Auto Scaling Groups (ASG) on AWS

Step 1: Create an EC2 Instance Before creating an Auto Scaling Group (ASG), we need an EC2 instance to use as a base for the Auto Scaling template. Go to the EC2 Dashboard: Navigate to the EC2 Dashboard in your AWS console. Launch an EC2 Instance: Click Launch Instance. Select an Amazon Machine Image (AMI): Choose an Amazon Linux 2 or any preferred AMI for your EC2 instance. Choose an Instance Type: Select t2.micro (Free Tier) or t3.micro for a test environment. Configure Instance: Leave the default settings unless you need specific configurations. Set the number of instances to 1. Add Storage: The default 8GB is enough for a simple test setup. Configure Security Group: Add rules for: HTTP (port 80) – for web access SSH (port 22) – for remote access (you can restrict to your IP for security) Review and Launch: Select or create a key pair to SSH into the instance. Launch the Instance. Step 2: Create a Launch Template A Launch Template defines the configuration for your EC2 instances. Go to EC2 Dashboard and click Launch Templates. Click “Create Launch Template”. Fill in the following fields: Name: Choose a name for the template (e.g., MyWebAppTemplate). AMI: Select the same AMI you used for the EC2 instance (e.g., Amazon Linux 2). Instance Type: Select the instance type (e.g., t2.micro). Key Pair: Select the key pair you created earlier. Security Group: Select the same security group you used for the EC2 instance. Click Create Launch Template. Step 3: Create an Auto Scaling Group (ASG) Now that we have the Launch Template, let’s create an Auto Scaling Group. Go to EC2 Dashboard > Auto Scaling Groups. Click “Create Auto Scaling group”. Configure the Auto Scaling Group: Name: Enter a name (e.g., MyWebAppASG). Launch Template: Choose the template you created earlier (e.g., MyWebAppTemplate). Version: Select the latest version. Desired Capacity: Set to 2 (this means 2 instances should be running). Minimum Capacity: Set to 2. Maximum Capacity: Set to 3. Configure Scaling Policies: Scaling Type: Choose Target Tracking Policy. Target Value: Set to 30% for CPU utilization. Review and Create the Auto Scaling Group. Step 4: Monitor EC2 Instances Scaling After setting up the ASG, AWS will automatically create the required instances. You will see something like this, where EC2 instances are automatically managed: Even if you terminate an instance manually, the ASG will automatically launch a new one to replace it. Step 5: Clean Up to Avoid Extra Costs When you're done testing, it's important to clean up resources: Terminate the Auto Scaling Group: This will stop it from launching new instances. Delete EC2 Instances: Terminate any EC2 instances that are still running. Delete Launch Templates (if no longer needed): This will prevent clutter in your account. Final Notes: Auto Scaling Groups incur costs, so remember to clean up after you’re done testing. You can use this setup to automatically scale your application based on traffic and performance metrics. That’s it! You’ve now created an EC2 instance, Launch Template, Auto Scaling Group, and learned how to clean up afterward to save costs. Feel free to ask if you have any questions!

Apr 20, 2025 - 04:59
 0
Simple Guide to Setting Up EC2 with Auto Scaling Groups (ASG) on AWS

Step 1: Create an EC2 Instance

Before creating an Auto Scaling Group (ASG), we need an EC2 instance to use as a base for the Auto Scaling template.

  1. Go to the EC2 Dashboard:
    • Navigate to the EC2 Dashboard in your AWS console.

EC2 Dashboard

  1. Launch an EC2 Instance:

    • Click Launch Instance.
  2. Select an Amazon Machine Image (AMI):

    • Choose an Amazon Linux 2 or any preferred AMI for your EC2 instance.
  3. Choose an Instance Type:

    • Select t2.micro (Free Tier) or t3.micro for a test environment.
  4. Configure Instance:

    • Leave the default settings unless you need specific configurations.
    • Set the number of instances to 1.
  5. Add Storage:

    • The default 8GB is enough for a simple test setup.
  6. Configure Security Group:

    • Add rules for:
      • HTTP (port 80) – for web access
      • SSH (port 22) – for remote access (you can restrict to your IP for security)
  7. Review and Launch:

    • Select or create a key pair to SSH into the instance.
  8. Launch the Instance.

Step 2: Create a Launch Template

A Launch Template defines the configuration for your EC2 instances.

  1. Go to EC2 Dashboard and click Launch Templates.
  2. Click “Create Launch Template”.

Launch Template

  1. Fill in the following fields:

    • Name: Choose a name for the template (e.g., MyWebAppTemplate).
    • AMI: Select the same AMI you used for the EC2 instance (e.g., Amazon Linux 2).
    • Instance Type: Select the instance type (e.g., t2.micro).
    • Key Pair: Select the key pair you created earlier.
    • Security Group: Select the same security group you used for the EC2 instance.
  2. Click Create Launch Template.

Step 3: Create an Auto Scaling Group (ASG)

Now that we have the Launch Template, let’s create an Auto Scaling Group.

  1. Go to EC2 Dashboard > Auto Scaling Groups.
  2. Click “Create Auto Scaling group”.

Create ASG

  1. Configure the Auto Scaling Group:

    • Name: Enter a name (e.g., MyWebAppASG).
    • Launch Template: Choose the template you created earlier (e.g., MyWebAppTemplate).
    • Version: Select the latest version.
    • Desired Capacity: Set to 2 (this means 2 instances should be running).
    • Minimum Capacity: Set to 2.
    • Maximum Capacity: Set to 3.
  2. Configure Scaling Policies:

    • Scaling Type: Choose Target Tracking Policy.
    • Target Value: Set to 30% for CPU utilization.
  3. Review and Create the Auto Scaling Group.

Step 4: Monitor EC2 Instances Scaling

After setting up the ASG, AWS will automatically create the required instances.

You will see something like this, where EC2 instances are automatically managed:

EC2 Instances Running

Even if you terminate an instance manually, the ASG will automatically launch a new one to replace it.

Step 5: Clean Up to Avoid Extra Costs

When you're done testing, it's important to clean up resources:

  1. Terminate the Auto Scaling Group:

    • This will stop it from launching new instances.
  2. Delete EC2 Instances:

    • Terminate any EC2 instances that are still running.
  3. Delete Launch Templates (if no longer needed):

    • This will prevent clutter in your account.

ASG Terminated

Final Notes:

  • Auto Scaling Groups incur costs, so remember to clean up after you’re done testing.
  • You can use this setup to automatically scale your application based on traffic and performance metrics.

That’s it! You’ve now created an EC2 instance, Launch Template, Auto Scaling Group, and learned how to clean up afterward to save costs.

Feel free to ask if you have any questions!