💾 Archived View for cfdocs.wetterberg.nu › quickref-autoscaling.gemini captured on 2024-05-10 at 12:33:11. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2021-12-03)

-=-=-=-=-=-=-

Auto scaling template snippets

Search

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

Amazon EC2 Auto Scaling enables you to automatically scale Amazon EC2 instances, either with scaling policies or with scheduled scaling. Auto Scaling groups are collections of Amazon EC2 instances that enable automatic scaling and fleet management features, such as health checks and integration with Elastic Load Balancing.

Application Auto Scaling provides automatic scaling of different resources beyond Amazon EC2, either with scaling policies or with scheduled scaling.

Declaring 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 is specified in the template, and an existing EC2 security group named `myExistingEC2SecurityGroup`.

AWS::AutoScaling::LaunchConfiguration

AWS::EC2::SecurityGroup

JSON

1. "mySimpleConfig" : {
2.    "Type" : "AWS::AutoScaling::LaunchConfiguration",
3.    "Properties" : {
4.       "ImageId" : "ami-02354e95b39ca8dec",
5.       "SecurityGroups" : [ { "Ref" : "logicalName" }, "myExistingEC2SecurityGroup" ],
6.       "InstanceType" : "t3.micro"
7.    }
8. }

YAML

1. mySimpleConfig:
2.   Type: AWS::AutoScaling::LaunchConfiguration
3.   Properties:
4.     ImageId: ami-02354e95b39ca8dec
5.     SecurityGroups:
6.     - Ref: logicalName
7.     - myExistingEC2SecurityGroup
8.     InstanceType: t3.micro

Declaring 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 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.

AWS::AutoScaling::AutoScalingGroup

AWS::AutoScaling::LaunchConfiguration

JSON

 1. "myASG" : {
 2.    "Type" : "AWS::AutoScaling::AutoScalingGroup",
 3.    "Properties" : {
 4.       "VPCZoneIdentifier" : [ "subnetIdAz1", "subnetIdAz2", "subnetIdAz3" ],
 5.       "LaunchConfigurationName" : { "Ref" : "mySimpleConfig" },
 6.       "MinSize" : "0",
 7.       "MaxSize" : "1",
 8.       "DesiredCapacity" : "1"
 9.    }
10. }

YAML

 1. myASG:
 2.   Type: AWS::AutoScaling::AutoScalingGroup
 3.   Properties:
 4.     VPCZoneIdentifier:
 5.       - subnetIdAz1
 6.       - subnetIdAz2
 7.       - subnetIdAz3
 8.     LaunchConfigurationName: !Ref mySimpleConfig
 9.     MinSize: '0'
10.     MaxSize: '1'
11.     DesiredCapacity: '1'

Declaring an Auto Scaling group with load balancing

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.

AWS::AutoScaling::AutoScalingGroup

AWS::EC2::Subnet

AWS::AutoScaling::LaunchConfiguration

AWS::ElasticLoadBalancingV2::TargetGroup

JSON

 1. "myServerGroup" : {
 2.    "Type" : "AWS::AutoScaling::AutoScalingGroup",
 3.    "Properties" : {
 4.       "VPCZoneIdentifier" : [ { "Ref" : "myPublicSubnet1" }, { "Ref" : "myPublicSubnet2" } ],
 5.       "LaunchConfigurationName" : { "Ref" : "mySimpleConfig" },
 6.       "MinSize" : "1",
 7.       "MaxSize" : "5",
 8.       "HealthCheckGracePeriod" : 300,
 9.       "MaxInstanceLifetime" : 2592000,
10.       "TargetGroupARNs" : [ { "Ref" : "myTargetGroup" } ]
11.    }
12. }

YAML

 1. myServerGroup:
 2.   Type: AWS::AutoScaling::AutoScalingGroup
 3.   Properties:
 4.     VPCZoneIdentifier:
 5.       - !Ref myPublicSubnet1
 6.       - !Ref myPublicSubnet2
 7.     LaunchConfigurationName: !Ref mySimpleConfig
 8.     MinSize: '1'
 9.     MaxSize: '5'
10.     HealthCheckGracePeriod: 300
11.     MaxInstanceLifetime: 2592000
12.     TargetGroupARNs:
13.       - !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.

Examples

Declaring a scaling policy with a CloudWatch alarm

This example shows an AWS::AutoScaling::ScalingPolicy resource that scales out the Auto Scaling group using a simple 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.

