Configure Amazon EC2 Auto Scaling resources with AWS CloudFormation - AWS CloudFormation

Configure Amazon EC2 Auto Scaling resources with AWS CloudFormation

The following examples show different snippets to include in templates for use with Amazon EC2 Auto Scaling.

Create a single instance Auto Scaling group

This example shows an AWS::AutoScaling::AutoScalingGroup resource with a single instance to help you get started. The VPCZoneIdentifier property of the Auto Scaling group specifies a list of existing subnets in three different Availability Zones. You must specify the applicable subnet IDs from your account before you create your stack. The LaunchTemplate property references an AWS::EC2::LaunchTemplate resource with the logical name myLaunchTemplate that is defined elsewhere in your template.

Note

For examples of launch templates, see Create launch templates with AWS CloudFormation in the Amazon EC2 snippets section and the Examples section in the AWS::EC2::LaunchTemplate resource.

JSON

"myASG" : { "Type" : "AWS::AutoScaling::AutoScalingGroup", "Properties" : { "VPCZoneIdentifier" : [ "subnetIdAz1", "subnetIdAz2", "subnetIdAz3" ], "LaunchTemplate" : { "LaunchTemplateId" : { "Ref" : "myLaunchTemplate" }, "Version" : { "Fn::GetAtt" : [ "myLaunchTemplate", "LatestVersionNumber" ] } }, "MaxSize" : "1", "MinSize" : "1" } }

YAML

myASG: Type: AWS::AutoScaling::AutoScalingGroup Properties: VPCZoneIdentifier: - subnetIdAz1 - subnetIdAz2 - subnetIdAz3 LaunchTemplate: LaunchTemplateId: !Ref myLaunchTemplate Version: !GetAtt myLaunchTemplate.LatestVersionNumber MaxSize: '1' MinSize: '1'

Create an Auto Scaling group with an attached load balancer

This example shows an AWS::AutoScaling::AutoScalingGroup resource for load balancing over multiple servers. It specifies the logical names of AWS resources declared elsewhere in the same template.

  1. The VPCZoneIdentifier property specifies the logical names of two AWS::EC2::Subnet resources where the Auto Scaling group's EC2 instances will be created: myPublicSubnet1 and myPublicSubnet2.

  2. The LaunchTemplate property specifies an AWS::EC2::LaunchTemplate resource with the logical name myLaunchTemplate.

  3. The TargetGroupARNs property lists the target groups for an Application Load Balancer or Network Load Balancer used to route traffic to the Auto Scaling group. In this example, one target group is specified, an AWS::ElasticLoadBalancingV2::TargetGroup resource with the logical name myTargetGroup.

JSON

"myServerGroup" : { "Type" : "AWS::AutoScaling::AutoScalingGroup", "Properties" : { "VPCZoneIdentifier" : [ { "Ref" : "myPublicSubnet1" }, { "Ref" : "myPublicSubnet2" } ], "LaunchTemplate" : { "LaunchTemplateId" : { "Ref" : "myLaunchTemplate" }, "Version" : { "Fn::GetAtt" : [ "myLaunchTemplate", "LatestVersionNumber" ] } }, "MaxSize" : "5", "MinSize" : "1", "TargetGroupARNs" : [ { "Ref" : "myTargetGroup" } ] } }

YAML

myServerGroup: Type: AWS::AutoScaling::AutoScalingGroup Properties: VPCZoneIdentifier: - !Ref myPublicSubnet1 - !Ref myPublicSubnet2 LaunchTemplate: LaunchTemplateId: !Ref myLaunchTemplate Version: !GetAtt myLaunchTemplate.LatestVersionNumber MaxSize: '5' MinSize: '1' TargetGroupARNs: - !Ref myTargetGroup

See also

For a detailed example that creates an Auto Scaling group with a target tracking scaling policy based on the ALBRequestCountPerTarget predefined metric for your Application Load Balancer, see the Examples section in the AWS::AutoScaling::ScalingPolicy resource.

Create an Auto Scaling group with notifications

This example shows an AWS::AutoScaling::AutoScalingGroup resource that sends Amazon SNS notifications when the specified events take place. The NotificationConfigurations property specifies the SNS topic where AWS CloudFormation sends a notification and the events that will cause AWS CloudFormation to send notifications. When the events specified by NotificationTypes occur, AWS CloudFormation will send a notification to the SNS topic specified by TopicARN. When you launch the stack, AWS CloudFormation creates an AWS::SNS::Subscription resource (snsTopicForAutoScalingGroup) that's declared within the same template.

