How to do it...

  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 optional auto scaling group-configuration parameters:
      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

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 auto scaling 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 used by the auto scaling 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 resourcesone 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 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 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-auto-scaling-an-application-server.yaml.
  1. Launch the template with the following AWS CLI command, supplying your subnet IDs:
      aws cloudformation create-stack  
--stack-name asg
--template-body file://04-auto-scaling-an-application-server.yaml
--parameters
ParameterKey=SubnetIds,ParameterValue='<subnet-id-1>,
<subnet-id-2>'
..................Content has been hidden....................

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