AWS CloudFormation
User Guide (Version )

AWS::ApplicationAutoScaling::ScalingPolicy

The AWS::ApplicationAutoScaling::ScalingPolicy resource defines a scaling policy that Application Auto Scaling uses to adjust your application resources.

For more information, see PutScalingPolicy in the Application Auto Scaling API Reference. For more information about scaling policies, see the Application Auto Scaling User Guide.

Syntax

To declare this entity in your AWS CloudFormation template, use the following syntax:

JSON

{ "Type" : "AWS::ApplicationAutoScaling::ScalingPolicy", "Properties" : { "PolicyName" : String, "PolicyType" : String, "ResourceId" : String, "ScalableDimension" : String, "ScalingTargetId" : String, "ServiceNamespace" : String, "StepScalingPolicyConfiguration" : StepScalingPolicyConfiguration, "TargetTrackingScalingPolicyConfiguration" : TargetTrackingScalingPolicyConfiguration } }

Properties

PolicyName

The name of the scaling policy.

Required: Yes

Type: String

Minimum: 1

Maximum: 256

Pattern: \p{Print}+

Update requires: Replacement

PolicyType

The Application Auto Scaling policy type.

The following policy types are supported:

TargetTrackingScaling—Not supported for Amazon EMR

StepScaling—Not supported for DynamoDB, Comprehend, or Lambda

Required: Yes

Type: String

Update requires: No interruption

ResourceId

The unique resource identifier for the scalable target that this scaling policy applies to. For valid values, see the ResourceId parameter for PutScalingPolicy in the Application Auto Scaling API Reference.

You must specify either the ResourceId, ScalableDimension, and ServiceNamespace properties, or the ScalingTargetId property, but not both.

Required: Conditional

Type: String

Update requires: Replacement

ScalableDimension

The scalable dimension of the scalable target that this scaling policy applies to. The scalable dimension contains the service namespace, resource type, and scaling property, such as ecs:service:DesiredCount for the desired task count of an Amazon ECS service. For valid values, see the ScalableDimension parameter for PutScalingPolicy in the Application Auto Scaling API Reference.

You must specify either the ResourceId, ScalableDimension, and ServiceNamespace properties, or the ScalingTargetId property, but not both.

Required: Conditional

Type: String

Update requires: Replacement

ScalingTargetId

The AWS CloudFormation-generated ID of an Application Auto Scaling scalable target. For more information about the ID, see the Return Value section of the AWS::ApplicationAutoScaling::ScalableTarget resource.

You must specify either the ScalingTargetId property, or the ResourceId, ScalableDimension, and ServiceNamespace properties, but not both.

Required: Conditional

Type: String

Update requires: Replacement

ServiceNamespace

The namespace of the AWS service that provides the resource or custom-resource for a resource provided by your own application or service. For valid values, see the ServiceNamespace parameter for PutScalingPolicy in the Application Auto Scaling API Reference.

You must specify either the ResourceId, ScalableDimension, and ServiceNamespace properties, or the ScalingTargetId property, but not both.

Required: Conditional

Type: String

Update requires: Replacement

StepScalingPolicyConfiguration

A step scaling policy.

Required: No

Type: StepScalingPolicyConfiguration

Update requires: No interruption

TargetTrackingScalingPolicyConfiguration

A target tracking scaling policy.

Required: No

Type: TargetTrackingScalingPolicyConfiguration

Update requires: No interruption

Return Values

Ref

When the logical ID of this resource is provided to the Ref intrinsic function, Ref returns the Application Auto Scaling scaling policy Amazon Resource Name (ARN). For example: arn:aws:autoscaling:us-east-2:123456789012:scalingPolicy:12ab3c4d-56789-0ef1-2345-6ghi7jk8lm90:resource/ecs/service/ecsStack-MyECSCluster-AB12CDE3F4GH/ecsStack-MyECSService-AB12CDE3F4GH:policyName/MyStepPolicy.

For more information about using the Ref function, see Ref.

Examples

The following examples create scaling policies for a scalable target that is registered with Application Auto Scaling. For more information, see PutScalingPolicy in the Application Auto Scaling API Reference.

Target Tracking Scaling Policy with a Predefined Metric Specification for an Amazon DynamoDB Table

This example both registers an AWS::DynamoDB::Table as a scalable target and defines a TargetTrackingScaling scaling policy that scales the table's write capacity (throughput) to maintain the target utilization (50%) based on the write capacity utilization metric.

JSON