The VPCZoneIdentifier property of the Auto Scaling group specifies a list of existing subnets in three different Availability Zones. You must specify the applicable subnet IDs from your account before you create your stack. The LaunchTemplate property references the logical name of an AWS::EC2::LaunchTemplate resource declared elsewhere in the same template.

JSON

"myASG" : { "Type" : "AWS::AutoScaling::AutoScalingGroup", "DependsOn": [ "snsTopicForAutoScalingGroup" ], "Properties" : { "VPCZoneIdentifier" : [ "subnetIdAz1", "subnetIdAz2", "subnetIdAz3" ], "LaunchTemplate" : { "LaunchTemplateId" : { "Ref" : "logicalName" }, "Version" : { "Fn::GetAtt" : [ "logicalName", "LatestVersionNumber" ] } }, "MaxSize" : "5", "MinSize" : "1", "NotificationConfigurations" : [ { "TopicARN" : { "Ref" : "snsTopicForAutoScalingGroup" }, "NotificationTypes" : [ "autoscaling:EC2_INSTANCE_LAUNCH", "autoscaling:EC2_INSTANCE_LAUNCH_ERROR", "autoscaling:EC2_INSTANCE_TERMINATE", "autoscaling:EC2_INSTANCE_TERMINATE_ERROR", "autoscaling:TEST_NOTIFICATION" ] } ] } }

YAML

myASG: Type: AWS::AutoScaling::AutoScalingGroup DependsOn: - snsTopicForAutoScalingGroup Properties: VPCZoneIdentifier: - subnetIdAz1 - subnetIdAz2 - subnetIdAz3 LaunchTemplate: LaunchTemplateId: !Ref logicalName Version: !GetAtt logicalName.LatestVersionNumber MaxSize: '5' MinSize: '1' NotificationConfigurations: - TopicARN: !Ref snsTopicForAutoScalingGroup NotificationTypes: - autoscaling:EC2_INSTANCE_LAUNCH - autoscaling:EC2_INSTANCE_LAUNCH_ERROR - autoscaling:EC2_INSTANCE_TERMINATE - autoscaling:EC2_INSTANCE_TERMINATE_ERROR - autoscaling:TEST_NOTIFICATION

Create an Auto Scaling group that uses a CreationPolicy and an UpdatePolicy

The following example shows how to add CreationPolicy attribute and UpdatePolicy attributes to an AWS::AutoScaling::AutoScalingGroup resource.

The sample creation policy prevents the Auto Scaling group from reaching CREATE_COMPLETE status until AWS CloudFormation receives Count number of success signals when the group is ready. To signal that the Auto Scaling group is ready, a cfn-signal helper script added to the launch template's user data (not shown) is run on the instances. If the instances don't send a signal within the specified Timeout, CloudFormation assumes that the instances were not created, the resource creation fails, and CloudFormation rolls the stack back.

The sample update policy instructs CloudFormation to perform a rolling update using the AutoScalingRollingUpdate property. The rolling update makes changes to the Auto Scaling group in small batches (for this example, instance by instance) based on the MaxBatchSize and a pause time between batches of updates based on the PauseTime. The MinInstancesInService attribute specifies the minimum number of instances that must be in service within the Auto Scaling group while CloudFormation updates old instances.

The WaitOnResourceSignals attribute is set to true. CloudFormation must receive a signal from each new instance within the specified PauseTime before continuing the update. While the stack update is in progress, the following EC2 Auto Scaling processes are suspended: HealthCheck, ReplaceUnhealthy, AZRebalance, AlarmNotification, and ScheduledActions. Note: Don't suspend the Launch, Terminate, or AddToLoadBalancer (if the Auto Scaling group is being used with Elastic Load Balancing) process types because doing so can prevent the rolling update from functioning properly.

The VPCZoneIdentifier property of the Auto Scaling group specifies a list of existing subnets in three different Availability Zones. You must specify the applicable subnet IDs from your account before you create your stack. The LaunchTemplate property references the logical name of an AWS::EC2::LaunchTemplate resource declared elsewhere in the same template.

JSON