AWS::AutoScaling::ScalingPolicy

The AWS::CloudWatch::Alarm resource `CPUAlarmHigh` specifies the scaling policy `myScaleOutPolicy` as the action to run when the alarm is in an ALARM state (`AlarmActions`).

AWS::CloudWatch::Alarm

JSON

 1. "myScaleOutPolicy" : {
 2.    "Type" : "AWS::AutoScaling::ScalingPolicy",
 3.    "Properties" : {
 4.       "AdjustmentType" : "ChangeInCapacity",
 5.       "AutoScalingGroupName" : { "Ref" : "logicalName" },
 6.       "ScalingAdjustment" : "1"
 7.    }
 8. },
 9. "CPUAlarmHigh" : {
10.    "Type" : "AWS::CloudWatch::Alarm",
11.    "Properties" : {
12.       "EvaluationPeriods" : "1",
13.       "Statistic" : "Average",
14.       "Threshold" : "10",
15.       "AlarmDescription" : "Alarm if CPU too high or metric disappears indicating instance is down",
16.       "Period" : "60",
17.       "AlarmActions" : [ { "Ref" : "myScaleOutPolicy" } ],
18.       "Namespace" : "AWS/EC2",
19.       "Dimensions" : [ {
20.          "Name" : "AutoScalingGroupName",
21.          "Value" : { "Ref" : "logicalName" }
22.       } ],
23.       "ComparisonOperator" : "GreaterThanThreshold",
24.       "MetricName" : "CPUUtilization"
25.    }
26. }

YAML

 1. myScaleOutPolicy:
 2.   Type: AWS::AutoScaling::ScalingPolicy
 3.   Properties:
 4.     AdjustmentType: ChangeInCapacity
 5.     AutoScalingGroupName: !Ref logicalName
 6.     ScalingAdjustment: '1'
 7. CPUAlarmHigh:
 8.   Type: AWS::CloudWatch::Alarm
 9.   Properties:
10.     EvaluationPeriods: '1'
11.     Statistic: Average
12.     Threshold: '10'
13.     AlarmDescription: Alarm if CPU too high or metric disappears indicating instance
14.       is down
15.     Period: '60'
16.     AlarmActions:
17.     - !Ref myScaleOutPolicy
18.     Namespace: AWS/EC2
19.     Dimensions:
20.     - Name: AutoScalingGroupName
21.       Value:
22.         Ref: logicalName
23.     ComparisonOperator: GreaterThanThreshold
24.     MetricName: CPUUtilization

See also

For example templates for the `TargetTrackingScaling` and `StepScaling` policy types, see the Examples section in the AWS::AutoScaling::ScalingPolicy resource.

Examples

Declaring an Auto Scaling group with a launch template and 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.

AWS::AutoScaling::AutoScalingGroup

AWS::SNS::Subscription

The `AvailabilityZones` and `VPCZoneIdentifier` properties of the Auto Scaling group reference parameter values that you pass to the template when creating or updating a stack. The `LaunchTemplate` property references the logical name of an AWS::EC2::LaunchTemplate resource declared elsewhere in the same template.

AWS::EC2::LaunchTemplate

JSON

 1. "myASG" : {
 2.   "Type" : "AWS::AutoScaling::AutoScalingGroup",
 3.   "DependsOn": [
 4.     "snsTopicForAutoScalingGroup"
 5.   ],
 6.   "Properties" : {
 7.     "AvailabilityZones" : { "Ref" : "AZs" },
 8.     "VPCZoneIdentifier" : { "Ref" : "Subnets" },
 9.     "LaunchTemplate" : {
10.       "LaunchTemplateId" : {
11.         "Ref" : "logicalName"
12.       },
13.       "Version" : {
14.         "Fn::GetAtt" : [
15.           "logicalName",
16.           "LatestVersionNumber"
17.         ]
18.       }
19.     },
20.     "MinSize" : "1",
21.     "MaxSize" : "5",
22.     "NotificationConfigurations" : [
23.       {
24.         "TopicARN" : { "Ref" : "snsTopicForAutoScalingGroup" },
25.         "NotificationTypes" : [
26.           "autoscaling:EC2_INSTANCE_LAUNCH",
27.           "autoscaling:EC2_INSTANCE_LAUNCH_ERROR",
28.           "autoscaling:EC2_INSTANCE_TERMINATE",
29.           "autoscaling:EC2_INSTANCE_TERMINATE_ERROR",
30.           "autoscaling:TEST_NOTIFICATION"
31.         ]
32.       }
33.     ]
34.   }
35. }