{ "Resources":{ "DDBTable":{ "Type":"AWS::DynamoDB::Table", "Properties":{ "AttributeDefinitions":[ { "AttributeName":"ArtistId", "AttributeType":"S" }, { "AttributeName":"Concert", "AttributeType":"S" }, { "AttributeName":"TicketSales", "AttributeType":"S" } ], "KeySchema":[ { "AttributeName":"ArtistId", "KeyType":"HASH" }, { "AttributeName":"Concert", "KeyType":"RANGE" } ], "GlobalSecondaryIndexes":[ { "IndexName":"GSI", "KeySchema":[ { "AttributeName":"TicketSales", "KeyType":"HASH" } ], "Projection":{ "ProjectionType":"KEYS_ONLY" }, "ProvisionedThroughput":{ "ReadCapacityUnits":5, "WriteCapacityUnits":5 } } ], "ProvisionedThroughput":{ "ReadCapacityUnits":5, "WriteCapacityUnits":5 } } }, "WriteCapacityScalableTarget":{ "Type":"AWS::ApplicationAutoScaling::ScalableTarget", "Properties":{ "MaxCapacity":15, "MinCapacity":5, "RoleARN":"arn:aws:iam::012345678910:role/aws-service-role/dynamodb.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_DynamoDBTable", "ServiceNamespace":"dynamodb", "ScalableDimension":"dynamodb:table:WriteCapacityUnits", "ResourceId":{ "Fn::Join":[ "/", [ "table", { "Ref":"DDBTable" } ] ] } } }, "WriteScalingPolicy":{ "Type":"AWS::ApplicationAutoScaling::ScalingPolicy", "Properties":{ "PolicyName":"WriteAutoScalingPolicy", "PolicyType":"TargetTrackingScaling", "ScalingTargetId":{ "Ref":"WriteCapacityScalableTarget" }, "TargetTrackingScalingPolicyConfiguration":{ "TargetValue":50.0, "ScaleInCooldown":60, "ScaleOutCooldown":60, "PredefinedMetricSpecification":{ "PredefinedMetricType":"DynamoDBWriteCapacityUtilization" } } } } } }

YAML

Resources: DDBTable: Type: AWS::DynamoDB::Table Properties: AttributeDefinitions: - AttributeName: "ArtistId" AttributeType: "S" - AttributeName: "Concert" AttributeType: "S" - AttributeName: "TicketSales" AttributeType: "S" KeySchema: - AttributeName: "ArtistId" KeyType: "HASH" - AttributeName: "Concert" KeyType: "RANGE" GlobalSecondaryIndexes: - IndexName: "GSI" KeySchema: - AttributeName: "TicketSales" KeyType: "HASH" Projection: ProjectionType: "KEYS_ONLY" ProvisionedThroughput: ReadCapacityUnits: 5 WriteCapacityUnits: 5 ProvisionedThroughput: ReadCapacityUnits: 5 WriteCapacityUnits: 5 WriteCapacityScalableTarget: Type: AWS::ApplicationAutoScaling::ScalableTarget Properties: MaxCapacity: 15 MinCapacity: 5 RoleARN: arn:aws:iam::012345678910:role/aws-service-role/dynamodb.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_DynamoDBTable ServiceNamespace: dynamodb ScalableDimension: dynamodb:table:WriteCapacityUnits ResourceId: !Join - / - - table - !Ref DDBTable WriteScalingPolicy: Type: AWS::ApplicationAutoScaling::ScalingPolicy Properties: PolicyName: WriteAutoScalingPolicy PolicyType: TargetTrackingScaling ScalingTargetId: !Ref WriteCapacityScalableTarget TargetTrackingScalingPolicyConfiguration: TargetValue: 50.0 ScaleInCooldown: 60 ScaleOutCooldown: 60 PredefinedMetricSpecification: PredefinedMetricType: DynamoDBWriteCapacityUtilization

Target Tracking Scaling Policy for Scale Out Only for an ECS Service

The following example both registers an AWS::ECS::Service as a scalable target and defines a TargetTrackingScaling scaling policy. The policy is used to add capacity to the ECS service when the average CPU utilization metric exceeds the target utilization. Because the value of DisableScaleIn is set to true, the target tracking policy won't remove capacity from the scalable resource.

JSON