{ "Resources":{ "myASG":{ "CreationPolicy":{ "ResourceSignal":{ "Count":"3", "Timeout":"PT15M" } }, "UpdatePolicy":{ "AutoScalingRollingUpdate":{ "MinInstancesInService":"1", "MaxBatchSize":"1", "PauseTime":"PT12M5S", "WaitOnResourceSignals":"true", "SuspendProcesses":[ "HealthCheck", "ReplaceUnhealthy", "AZRebalance", "AlarmNotification", "ScheduledActions" ] } }, "Type":"AWS::AutoScaling::AutoScalingGroup", "Properties":{ "VPCZoneIdentifier":[ "subnetIdAz1", "subnetIdAz2", "subnetIdAz3" ], "LaunchTemplate":{ "LaunchTemplateId":{ "Ref":"logicalName" }, "Version":{ "Fn::GetAtt":[ "logicalName", "LatestVersionNumber" ] } }, "MaxSize":"5", "MinSize":"3" } } } }

YAML

--- Resources: myASG: CreationPolicy: ResourceSignal: Count: '3' Timeout: PT15M UpdatePolicy: AutoScalingRollingUpdate: MinInstancesInService: '1' MaxBatchSize: '1' PauseTime: PT12M5S WaitOnResourceSignals: true SuspendProcesses: - HealthCheck - ReplaceUnhealthy - AZRebalance - AlarmNotification - ScheduledActions Type: AWS::AutoScaling::AutoScalingGroup Properties: VPCZoneIdentifier: - subnetIdAz1 - subnetIdAz2 - subnetIdAz3 LaunchTemplate: LaunchTemplateId: !Ref logicalName Version: !GetAtt logicalName.LatestVersionNumber MaxSize: '5' MinSize: '3'

Create a step scaling policy

This example shows an AWS::AutoScaling::ScalingPolicy resource that scales out the Auto Scaling group using a step scaling policy. The AdjustmentType property specifies ChangeInCapacity, which means that the ScalingAdjustment represents the number of instances to add (if ScalingAdjustment is positive) or delete (if it is negative). In this example, ScalingAdjustment is 1; therefore, the policy increments the number of EC2 instances in the group by 1 when the alarm threshold is breached.

The AWS::CloudWatch::Alarm resource CPUAlarmHigh specifies the scaling policy ASGScalingPolicyHigh as the action to run when the alarm is in an ALARM state (AlarmActions). The Dimensions property references the logical name of an AWS::AutoScaling::AutoScalingGroup resource declared elsewhere in the same template.

JSON

{ "Resources":{ "ASGScalingPolicyHigh":{ "Type":"AWS::AutoScaling::ScalingPolicy", "Properties":{ "AutoScalingGroupName":{ "Ref":"logicalName" }, "PolicyType":"StepScaling", "AdjustmentType":"ChangeInCapacity", "StepAdjustments":[ { "MetricIntervalLowerBound":0, "ScalingAdjustment":1 } ] } }, "CPUAlarmHigh":{ "Type":"AWS::CloudWatch::Alarm", "Properties":{ "EvaluationPeriods":"2", "Statistic":"Average", "Threshold":"90", "AlarmDescription":"Scale out if CPU > 90% for 2 minutes", "Period":"60", "AlarmActions":[ { "Ref":"ASGScalingPolicyHigh" } ], "Namespace":"AWS/EC2", "Dimensions":[ { "Name":"AutoScalingGroupName", "Value":{ "Ref":"logicalName" } } ], "ComparisonOperator":"GreaterThanThreshold", "MetricName":"CPUUtilization" } } } }

YAML

--- Resources: ASGScalingPolicyHigh: Type: AWS::AutoScaling::ScalingPolicy Properties: AutoScalingGroupName: !Ref logicalName PolicyType: StepScaling AdjustmentType: ChangeInCapacity StepAdjustments: - MetricIntervalLowerBound: 0 ScalingAdjustment: 1 CPUAlarmHigh: Type: AWS::CloudWatch::Alarm Properties: EvaluationPeriods: 2 Statistic: Average Threshold: 90 AlarmDescription: 'Scale out if CPU > 90% for 2 minutes' Period: 60 AlarmActions: - !Ref ASGScalingPolicyHigh Namespace: AWS/EC2 Dimensions: - Name: AutoScalingGroupName Value: !Ref logicalName ComparisonOperator: GreaterThanThreshold MetricName: CPUUtilization