YAML

 1. myASG:
 2.   Type: AWS::AutoScaling::AutoScalingGroup
 3.   DependsOn:
 4.     - snsTopicForAutoScalingGroup
 5.   Properties:
 6.     AvailabilityZones: !Ref AZs
 7.     VPCZoneIdentifier: !Ref Subnets
 8.     LaunchTemplate:
 9.       LaunchTemplateId: !Ref logicalName
10.       Version: !GetAtt logicalName.LatestVersionNumber
11.     MinSize: '1'
12.     MaxSize: '5'
13.     NotificationConfigurations:
14.     - TopicARN: !Ref snsTopicForAutoScalingGroup
15.       NotificationTypes:
16.       - autoscaling:EC2_INSTANCE_LAUNCH
17.       - autoscaling:EC2_INSTANCE_LAUNCH_ERROR
18.       - autoscaling:EC2_INSTANCE_TERMINATE
19.       - autoscaling:EC2_INSTANCE_TERMINATE_ERROR
20.       - autoscaling:TEST_NOTIFICATION

See also

For more examples that specify a launch template for an Auto Scaling group, see the Examples section in the AWS::AutoScaling::AutoScalingGroup resource.

Examples

Declaring an Auto Scaling group with an UpdatePolicy

The following example specifies an UpdatePolicy attribute for an Auto Scaling group. 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.

UpdatePolicy attribute

The `WaitOnResourceSignals` attribute is set to `true`. CloudFormation must receive a signal from each new instance within the specified `PauseTime` before continuing the update. To signal the Auto Scaling group, a cfn\-signal helper script (not shown) is run on each instance. While the stack update is in progress, the following EC2 Auto Scaling processes are suspended: `HealthCheck`, `ReplaceUnhealthy`, `AZRebalance`, `AlarmNotification`, and `ScheduledActions`. Note: Do not 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.

cfn-signal

JSON

"myASG" : {
   "UpdatePolicy" : {
      "AutoScalingRollingUpdate" : {
         "MinInstancesInService" : "1",
         "MaxBatchSize" : "1",
         "PauseTime" : "PT12M5S",
         "WaitOnResourceSignals" : "true",
         "SuspendProcesses" : [
            "HealthCheck",
            "ReplaceUnhealthy",
            "AZRebalance",
            "AlarmNotification",
            "ScheduledActions"
         ]
      }
   },
   "Type" : "AWS::AutoScaling::AutoScalingGroup",
   "Properties" : {
      "AvailabilityZones" : { "Ref" : "AZs" },
      "VPCZoneIdentifier" : { "Ref" : "Subnets" },
      "LaunchConfigurationName" : { "Ref" : "logicalName" },
      "MaxSize" : "5",
      "MinSize" : "1"
   }
}

YAML

myASG:
  UpdatePolicy:
    AutoScalingRollingUpdate:
      MinInstancesInService: '1'
      MaxBatchSize: '1'
      PauseTime: PT12M5S
      WaitOnResourceSignals: true
      SuspendProcesses:
        - HealthCheck
        - ReplaceUnhealthy
        - AZRebalance
        - AlarmNotification
        - ScheduledActions
  Type: AWS::AutoScaling::AutoScalingGroup
  Properties:
    AvailabilityZones: !Ref AZs
    VPCZoneIdentifier: !Ref Subnets
    LaunchConfigurationName: !Ref logicalName
    MaxSize: '5'
    MinSize: '1'

Application Auto Scaling template examples

This section provides AWS CloudFormation template examples for Application Auto Scaling scaling policies and scheduled actions for different AWS resources.

When an Application Auto Scaling snippet is included in the template, you should declare a dependency on the specific scalable resource that is created through the template using the DependsOn attribute. This overrides the default parallelism and directs AWS CloudFormation to operate on resources in a specified order. Otherwise, the scaling configuration might be applied before the resource has been set up completely.

DependsOn

Declaring a scaling policy for an Aurora DB cluster

In this snippet, you register an existing AWS::RDS::DBCluster resource named `my-db-cluster`. The AWS::ApplicationAutoScaling::ScalableTarget resource indicates that the DB cluster should be dynamically scaled to have from one to eight Aurora Replicas. You also apply a target tracking scaling policy to the cluster using the AWS::ApplicationAutoScaling::ScalingPolicy resource.

