0
0
AWScloud~5 mins

Auto Scaling groups in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
Auto Scaling groups help keep your app running smoothly by automatically adding or removing servers based on how busy your app is. This means your app can handle more visitors without crashing and save money when fewer servers are needed.
When your website gets more visitors during the day and fewer at night, and you want to adjust servers automatically.
When you want to replace servers that stop working without manual effort.
When you want to save money by running only the number of servers needed at any time.
When you want to make sure your app stays available even if some servers fail.
When you want to test how your app behaves under different loads by scaling servers up and down.
Config File - asg-configuration.json
asg-configuration.json
{
  "AutoScalingGroupName": "example-asg",
  "LaunchConfigurationName": "example-launch-config",
  "MinSize": 1,
  "MaxSize": 3,
  "DesiredCapacity": 2,
  "AvailabilityZones": ["us-east-1a", "us-east-1b"],
  "Tags": [
    {
      "Key": "Name",
      "Value": "example-instance",
      "PropagateAtLaunch": true
    }
  ]
}

This JSON file defines an Auto Scaling group named example-asg. It uses a launch configuration called example-launch-config to create servers. The group keeps at least 1 server running and can scale up to 3 servers. It tries to keep 2 servers running by default. The servers will be launched in two availability zones for reliability. The tag Name=example-instance helps identify the servers.

Commands
This command creates a launch configuration named example-launch-config. It tells AWS what kind of server to launch, using a specific image and instance type.
Terminal
aws autoscaling create-launch-configuration --launch-configuration-name example-launch-config --image-id ami-0c94855ba95c71c99 --instance-type t2.micro
Expected OutputExpected
No output (command runs silently)
--launch-configuration-name - Names the launch configuration for reuse in Auto Scaling groups
--image-id - Specifies the server image to use
--instance-type - Specifies the size and power of the server
This command creates the Auto Scaling group named example-asg using the launch configuration. It sets the minimum, maximum, and desired number of servers and specifies the zones where servers will run. It also adds a tag to identify the servers.
Terminal
aws autoscaling create-auto-scaling-group --auto-scaling-group-name example-asg --launch-configuration-name example-launch-config --min-size 1 --max-size 3 --desired-capacity 2 --availability-zones us-east-1a us-east-1b --tags Key=Name,Value=example-instance,PropagateAtLaunch=true
Expected OutputExpected
No output (command runs silently)
--auto-scaling-group-name - Names the Auto Scaling group
--min-size - Minimum number of servers to keep running
--max-size - Maximum number of servers allowed
--desired-capacity - Number of servers to start with
This command checks the details of the Auto Scaling group to confirm it was created correctly and see its current state.
Terminal
aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names example-asg
Expected OutputExpected
{ "AutoScalingGroups": [ { "AutoScalingGroupName": "example-asg", "LaunchConfigurationName": "example-launch-config", "MinSize": 1, "MaxSize": 3, "DesiredCapacity": 2, "AvailabilityZones": [ "us-east-1a", "us-east-1b" ], "Instances": [], "Tags": [ { "Key": "Name", "Value": "example-instance", "PropagateAtLaunch": true } ] } ] }
--auto-scaling-group-names - Specifies which Auto Scaling group to describe
This command changes the desired number of servers to 3, telling AWS to add one more server to handle more traffic.
Terminal
aws autoscaling update-auto-scaling-group --auto-scaling-group-name example-asg --desired-capacity 3
Expected OutputExpected
No output (command runs silently)
--desired-capacity - Sets the new target number of servers
This command deletes the Auto Scaling group and all its servers immediately, cleaning up resources when you no longer need them.
Terminal
aws autoscaling delete-auto-scaling-group --auto-scaling-group-name example-asg --force-delete
Expected OutputExpected
No output (command runs silently)
--force-delete - Deletes the group even if it has running servers
Key Concept

If you remember nothing else from this pattern, remember: Auto Scaling groups automatically adjust the number of servers to keep your app running well and save money.

Common Mistakes
Not creating a launch configuration before creating the Auto Scaling group
The Auto Scaling group needs a launch configuration to know what kind of servers to create, so it will fail without one.
Always create a launch configuration first with the desired server settings before creating the Auto Scaling group.
Setting min-size higher than max-size
This causes a conflict and the Auto Scaling group will not work properly because it cannot keep more servers than the maximum allowed.
Ensure min-size is less than or equal to max-size when creating or updating the group.
Not specifying availability zones
Without availability zones, the Auto Scaling group cannot launch servers in the right places, which may cause failures or poor availability.
Always specify at least one availability zone when creating the Auto Scaling group.
Summary
Create a launch configuration to define the server type and image.
Create an Auto Scaling group using the launch configuration, setting min, max, and desired server counts.
Check the Auto Scaling group details to confirm setup.
Update the desired capacity to scale servers up or down as needed.
Delete the Auto Scaling group when it is no longer needed.