AWS CloudFormation
User Guide (API 版本 2010-05-15)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 Amazon AWS 入门

AWS::ApplicationAutoScaling::ScalableTarget

AWS::ApplicationAutoScaling::ScalableTarget 资源指定 Application Auto Scaling 可向上扩展或向下扩展的资源。有关更多信息,请参阅 Application Auto Scaling API 参考 中的 RegisterScalableTarget 操作。

对与 AWS::ApplicationAutoScaling::ScalableTarget 资源关联的 AWS::DynamoDB::Table 资源进行更新将始终导致更新失败,然后更新回滚失败。以下 ScalableDimension 属性在与该表关联时将导致此问题:

  • dynamodb:table:ReadCapacityUnits

  • dynamodb:table:WriteCapacityUnits

  • dynamodb:index:ReadCapacityUnits

  • dynamodb:index:WriteCapacityUnits

作为解决办法,请在更新 AWS::DynamoDB::Table 资源之前取消注册可扩展目标。

语法

要在 AWS CloudFormation 模板中声明此实体,请使用以下语法:

JSON

{ "Type" : "AWS::ApplicationAutoScaling::ScalableTarget", "Properties" : { "MaxCapacity" : Integer, "MinCapacity" : Integer, "ResourceId" : String, "RoleARN" : String, "ScalableDimension" : String, "ScheduledActions" : [ ScheduledAction, ... ], "ServiceNamespace" : String } }

YAML

Type: "AWS::ApplicationAutoScaling::ScalableTarget" Properties: MaxCapacity: Integer MinCapacity: Integer ResourceId: String RoleARN: String ScalableDimension: String ScheduledActions: - ScheduledAction ServiceNamespace: String

属性

MaxCapacity

Application Auto Scaling 可在扩展活动期间用来扩展目标的最大值。

Required: Yes

Type: Integer

更新要求无需中断

MinCapacity

Application Auto Scaling 可在扩展活动期间用来扩展目标的最小值。

Required: Yes

Type: Integer

更新要求无需中断

ResourceId

要与此可扩展目标关联的资源标识符。该字符串由资源类型和唯一标识符组成。有关更多信息,请参阅 Application Auto Scaling API 参考 中的 RegisterScalableTarget 操作的 ResourceId 参数,或查看ScalableTarget示例

Required: Yes

Type: String

更新要求替换

RoleARN

允许 Application Auto Scaling 修改您的可扩展目标的 AWS Identity and Access Management (IAM) 角色的 Amazon 资源名称 (ARN)。

Required: Yes

Type: String

更新要求无需中断

ScalableDimension

与可扩展目标关联的可扩展维度。指定服务命名空间、资源类型和扩展属性 - 例如,Amazon Elastic Container Service 服务的所需任务计数的 ecs:service:DesiredCount。对于有效值,请参阅 Application Auto Scaling API 参考 中的 ScalingPolicy 数据类型的 ScalableDimension 内容。

Required: Yes

Type: String

更新要求替换

ScheduledActions

可扩展目标的计划操作。不允许重复项。

必需:否

类型Application Auto Scaling ScalableTarget ScheduledAction 属性类型的列表

更新要求无需中断

ServiceNamespace

可扩展目标的 AWS 服务命名空间。有关服务命名空间的列表,请参阅 AWS General Reference 中的 AWS 服务命名空间

Required: Yes

Type: String

更新要求替换

返回值

Ref

当向 Ref 内部函数提供此资源的逻辑 ID 时,Ref 将返回此资源的 AWS CloudFormation 生成的 ID,如 service/ecsStack-MyECSCluster-AB12CDE3F4GH/ecsStack-MyECSService-AB12CDE3F4GH|ecs:service:DesiredCount|ecs

AWS CloudFormation 使用以下格式生成 ID:service/resource_ID|scalable_dimension|service_namespace

有关使用 Ref 功能的更多信息,请参阅参考

示例

任务数

以下示例将为 Amazon Elastic Container Service 服务创建可扩展目标。Application Auto Scaling 将扩展任务的数量,最少 1 个任务,最多 2 个任务。

JSON

"scalableTarget" : { "Type" : "AWS::ApplicationAutoScaling::ScalableTarget", "Properties" : { "MaxCapacity" : 2, "MinCapacity" : 1, "ResourceId" : "service/ecsStack-MyECSCluster-AB12CDE3F4GH/ecsStack-MyECSService-AB12CDE3F4GH", "RoleARN" : {"Fn::GetAtt" : ["ApplicationAutoScalingRole", "Arn"] }, "ScalableDimension" : "ecs:service:DesiredCount", "ServiceNamespace" : "ecs" } }

YAML

scalableTarget: Type: AWS::ApplicationAutoScaling::ScalableTarget Properties: MaxCapacity: 2 MinCapacity: 1 ResourceId: service/ecsStack-MyECSCluster-AB12CDE3F4GH/ecsStack-MyECSService-AB12CDE3F4GH RoleARN: !GetAtt [ ApplicationAutoScalingRole, Arn ] ScalableDimension: ecs:service:DesiredCount ServiceNamespace: ecs

使用 Fn::JoinRef 构建 ResourceId

以下示例使用 Fn::JoinRef 内部函数来构造扩展目标的 ResourceId 属性。

JSON

"SpotFleetScalingTarget": { "Type": "AWS::ApplicationAutoScaling::ScalableTarget", "Properties": { "MaxCapacity": 2, "MinCapacity": 1, "ResourceId": { "Fn::Join": [ "/", [ "spot-fleet-request", { "Ref": "ECSSpotFleet" } ] ] }, "RoleARN": { "Fn::GetAtt": [ "AutoScalingRole", "Arn" ] }, "ScalableDimension": "ec2:spot-fleet-request:TargetCapacity", "ServiceNamespace": "ec2" } }

YAML

SpotFleetScalingTarget: Type: 'AWS::ApplicationAutoScaling::ScalableTarget' Properties: MaxCapacity: 2 MinCapacity: 1 ResourceId: !Join - / - - spot-fleet-request - !Ref ECSSpotFleet RoleARN: !GetAtt - AutoScalingRole - Arn ScalableDimension: 'ec2:spot-fleet-request:TargetCapacity' ServiceNamespace: ec2

带 Amazon DynamoDB 表的 Application Auto Scaling 可扩展目标

此示例为 AWS::DynamoDB::Table 资源设置 Application Auto Scaling。此模板定义扩展表的 WriteCapacityUnits 吞吐量的 TargetTrackingScaling 扩展策略。

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, "ResourceId": { "Fn::Join": [ "/", [ "table", { "Ref": "DDBTable" } ] ] }, "RoleARN": { "Fn::GetAtt": ["ScalingRole", "Arn"] }, "ScalableDimension": "dynamodb:table:WriteCapacityUnits", "ServiceNamespace": "dynamodb" } }, "ScalingRole": { "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": [ "application-autoscaling.amazonaws.com" ] }, "Action": [ "sts:AssumeRole" ] } ] }, "Path": "/", "Policies": [ { "PolicyName": "root", "PolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "dynamodb:DescribeTable", "dynamodb:UpdateTable", "cloudwatch:PutMetricAlarm", "cloudwatch:DescribeAlarms", "cloudwatch:GetMetricStatistics", "cloudwatch:SetAlarmState", "cloudwatch:DeleteAlarms" ], "Resource": "*" } ] } } ] } }, "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 ResourceId: !Join - / - - table - !Ref DDBTable RoleARN: !GetAtt ScalingRole.Arn ScalableDimension: dynamodb:table:WriteCapacityUnits ServiceNamespace: dynamodb ScalingRole: Type: "AWS::IAM::Role" Properties: AssumeRolePolicyDocument: Version: "2012-10-17" Statement: - Effect: "Allow" Principal: Service: - application-autoscaling.amazonaws.com Action: - "sts:AssumeRole" Path: "/" Policies: - PolicyName: "root" PolicyDocument: Version: "2012-10-17" Statement: - Effect: "Allow" Action: - "dynamodb:DescribeTable" - "dynamodb:UpdateTable" - "cloudwatch:PutMetricAlarm" - "cloudwatch:DescribeAlarms" - "cloudwatch:GetMetricStatistics" - "cloudwatch:SetAlarmState" - "cloudwatch:DeleteAlarms" Resource: "*" WriteScalingPolicy: Type: "AWS::ApplicationAutoScaling::ScalingPolicy" Properties: PolicyName: WriteAutoScalingPolicy PolicyType: TargetTrackingScaling ScalingTargetId: !Ref WriteCapacityScalableTarget TargetTrackingScalingPolicyConfiguration: TargetValue: 50.0 ScaleInCooldown: 60 ScaleOutCooldown: 60 PredefinedMetricSpecification: PredefinedMetricType: DynamoDBWriteCapacityUtilization

计划的操作

以下示例为目标创建一个计划操作。

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": { "ResourceId": { "Fn::Join": [ "/", [ "service", { "Ref": "cluster" }, { "Fn::GetAtt": [ "service", "Name" ] } ] ] }, "ServiceNamespace": "ecs", "ScalableDimension": "ecs:service:DesiredCount", "RoleARN": { "Fn::GetAtt": [ "scalingRole", "Arn" ] }, "MaxCapacity": "2", "MinCapacity": "1", "ScheduledActions": [{ "EndTime": "2018-12-04T22:14:41.951Z", "ScalableTargetAction": { "MaxCapacity": "2", "MinCapacity": "1" }, "ScheduledActionName": "First", "StartTime": "2018-11-28T22:14:41.951Z", "Schedule": "cron(0 0 12 ? * MON *)" } ] } }, "scalingRole": { "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": ["application-autoscaling.amazonaws.com"] }, "Action": [ "sts:AssumeRole" ] } ] }, "Path": "/", "Policies": [ { "PolicyName": "root", "PolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "*", "Resource": "*" } ] } } ] } }, "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: ResourceId: !Join - / - - service - !Ref cluster - !GetAtt service.Name ServiceNamespace: ecs ScalableDimension: 'ecs:service:DesiredCount' RoleARN: !GetAtt - scalingRole - Arn MaxCapacity: '2' MinCapacity: '1' ScheduledActions: - EndTime: '2018-12-04T22:14:41.951Z' ScalableTargetAction: MaxCapacity: '2' MinCapacity: '1' ScheduledActionName: First StartTime: '2018-11-28T22:14:41.951Z' Schedule: cron(0 0 12 ? * MON *) scalingRole: Type: 'AWS::IAM::Role' Properties: AssumeRolePolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Principal: Service: - application-autoscaling.amazonaws.com Action: - 'sts:AssumeRole' Path: / Policies: - PolicyName: root PolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Action: '*' Resource: '*' 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

本页内容: