How to do it…

Follow these steps to create a CloudFormation template that launches a stack with an autoscaling group:

  1. Start by defining the template version and description:
AWSTemplateFormatVersion: "2010-09-09"
Description: Create an Auto Scaling Group
  1. 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>
  1. 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
  1. 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
  1. 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
  1. 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
  1. 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
  1. 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
  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
  1. 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
  1. Save the template with the filename 04-01-AutoScaling.yml.
  1. 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>'
  1. 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.
  2. Delete the stack to prevent future charges for the resources that were created in this recipe.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset