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
                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-subnetIdAz3LaunchTemplate: LaunchTemplateId: !RefmyLaunchTemplateVersion: !GetAttmyLaunchTemplate.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.
- 
          The VPCZoneIdentifierproperty specifies the logical names of twoAWS::EC2::Subnetresources where the Auto Scaling group's EC2 instances will be created:myPublicSubnet1andmyPublicSubnet2.
- 
          The LaunchTemplateproperty specifies anAWS::EC2::LaunchTemplateresource with the logical namemyLaunchTemplate.
- 
          The TargetGroupARNsproperty 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, anAWS::ElasticLoadBalancingV2::TargetGroupresource with the logical namemyTargetGroup.
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: - !RefmyPublicSubnet1- !RefmyPublicSubnet2LaunchTemplate: LaunchTemplateId: !RefmyLaunchTemplateVersion: !GetAttmyLaunchTemplate.LatestVersionNumber MaxSize: '5' MinSize: '1' TargetGroupARNs: - !RefmyTargetGroup
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-subnetIdAz3LaunchTemplate: LaunchTemplateId: !ReflogicalNameVersion: !GetAttlogicalName.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 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.
For more information about the CreationPolicy and UpdatePolicy
        attributes, see Resource attribute reference.
JSON
{ "Resources":{ "myASG":{ "CreationPolicy":{ "ResourceSignal":{ "Count":"3", "Timeout":"PT15M" } }, "UpdatePolicy":{ "AutoScalingRollingUpdate":{ "MinInstancesInService":"3", "MaxBatchSize":"1", "PauseTime":"PT12M5S", "WaitOnResourceSignals":"true", "SuspendProcesses":[ "HealthCheck", "ReplaceUnhealthy", "AZRebalance", "AlarmNotification", "ScheduledActions", "InstanceRefresh" ] } }, "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: '3' MaxBatchSize: '1' PauseTime: PT12M5S WaitOnResourceSignals: true SuspendProcesses: - HealthCheck - ReplaceUnhealthy - AZRebalance - AlarmNotification - ScheduledActions - InstanceRefresh Type: AWS::AutoScaling::AutoScalingGroup Properties: VPCZoneIdentifier: -subnetIdAz1-subnetIdAz2-subnetIdAz3LaunchTemplate: LaunchTemplateId: !ReflogicalNameVersion: !GetAttlogicalName.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: !ReflogicalNamePolicyType: 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: !ReflogicalNameComparisonOperator: 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-subnetIdAz3MixedInstancesPolicy: LaunchTemplate: LaunchTemplateSpecification: LaunchTemplateId: !ReflogicalNameVersion: !GetAttlogicalName.LatestVersionNumber Overrides: - InstanceRequirements: VCpuCount: Min:2Max:4MemoryMiB: Min:2048MaxSize: '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-02354e95b3exampleInstanceType:t3.microSecurityGroups: - !ReflogicalName-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-subnetIdAz3LaunchConfigurationName: !RefmySimpleConfigMaxSize: '1' MinSize: '1'