Follow these steps to create a CloudFormation template that launches a stack with an autoscaling group:
- Start by defining the template version and description:
AWSTemplateFormatVersion: "2010-09-09"
Description: Create an Auto Scaling Group
- Add a Parameters section with the required parameters that will be used later in the template:
Parameters:
SubnetIds:
Description: Subnet IDs where instances can be launched
Type: List<AWS::EC2::Subnet::Id>
- Still under the Parameters section, add the optional instance configuration parameters:
AmiId:
Description: The application server's AMI ID
Type: AWS::EC2::Image::Id
Default: ami-9be6f38c # AWS Linux in us-east-1
InstanceType:
Description: The type of instance to launch
Type: String
Default: t2.micro
- Still under the Parameters section, add the minimum and maximum sizes:
MinSize:
Description: Minimum number of instances in the group
Type: Number
Default: 1
MaxSize:
Description: Maximum number of instances in the group
Type: Number
Default: 4
- Then, add the settings for the CPU thresholds:
ThresholdCPUHigh:
Description: Launch new instances when CPU utilization
is over this threshold
Type: Number
Default: 60
ThresholdCPULow:
Description: Remove instances when CPU utilization
is under this threshold
Type: Number
Default: 40
ThresholdMinutes:
Description: Launch new instances when over the CPU
threshold for this many minutes
Type: Number
Default: 5
- Add a Resources section and define the autoscaling group resource:
Resources:
AutoScalingGroup:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
MinSize: !Ref MinSize
MaxSize: !Ref MaxSize
LaunchConfigurationName: !Ref LaunchConfiguration
Tags:
- Key: Name
Value: !Sub "${AWS::StackName} server"
PropagateAtLaunch: true
VPCZoneIdentifier: !Ref SubnetIds
- Still under the Resources section, define the launch configuration that's used by the autoscaling group:
LaunchConfiguration:
Type: AWS::AutoScaling::LaunchConfiguration
Properties:
ImageId: !Ref AmiId
InstanceType: !Ref InstanceType
UserData:
Fn::Base64: !Sub |
#!/bin/bash -xe
# This will be run on startup, launch your application here
- Next, define two scaling policy resources – one to scale up and the other to scale down:
ScaleUpPolicy:
Type: AWS::AutoScaling::ScalingPolicy
Properties:
AdjustmentType: ChangeInCapacity
AutoScalingGroupName: !Ref AutoScalingGroup
Cooldown: 60
ScalingAdjustment: 1
ScaleDownPolicy:
Type: AWS::AutoScaling::ScalingPolicy
Properties:
AdjustmentType: ChangeInCapacity
AutoScalingGroupName: !Ref AutoScalingGroup
Cooldown: 60
ScalingAdjustment: -1
- Define an alarm that will alert you when the CPU goes over the ThresholdCPUHigh parameter:
CPUHighAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
ActionsEnabled: true
AlarmActions:
- !Ref ScaleUpPolicy
AlarmDescription: Scale up on CPU load
ComparisonOperator: GreaterThanThreshold
Dimensions:
- Name: AutoScalingGroupName
Value: !Ref AutoScalingGroup
EvaluationPeriods: !Ref ThresholdMinutes
MetricName: CPUUtilization
Namespace: AWS/EC2
Period: 60
Statistic: Average
Threshold: !Ref ThresholdCPUHigh
- Finally, define an alarm that will alert you when the CPU goes under the ThresholdCPULow parameter:
CPULowAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
ActionsEnabled: true
AlarmActions:
- !Ref ScaleDownPolicy
AlarmDescription: Scale down on CPU load
ComparisonOperator: LessThanThreshold
Dimensions:
- Name: AutoScalingGroupName
Value: !Ref AutoScalingGroup
EvaluationPeriods: !Ref ThresholdMinutes
MetricName: CPUUtilization
Namespace: AWS/EC2
Period: 60
Statistic: Average
Threshold: !Ref ThresholdCPULow
- Save the template with the filename 04-01-AutoScaling.yml.
- Launch the template with the following AWS CLI command, supplying your subnet IDs in place of <subnet-id-1> and <subnet-id-2>:
aws cloudformation create-stack
--stack-name asg
--template-body file://04-01-AutoScaling.yml
--parameters
ParameterKey=SubnetIds,ParameterValue='<subnet-id-1>,<subnet-id-2>'
- At this point, the CFN service is provisioning all the resources in the template and will take a few minutes to complete. Once the stack has reached a CREATE_COMPLETE status, you can confirm that the autoscaling group is working correctly by checking for a new EC2 instance with the name asg-server.
- Delete the stack to prevent future charges for the resources that were created in this recipe.