AWS::RDS::DBCluster

AWS::ApplicationAutoScaling::ScalableTarget

AWS::ApplicationAutoScaling::ScalingPolicy

In this configuration, the `RDSReaderAverageCPUUtilization` predefined metric is used to adjust an Aurora DB cluster based on an average CPU utilization of 40 percent across all Aurora Replicas in that Aurora DB cluster. The configuration provides a scale-in cooldown period of 10 minutes and a scale-out cooldown period of 5 minutes.

JSON

{
  "Resources" : {
    "ScalableTarget" : {
      "Type" : "AWS::ApplicationAutoScaling::ScalableTarget",
      "Properties" : {
        "MaxCapacity" : 8,
        "MinCapacity" : 1,
        "RoleARN" : { "Fn::Sub" : "arn:aws:iam::${AWS::AccountId}:role/aws-service-role/rds.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_RDSCluster" },
        "ServiceNamespace" : "rds",
        "ScalableDimension" : "rds:cluster:ReadReplicaCount",
        "ResourceId" : "cluster:my-db-cluster"
      }
    },
    "ScalingPolicyDBCluster" : {
      "Type" : "AWS::ApplicationAutoScaling::ScalingPolicy",
      "Properties" : {
        "PolicyName" : { "Fn::Sub" : "${AWS::StackName}-target-tracking-cpu40" },
        "PolicyType" : "TargetTrackingScaling",
        "ServiceNamespace" : "rds",
        "ScalableDimension" : "rds:cluster:ReadReplicaCount",
        "ResourceId" : "cluster:my-db-cluster",
        "TargetTrackingScalingPolicyConfiguration" : {
          "TargetValue" : 40,
          "PredefinedMetricSpecification" : {
            "PredefinedMetricType" : "RDSReaderAverageCPUUtilization"
          },
          "ScaleInCooldown" : 600,
          "ScaleOutCooldown" : 300
        }
      }
    }
  }
}

YAML

---
Resources:
  ScalableTarget:
    Type: 'AWS::ApplicationAutoScaling::ScalableTarget'
    Properties:
      MaxCapacity: 8
      MinCapacity: 1
      RoleARN: 
        Fn::Sub: 'arn:aws:iam::${AWS::AccountId}:role/aws-service-role/rds.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_RDSCluster'
      ServiceNamespace: rds
      ScalableDimension: 'rds:cluster:ReadReplicaCount'
      ResourceId: 'cluster:my-db-cluster'
  ScalingPolicyDBCluster:
    Type: 'AWS::ApplicationAutoScaling::ScalingPolicy'
    Properties:
      PolicyName: !Sub ${AWS::StackName}-target-tracking-cpu40
      PolicyType: TargetTrackingScaling
      ServiceNamespace: rds
      ScalableDimension: 'rds:cluster:ReadReplicaCount'
      ResourceId: 'cluster:my-db-cluster'
      TargetTrackingScalingPolicyConfiguration:
        TargetValue: 40
        PredefinedMetricSpecification:
          PredefinedMetricType: RDSReaderAverageCPUUtilization
        ScaleInCooldown: 600
        ScaleOutCooldown: 300

Declaring a scaling policy for a DynamoDB table

This snippet shows how to create a policy with the `TargetTrackingScaling` policy type and apply it to an AWS::DynamoDB::Table resource using the AWS::ApplicationAutoScaling::ScalingPolicy resource. The AWS::ApplicationAutoScaling::ScalableTarget resource declares a scalable target to which this policy is applied, with a minimum of five write capacity units and a maximum of 15. The scaling policy scales the table's write capacity throughput to maintain the target utilization at 50 percent based on the `DynamoDBWriteCapacityUtilization` predefined metric.

AWS::DynamoDB::Table

AWS::ApplicationAutoScaling::ScalingPolicy

AWS::ApplicationAutoScaling::ScalableTarget

It uses the Fn::Join and Ref intrinsic functions to construct the `ResourceId` property with the logical name of the AWS::DynamoDB::Table resource that is specified in the same template.

Fn::Join

Ref

For more information about how to create an AWS CloudFormation template for DynamoDB resources, see the blog post How to use AWS CloudFormation to configure auto scaling for Amazon DynamoDB tables and indexes on the AWS Database Blog.

How to use AWS CloudFormation to configure auto scaling for Amazon DynamoDB tables and indexes

JSON

{
  "Resources" : {
    "WriteCapacityScalableTarget" : {
      "Type" : "AWS::ApplicationAutoScaling::ScalableTarget",
      "Properties" : {
        "MaxCapacity" : 15,
        "MinCapacity" : 5,
        "RoleARN" : { "Fn::Sub" : "arn:aws:iam::${AWS::AccountId}:role/aws-service-role/dynamodb.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_DynamoDBTable" },
        "ServiceNamespace" : "dynamodb",
        "ScalableDimension" : "dynamodb:table:WriteCapacityUnits",
        "ResourceId" : {
          "Fn::Join" : [
            "/",
            [
              "table",
              {
                "Ref" : "logicalName"
              }
            ]
          ]
        }
      }
    },
    "WriteScalingPolicy" : {
      "Type" : "AWS::ApplicationAutoScaling::ScalingPolicy",
      "Properties" : {
        "PolicyName" : "WriteScalingPolicy",
        "PolicyType" : "TargetTrackingScaling",
        "ScalingTargetId" : { "Ref" : "WriteCapacityScalableTarget" },
        "TargetTrackingScalingPolicyConfiguration" : {
          "TargetValue" : 50.0,
          "ScaleInCooldown" : 60,
          "ScaleOutCooldown" : 60,
          "PredefinedMetricSpecification" : {
            "PredefinedMetricType" : "DynamoDBWriteCapacityUtilization"
          }
        }
      }
    }
  }
}

YAML

---
Resources:
  WriteCapacityScalableTarget:
    Type: AWS::ApplicationAutoScaling::ScalableTarget
    Properties:
      MaxCapacity: 15
      MinCapacity: 5
      RoleARN: 
        Fn::Sub: 'arn:aws:iam::${AWS::AccountId}:role/aws-service-role/dynamodb.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_DynamoDBTable'
      ServiceNamespace: dynamodb
      ScalableDimension: dynamodb:table:WriteCapacityUnits
      ResourceId: !Join
        - /
        - - table
          - !Ref logicalName
  WriteScalingPolicy:
    Type: AWS::ApplicationAutoScaling::ScalingPolicy
    Properties:
      PolicyName: WriteScalingPolicy
      PolicyType: TargetTrackingScaling
      ScalingTargetId: !Ref WriteCapacityScalableTarget
      TargetTrackingScalingPolicyConfiguration:
        TargetValue: 50.0
        ScaleInCooldown: 60
        ScaleOutCooldown: 60
        PredefinedMetricSpecification:
          PredefinedMetricType: DynamoDBWriteCapacityUtilization

Declaring a scaling policy for an Amazon ECS service

This snippet shows how to create a policy and apply it to an AWS::ECS::Service resource using the AWS::ApplicationAutoScaling::ScalingPolicy resource. The AWS::ApplicationAutoScaling::ScalableTarget resource declares a scalable target to which this policy is applied. Application Auto Scaling can scale the number of tasks at a minimum of 1 task and a maximum of 2.

AWS::ECS::Service

AWS::ApplicationAutoScaling::ScalingPolicy

AWS::ApplicationAutoScaling::ScalableTarget

It creates two scaling policies with the `TargetTrackingScaling` policy type. The policies are used to scale the ECS service based on the service's average CPU and memory usage. It uses the Fn::Join and Ref intrinsic functions to construct the `ResourceId` property with the logical names of the AWS::ECS::Cluster (`myContainerCluster`) and AWS::ECS::Service (`myService`) resources that are specified in the same template.

Fn::Join

Ref

JSON