{ "AWSTemplateFormatVersion":"2010-09-09", "Description":"Creating ECS service", "Parameters":{ "AppName":{ "Type":"String", "Description":"Name of app requiring ELB exposure", "Default":"simple-app" }, "AppContainerPort":{ "Type":"Number", "Description":"Container port of app requiring ELB exposure", "Default":"80" }, "AppHostPort":{ "Type":"Number", "Description":"Host port of app requiring ELB exposure", "Default":"80" } }, "Resources":{ "ScalableTarget":{ "Type":"AWS::ApplicationAutoScaling::ScalableTarget", "Properties":{ "MaxCapacity":"2", "MinCapacity":"1", "RoleARN":"arn:aws:iam::012345678910:role/aws-service-role/ecs.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_ECSService", "ServiceNamespace":"ecs", "ScalableDimension":"ecs:service:DesiredCount", "ResourceId":{ "Fn::Join":[ "/", [ "service", { "Ref":"cluster" }, { "Fn::GetAtt":[ "service", "Name" ] } ] ] } } }, "TargetTrackingScalingPolicy":{ "Type":"AWS::ApplicationAutoScaling::ScalingPolicy", "Properties":{ "PolicyName":"TargetTrackingScalingPolicy", "PolicyType":"TargetTrackingScaling", "ScalingTargetId":{ "Ref":"ScalableTarget" }, "TargetTrackingScalingPolicyConfiguration":{ "TargetValue":70.0, "ScaleInCooldown":60, "ScaleOutCooldown":60, "DisableScaleIn":true, "PredefinedMetricSpecification":{ "PredefinedMetricType":"ECSServiceAverageCPUUtilization" } } } }, "cluster":{ "Type":"AWS::ECS::Cluster" }, "taskdefinition":{ "Type":"AWS::ECS::TaskDefinition", "Properties":{ "ContainerDefinitions":[ { "Name":{ "Ref":"AppName" }, "MountPoints":[ { "SourceVolume":"my-vol", "ContainerPath":"/var/www/my-vol" } ], "Image":"amazon/amazon-ecs-sample", "Cpu":"10", "PortMappings":[ { "ContainerPort":{ "Ref":"AppContainerPort" }, "HostPort":{ "Ref":"AppHostPort" } } ], "EntryPoint":[ "/usr/sbin/apache2", "-D", "FOREGROUND" ], "Memory":"500", "Essential":"true" }, { "Name":"busybox", "Image":"busybox", "Cpu":"10", "EntryPoint":[ "sh", "-c" ], "Memory":"500", "Command":[ "/bin/sh -c \"while true; do /bin/date > /var/www/my-vol/date; sleep 1; done\"" ], "Essential":"false", "VolumesFrom":[ { "SourceContainer":{ "Ref":"AppName" } } ] } ], "Volumes":[ { "Host":{ "SourcePath":"/var/lib/docker/vfs/dir/" }, "Name":"my-vol" } ] } }, "service":{ "Type":"AWS::ECS::Service", "Properties":{ "Cluster":{ "Ref":"cluster" }, "DesiredCount":0, "TaskDefinition":{ "Ref":"taskdefinition" } } } }, "Outputs":{ "resourceId":{ "Description":"ResourceId", "Value":{ "Fn::Join":[ "/", [ "service", { "Ref":"cluster" }, { "Fn::GetAtt":[ "service", "Name" ] } ] ] } } } }

YAML

AWSTemplateFormatVersion: 2010-09-09 Description: Creating ECS service Parameters: AppName: Type: String Description: Name of app requiring ELB exposure Default: simple-app AppContainerPort: Type: Number Description: Container port of app requiring ELB exposure Default: '80' AppHostPort: Type: Number Description: Host port of app requiring ELB exposure Default: '80' Resources: ScalableTarget: Type: AWS::ApplicationAutoScaling::ScalableTarget Properties: MaxCapacity: '2' MinCapacity: '1' RoleARN: arn:aws:iam::012345678910:role/aws-service-role/ecs.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_ECSService ServiceNamespace: ecs ScalableDimension: 'ecs:service:DesiredCount' ResourceId: !Join - / - - service - !Ref cluster - !GetAtt service.Name TargetTrackingScalingPolicy: Type: AWS::ApplicationAutoScaling::ScalingPolicy Properties: PolicyName: TargetTrackingScalingPolicy PolicyType: TargetTrackingScaling ScalingTargetId: Ref: ScalableTarget TargetTrackingScalingPolicyConfiguration: TargetValue: 70.0 ScaleInCooldown: 60 ScaleOutCooldown: 60 DisableScaleIn: true PredefinedMetricSpecification: PredefinedMetricType: ECSServiceAverageCPUUtilization cluster: Type: AWS::ECS::Cluster taskdefinition: Type: AWS::ECS::TaskDefinition Properties: ContainerDefinitions: - Name: !Ref AppName MountPoints: - SourceVolume: my-vol ContainerPath: /var/www/my-vol Image: amazon/amazon-ecs-sample Cpu: '10' PortMappings: - ContainerPort: !Ref AppContainerPort HostPort: !Ref AppHostPort EntryPoint: - /usr/sbin/apache2 - '-D' - FOREGROUND Memory: '500' Essential: 'true' - Name: busybox Image: busybox Cpu: '10' EntryPoint: - sh - '-c' Memory: '500' Command: - >- /bin/sh -c "while true; do /bin/date > /var/www/my-vol/date; sleep 1; done" Essential: 'false' VolumesFrom: - SourceContainer: !Ref AppName Volumes: - Host: SourcePath: /var/lib/docker/vfs/dir/ Name: my-vol service: Type: AWS::ECS::Service Properties: Cluster: !Ref cluster DesiredCount: 0 TaskDefinition: !Ref taskdefinition Outputs: resourceId: Description: ResourceId Value: !Join - / - - service - !Ref cluster - !GetAtt service.Name

See Also