See also

For more example templates for scaling policies, see the Examples section in the AWS::AutoScaling::ScalingPolicy resource.

Mixed instances group examples

Create an Auto Scaling group using attribute-based instance type selection

This example shows an AWS::AutoScaling::AutoScalingGroup resource that contains the information to launch a mixed instances group using attribute-based instance type selection. You specify the minimum and maximum values for the VCpuCount property and the minimum value for the MemoryMiB property. Any instance types used by the Auto Scaling group must match your required instance attributes.

The VPCZoneIdentifier property of the Auto Scaling group specifies a list of existing subnets in three different Availability Zones. You must specify the applicable subnet IDs from your account before you create your stack. The LaunchTemplate property references the logical name of an AWS::EC2::LaunchTemplate resource declared elsewhere in the same template.

JSON

{ "Resources":{ "myASG":{ "Type":"AWS::AutoScaling::AutoScalingGroup", "Properties":{ "VPCZoneIdentifier":[ "subnetIdAz1", "subnetIdAz2", "subnetIdAz3" ], "MixedInstancesPolicy":{ "LaunchTemplate":{ "LaunchTemplateSpecification":{ "LaunchTemplateId":{ "Ref":"logicalName" }, "Version":{ "Fn::GetAtt":[ "logicalName", "LatestVersionNumber" ] } }, "Overrides":[ { "InstanceRequirements":{ "VCpuCount":{ "Min":2, "Max":4 }, "MemoryMiB":{ "Min":2048 } } } ] } }, "MaxSize":"5", "MinSize":"1" } } } }

YAML

--- Resources: myASG: Type: AWS::AutoScaling::AutoScalingGroup Properties: VPCZoneIdentifier: - subnetIdAz1 - subnetIdAz2 - subnetIdAz3 MixedInstancesPolicy: LaunchTemplate: LaunchTemplateSpecification: LaunchTemplateId: !Ref logicalName Version: !GetAtt logicalName.LatestVersionNumber Overrides: - InstanceRequirements: VCpuCount: Min: 2 Max: 4 MemoryMiB: Min: 2048 MaxSize: '5' MinSize: '1'

Launch configuration examples

Create a launch configuration

This example shows an AWS::AutoScaling::LaunchConfiguration resource for an Auto Scaling group where you specify values for the ImageId, InstanceType, and SecurityGroups properties. The SecurityGroups property specifies both the logical name of an AWS::EC2::SecurityGroup resource that's specified elsewhere in the template, and an existing EC2 security group named myExistingEC2SecurityGroup.

JSON

"mySimpleConfig" : { "Type" : "AWS::AutoScaling::LaunchConfiguration", "Properties" : { "ImageId" : "ami-02354e95b3example", "InstanceType" : "t3.micro", "SecurityGroups" : [ { "Ref" : "logicalName" }, "myExistingEC2SecurityGroup" ] } }

YAML

mySimpleConfig: Type: AWS::AutoScaling::LaunchConfiguration Properties: ImageId: ami-02354e95b3example InstanceType: t3.micro SecurityGroups: - !Ref logicalName - myExistingEC2SecurityGroup

Create an Auto Scaling group that uses a launch configuration

This example shows an AWS::AutoScaling::AutoScalingGroup resource with a single instance. The VPCZoneIdentifier property of the Auto Scaling group specifies a list of existing subnets in three different Availability Zones. You must specify the applicable subnet IDs from your account before you create your stack. The LaunchConfigurationName property references an AWS::AutoScaling::LaunchConfiguration resource with the logical name mySimpleConfig that is defined in your template.

JSON

"myASG" : { "Type" : "AWS::AutoScaling::AutoScalingGroup", "Properties" : { "VPCZoneIdentifier" : [ "subnetIdAz1", "subnetIdAz2", "subnetIdAz3" ], "LaunchConfigurationName" : { "Ref" : "mySimpleConfig" }, "MaxSize" : "1", "MinSize" : "1" } }

YAML

myASG: Type: AWS::AutoScaling::AutoScalingGroup Properties: VPCZoneIdentifier: - subnetIdAz1 - subnetIdAz2 - subnetIdAz3 LaunchConfigurationName: !Ref mySimpleConfig MaxSize: '1' MinSize: '1'