{
  "Resources" : {
    "ECSScalableTarget" : {
      "Type" : "AWS::ApplicationAutoScaling::ScalableTarget",
      "Properties" : {
        "MaxCapacity" : "2",
        "MinCapacity" : "1",
        "RoleARN" : { "Fn::Sub" : "arn:aws:iam::${AWS::AccountId}:role/aws-service-role/ecs.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_ECSService" },
        "ServiceNamespace" : "ecs",
        "ScalableDimension" : "ecs:service:DesiredCount",
        "ResourceId" : {
          "Fn::Join" : [
            "/",
            [
              "service",
              {
                "Ref" : "myContainerCluster"
              },
              {
                "Fn::GetAtt" : [
                  "myService",
                  "Name"
                ]
              }
            ]
          ]
        }
      }
    },
    "ServiceScalingPolicyCPU" : {
      "Type" : "AWS::ApplicationAutoScaling::ScalingPolicy",
      "Properties" : {
        "PolicyName" : { "Fn::Sub" : "${AWS::StackName}-target-tracking-cpu70" },
        "PolicyType" : "TargetTrackingScaling",
        "ScalingTargetId" : { "Ref" : "ECSScalableTarget" },
        "TargetTrackingScalingPolicyConfiguration" : {
          "TargetValue" : 70.0,
          "ScaleInCooldown" : 180,
          "ScaleOutCooldown" : 60,
          "PredefinedMetricSpecification" : {
            "PredefinedMetricType" : "ECSServiceAverageCPUUtilization"
          }
        }
      }
    },
    "ServiceScalingPolicyMem" : {
      "Type" : "AWS::ApplicationAutoScaling::ScalingPolicy",
      "Properties" : {
        "PolicyName" : { "Fn::Sub" : "${AWS::StackName}-target-tracking-mem90" },
        "PolicyType" : "TargetTrackingScaling",
        "ScalingTargetId" : { "Ref" : "ECSScalableTarget" },
        "TargetTrackingScalingPolicyConfiguration" : {
          "TargetValue" : 90.0,
          "ScaleInCooldown" : 180,
          "ScaleOutCooldown" : 60,
          "PredefinedMetricSpecification" : {
            "PredefinedMetricType" : "ECSServiceAverageMemoryUtilization"
          }
        }
      }
    }
  }
}

YAML

---
Resources:
  ECSScalableTarget:
    Type: AWS::ApplicationAutoScaling::ScalableTarget
    Properties:
      MaxCapacity: '2'
      MinCapacity: '1'  
      RoleARN: 
        Fn::Sub: 'arn:aws:iam::${AWS::AccountId}:role/aws-service-role/ecs.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_ECSService'
      ServiceNamespace: ecs
      ScalableDimension: 'ecs:service:DesiredCount'
      ResourceId: !Join 
        - /
        - - service
          - !Ref myContainerCluster
          - !GetAtt myService.Name
  ServiceScalingPolicyCPU:
    Type: AWS::ApplicationAutoScaling::ScalingPolicy
    Properties:
      PolicyName: !Sub ${AWS::StackName}-target-tracking-cpu70
      PolicyType: TargetTrackingScaling
      ScalingTargetId: !Ref ECSScalableTarget
      TargetTrackingScalingPolicyConfiguration:
        TargetValue: 70.0
        ScaleInCooldown: 180
        ScaleOutCooldown: 60
        PredefinedMetricSpecification:
          PredefinedMetricType: ECSServiceAverageCPUUtilization
  ServiceScalingPolicyMem:
    Type: AWS::ApplicationAutoScaling::ScalingPolicy
    Properties:
      PolicyName: !Sub ${AWS::StackName}-target-tracking-mem90
      PolicyType: TargetTrackingScaling
      ScalingTargetId: !Ref ECSScalableTarget
      TargetTrackingScalingPolicyConfiguration:
        TargetValue: 90.0
        ScaleInCooldown: 180
        ScaleOutCooldown: 60
        PredefinedMetricSpecification:
          PredefinedMetricType: ECSServiceAverageMemoryUtilization

The following example applies a target tracking scaling policy with the `ALBRequestCountPerTarget` predefined metric to an ECS service. The policy is used to add capacity to the ECS service when the request count per target (per minute) exceeds the target value. Because the value of `DisableScaleIn` is set to `true`, the target tracking policy won't remove capacity from the scalable target.

It uses the Fn::Join and Fn::GetAtt intrinsic functions to construct the `ResourceLabel` property with the logical names of the AWS::ElasticLoadBalancingV2::LoadBalancer (`myLoadBalancer`) and AWS::ElasticLoadBalancingV2::TargetGroup (`myTargetGroup`) resources that are specified in the same template.

Fn::Join

Fn::GetAtt

The `MaxCapacity` and `MinCapacity` properties of the scalable target and the `TargetValue` property of the scaling policy reference parameter values that you pass to the template when creating or updating a stack.

JSON

{
  "Resources" : {
    "ECSScalableTarget" : {
      "Type" : "AWS::ApplicationAutoScaling::ScalableTarget",
      "Properties" : {
        "MaxCapacity" : { "Ref" : "MaxCount" },
        "MinCapacity" : { "Ref" : "MinCount" },
        "RoleARN" : { "Fn::Sub" : "arn:aws:iam::${AWS::AccountId}:role/aws-service-role/ecs.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_ECSService" },
        "ServiceNamespace" : "ecs",
        "ScalableDimension" : "ecs:service:DesiredCount",
        "ResourceId" : {
          "Fn::Join" : [
            "/",
            [
              "service",
              {
                "Ref" : "myContainerCluster"
              },
              {
                "Fn::GetAtt" : [
                  "myService",
                  "Name"
                ]
              }
            ]
          ]
        }
      }
    },
    "ServiceScalingPolicyALB" : {
      "Type" : "AWS::ApplicationAutoScaling::ScalingPolicy",
      "Properties" : {
        "PolicyName" : "alb-requests-per-target-per-minute",
        "PolicyType" : "TargetTrackingScaling",
        "ScalingTargetId" : { "Ref" : "ECSScalableTarget" },
        "TargetTrackingScalingPolicyConfiguration" : {
          "TargetValue" : { "Ref" : "ALBPolicyTargetValue" },
          "ScaleInCooldown" : 180,
          "ScaleOutCooldown" : 30,
          "DisableScaleIn" : true,
          "PredefinedMetricSpecification" : {
            "PredefinedMetricType" : "ALBRequestCountPerTarget",
            "ResourceLabel" : {
              "Fn::Join" : [
                "/",
                [
                  {
                    "Fn::GetAtt" : [
                      "myLoadBalancer",
                      "LoadBalancerFullName"
                    ]
                  },
                  {
                    "Fn::GetAtt" : [
                      "myTargetGroup",
                      "TargetGroupFullName"
                    ]
                  }
                ]
              ]
            }
          }
        }
      }
    }
  }
}

YAML

---
Resources:
  ECSScalableTarget:
    Type: AWS::ApplicationAutoScaling::ScalableTarget
    Properties:
      MaxCapacity: !Ref MaxCount
      MinCapacity: !Ref MinCount  
      RoleARN: 
        Fn::Sub: 'arn:aws:iam::${AWS::AccountId}:role/aws-service-role/ecs.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_ECSService'
      ServiceNamespace: ecs
      ScalableDimension: 'ecs:service:DesiredCount'
      ResourceId: !Join 
        - /
        - - service
          - !Ref myContainerCluster
          - !GetAtt myService.Name
  ServiceScalingPolicyALB:
    Type: AWS::ApplicationAutoScaling::ScalingPolicy
    Properties:
      PolicyName: alb-requests-per-target-per-minute
      PolicyType: TargetTrackingScaling
      ScalingTargetId: !Ref ECSScalableTarget
      TargetTrackingScalingPolicyConfiguration:
        TargetValue: !Ref ALBPolicyTargetValue
        ScaleInCooldown: 180
        ScaleOutCooldown: 30
        DisableScaleIn: true
        PredefinedMetricSpecification:
          PredefinedMetricType: ALBRequestCountPerTarget
          ResourceLabel: !Join 
            - '/' 
            - - !GetAtt myLoadBalancer.LoadBalancerFullName
              - !GetAtt myTargetGroup.TargetGroupFullName

Declaring a scheduled action for a Lambda function

This snippet registers the provisioned concurrency for a function alias (AWS::Lambda::Alias) named `BLUE` using the AWS::ApplicationAutoScaling::ScalableTarget resource. It also creates a scheduled action with a recurring schedule using a cron expression.

AWS::Lambda::Alias

AWS::ApplicationAutoScaling::ScalableTarget

It uses the Fn::Join and Ref intrinsic functions in the `RoleARN` property to specify the ARN of the service-linked role. It uses the Fn::Sub intrinsic function to construct the `ResourceId` property with the logical name of the AWS::Lambda::Function or AWS::Serverless::Function resource that is specified in the same template.

Fn::Join

Ref

Fn::Sub

Note: You can't allocate provisioned concurrency on an alias that points to the unpublished version ($LATEST).

For more information about how to create an AWS CloudFormation template for Lambda resources, see the blog post Scheduling AWS Lambda Provisioned Concurrency for recurring peak usage on the AWS Compute Blog.

Scheduling AWS Lambda Provisioned Concurrency for recurring peak usage

JSON

{
  "ScalableTarget" : {
    "Type" : "AWS::ApplicationAutoScaling::ScalableTarget",
    "Properties" : {
      "MaxCapacity" : 0,
      "MinCapacity" : 0,
      "RoleARN" : {
        "Fn::Join" : [
          ":",
          [
            "arn:aws:iam:",
            {
              "Ref" : "AWS::AccountId"
            },
            "role/aws-service-role/lambda.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_LambdaConcurrency"
          ]
        ]
      },
      "ServiceNamespace" : "lambda",
      "ScalableDimension" : "lambda:function:ProvisionedConcurrency",
      "ResourceId" : { "Fn::Sub" : "function:${logicalName}:BLUE" },
      "ScheduledActions" : [
        {
          "EndTime" : "2020-12-31T12:00:00.000Z",
          "ScalableTargetAction" : {
            "MaxCapacity" : "500",
            "MinCapacity" : "50"
          },
          "ScheduledActionName" : "First",
          "Schedule" : "cron(0 18 * * ? *)"
        }
      ]
    }
  }
}

YAML

ScalableTarget:
  Type: AWS::ApplicationAutoScaling::ScalableTarget
  Properties:
    MaxCapacity: 0
    MinCapacity: 0
    RoleARN: !Join 
      - ':'
      - - 'arn:aws:iam:'
        - !Ref 'AWS::AccountId'
        - role/aws-service-role/lambda.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_LambdaConcurrency
    ServiceNamespace: lambda
    ScalableDimension: lambda:function:ProvisionedConcurrency
    ResourceId: !Sub function:${logicalName}:BLUE
    ScheduledActions:
      - EndTime: '2020-12-31T12:00:00.000Z'
        ScalableTargetAction:
          MaxCapacity: '500'
          MinCapacity: '50'
        ScheduledActionName: First
        Schedule: 'cron(0 18 * * ? *)'

Declaring a scheduled action for a Spot Fleet

This snippet shows how to create a scheduled action and apply it to an AWS::EC2::SpotFleet resource using the AWS::ApplicationAutoScaling::ScalableTarget resource. It uses the Fn::Join and Ref intrinsic functions to construct the `ResourceId` property with the logical name of the AWS::EC2::SpotFleet resource that is specified in the same template.

AWS::EC2::SpotFleet

AWS::ApplicationAutoScaling::ScalableTarget

Fn::Join

Ref

Note: The Spot Fleet request must have a request type of `maintain`. Automatic scaling is not supported for one-time requests or Spot blocks.

JSON

{
  "Resources" : {
    "SpotFleetScalableTarget" : {
      "Type" : "AWS::ApplicationAutoScaling::ScalableTarget",
      "Properties" : {
        "MaxCapacity" : 0,
        "MinCapacity" : 0,
        "RoleARN" : { "Fn::Sub" : "arn:aws:iam::${AWS::AccountId}:role/aws-service-role/ec2.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_EC2SpotFleetRequest" },
        "ServiceNamespace" : "ec2",
        "ScalableDimension" : "ec2:spot-fleet-request:TargetCapacity",
        "ResourceId" : {
          "Fn::Join" : [
            "/",
            [
              "spot-fleet-request",
              {
                "Ref" : "logicalName"
              }
            ]
          ]
        },
        "ScheduledActions" : [
          {
            "EndTime" : "2020-12-31T12:00:00.000Z",
            "ScalableTargetAction" : {
              "MaxCapacity" : "20",
              "MinCapacity" : "5"
            },
            "ScheduledActionName" : "First",
            "Schedule" : "cron(0 18 * * ? *)"
          }
        ]
      }
    }
  }
}

YAML

---
Resources:
  SpotFleetScalableTarget:
    Type: AWS::ApplicationAutoScaling::ScalableTarget
    Properties:
      MaxCapacity: 0
      MinCapacity: 0
      RoleARN: 
        Fn::Sub: 'arn:aws:iam::${AWS::AccountId}:role/aws-service-role/ec2.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_EC2SpotFleetRequest'
      ServiceNamespace: ec2
      ScalableDimension: 'ec2:spot-fleet-request:TargetCapacity'
      ResourceId: !Join 
        - /
        - - spot-fleet-request
          - !Ref logicalName
      ScheduledActions:
        - EndTime: '2020-12-31T12:00:00.000Z'
          ScalableTargetAction:
            MaxCapacity: '20'
            MinCapacity: '5'
          ScheduledActionName: First
          Schedule: 'cron(0 18 * * ? *)'