Auto Scaling 模板代码段 - Amazon CloudFormation
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

Auto Scaling 模板代码段

以下示例演示了要包含在模板中的不同代码段,以便与 Amazon EC2 Auto Scaling 或 Application Auto Scaling 一起使用。

Amazon EC2 Auto Scaling 使您能够通过扩展策略或计划扩展自动扩展 Amazon EC2 实例。Auto Scaling 组是 Amazon EC2 实例的集合,这些实例支持自动扩展和队列管理功能,例如运行状况检查以及与 Elastic Load Balancing 的集成。

Application Auto Scaling 通过扩展策略或计划扩展,可以自动扩展 Amazon EC2 以外的其他资源。

声明启动配置

此示例显示了 Auto Scaling 组的 AWS::AutoScaling::LaunchConfiguration 资源,您可以在其中指定 ImageIdInstanceTypeSecurityGroups 属性的值。SecurityGroups 属性指定在模板中指定的 AWS::EC2::SecurityGroup 资源的逻辑名称,以及名为 myExistingEC2SecurityGroup 的现有 EC2 安全组。

JSON

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

YAML

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

声明单实例 Auto Scaling 组

此示例显示了具有单个实例的 AWS::AutoScaling::AutoScalingGroup 资源,以帮助您开始操作。Auto Scaling 组的 VPCZoneIdentifier 属性指定不同可用区中的一系列现有子网。在创建堆栈之前,必须先从您的账户指定适用的子网 ID。LaunchConfigurationName 属性引用具有在模板中定义的逻辑名称 mySimpleConfigAWS::AutoScaling::LaunchConfiguration 资源。

JSON

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

YAML

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

声明用于实现负载均衡的 Auto Scaling 组

此示例显示了用于在多个服务器之间实现负载均衡的 AWS::AutoScaling::AutoScalingGroup 资源。它指定在同一模板中的其他位置声明的 Amazon 资源的逻辑名称。

  1. VPCZoneIdentifier 属性指定将在其中创建 Auto Scaling 组的 EC2 实例的两个 AWS::EC2::Subnet 资源的逻辑名称:myPublicSubnet1myPublicSubnet2

  2. LaunchConfigurationName 属性指定具有逻辑名称 mySimpleConfigAWS::AutoScaling::LaunchConfiguration 资源。

  3. TargetGroupARNs 属性列出用于将流量路由到 Auto Scaling 组的应用程序负载均衡器或Network Load Balancer的目标组。在此示例中,指定了一个目标组,即具有逻辑名称 myTargetGroupAWS::ElasticLoadBalancingV2::TargetGroup 资源。

JSON

"myServerGroup" : { "Type" : "AWS::AutoScaling::AutoScalingGroup", "Properties" : { "VPCZoneIdentifier" : [ { "Ref" : "myPublicSubnet1" }, { "Ref" : "myPublicSubnet2" } ], "LaunchConfigurationName" : { "Ref" : "mySimpleConfig" }, "MinSize" : "1", "MaxSize" : "5", "HealthCheckGracePeriod" : 300, "MaxInstanceLifetime" : 2592000, "TargetGroupARNs" : [ { "Ref" : "myTargetGroup" } ] } }

YAML

myServerGroup: Type: AWS::AutoScaling::AutoScalingGroup Properties: VPCZoneIdentifier: - !Ref myPublicSubnet1 - !Ref myPublicSubnet2 LaunchConfigurationName: !Ref mySimpleConfig MinSize: '1' MaxSize: '5' HealthCheckGracePeriod: 300 MaxInstanceLifetime: 2592000 TargetGroupARNs: - !Ref myTargetGroup

另请参阅

有关根据 ALBRequestCountPerTarget 预定义指标为 应用程序负载均衡器 创建具有目标追踪扩展策略的 Auto Scaling 组的详细示例,请参见 AWS::AutoScaling::ScalingPolicy 资源中的示例部分。

使用 CloudWatch 警报声明扩展策略

此示例显示了使用简单扩展策略扩展 Auto Scaling 组的 AWS::AutoScaling::ScalingPolicy 资源。AdjustmentType 属性指定 ChangeInCapacity,这表示 ScalingAdjustment 代表待添加(如果 ScalingAdjustment 为正值)或待删除(如果它为负值)实例的数量。在此示例中,ScalingAdjustment 为 1;因此,当超出警报阈值时,策略会将组中 EC2 实例的数量增加 1。

AWS::CloudWatch::Alarm 资源 CPUAlarmHigh 指定扩展策略 myScaleOutPolicy 作为警报处于 ALARM 状态时要运行的操作 (AlarmActions)。

JSON

"myScaleOutPolicy" : { "Type" : "AWS::AutoScaling::ScalingPolicy", "Properties" : { "AdjustmentType" : "ChangeInCapacity", "AutoScalingGroupName" : { "Ref" : "logicalName" }, "ScalingAdjustment" : "1" } }, "CPUAlarmHigh" : { "Type" : "AWS::CloudWatch::Alarm", "Properties" : { "EvaluationPeriods" : "1", "Statistic" : "Average", "Threshold" : "10", "AlarmDescription" : "Alarm if CPU too high or metric disappears indicating instance is down", "Period" : "60", "AlarmActions" : [ { "Ref" : "myScaleOutPolicy" } ], "Namespace" : "AWS/EC2", "Dimensions" : [ { "Name" : "AutoScalingGroupName", "Value" : { "Ref" : "logicalName" } } ], "ComparisonOperator" : "GreaterThanThreshold", "MetricName" : "CPUUtilization" } }

YAML

myScaleOutPolicy: Type: AWS::AutoScaling::ScalingPolicy Properties: AdjustmentType: ChangeInCapacity AutoScalingGroupName: !Ref logicalName ScalingAdjustment: '1' CPUAlarmHigh: Type: AWS::CloudWatch::Alarm Properties: EvaluationPeriods: '1' Statistic: Average Threshold: '10' AlarmDescription: Alarm if CPU too high or metric disappears indicating instance is down Period: '60' AlarmActions: - !Ref myScaleOutPolicy Namespace: AWS/EC2 Dimensions: - Name: AutoScalingGroupName Value: Ref: logicalName ComparisonOperator: GreaterThanThreshold MetricName: CPUUtilization

另请参阅

有关 TargetTrackingScalingStepScaling 策略类型的示例模板,请参阅 AWS::AutoScaling::ScalingPolicy 资源中的示例部分。

声明具有启动模板和通知的 Auto Scaling 组

此示例显示的是在指定事件发生时发送 Amazon SNS 通知的 AWS::AutoScaling::AutoScalingGroup 资源。NotificationConfigurations 属性指定 Amazon CloudFormation 在其中发送通知的 SNS 主题和将导致 Amazon CloudFormation 发送通知的事件。当 NotificationTypes 指定的事件发生时,Amazon CloudFormation 会发送一条通知至 TopicARN 指定的 SNS 主题中。启动堆栈时,Amazon CloudFormation 会创建在同一模板中声明的 AWS::SNS::Subscription 资源 (snsTopicForAutoScalingGroup)。

Auto Scaling 组的 AvailabilityZonesVPCZoneIdentifier 属性引用您在创建或更新堆栈时传递给模板的参数值。LaunchTemplate 属性引用在同一模板中的其他位置声明的 AWS::EC2::LaunchTemplate 资源的逻辑名称。

JSON

"myASG" : { "Type" : "AWS::AutoScaling::AutoScalingGroup", "DependsOn": [ "snsTopicForAutoScalingGroup" ], "Properties" : { "AvailabilityZones" : { "Ref" : "AZs" }, "VPCZoneIdentifier" : { "Ref" : "Subnets" }, "LaunchTemplate" : { "LaunchTemplateId" : { "Ref" : "logicalName" }, "Version" : { "Fn::GetAtt" : [ "logicalName", "LatestVersionNumber" ] } }, "MinSize" : "1", "MaxSize" : "5", "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: AvailabilityZones: !Ref AZs VPCZoneIdentifier: !Ref Subnets LaunchTemplate: LaunchTemplateId: !Ref logicalName Version: !GetAtt logicalName.LatestVersionNumber MinSize: '1' MaxSize: '5' 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

另请参阅

有关为 Auto Scaling 组指定启动模板的更多示例,请参阅 AWS::AutoScaling::AutoScalingGroup 资源中的示例部分。

使用 UpdatePolicy 声明 Auto Scaling 组

以下示例为 Auto Scaling 组指定了 UpdatePolicy 属性。示例更新策略指示 CloudFormation 使用 AutoScalingRollingUpdate 属性执行滚动更新。滚动更新根据 MaxBatchSize 和基于 PauseTime 的更新批次之间的暂停时间以小批量的方式更改 Auto Scaling 组(在本示例中,为逐个实例更改)。MinInstancesInService 属性指定当 CloudFormation 更新旧实例时,Auto Scaling 组内必须处于服务状态的实例的最小数量。

WaitOnResourceSignals 属性设置为 true。在继续更新之前,CloudFormation 必须在指定的 PauseTime 内收到来自每个新实例的信号。为了向 Auto Scaling 组发出信号,将在每个实例上运行一个 cfn-signal 帮助程序脚本(未显示)。在执行堆栈更新时,以下 EC2 Auto Scaling 进程会暂停:HealthCheckReplaceUnhealthyAZRebalanceAlarmNotificationScheduledActions。注意:不要暂停 LaunchTerminateAddToLoadBalancer (如果 Auto Scaling 组正在与 Elastic Load Balancing 一起使用)进程类型,因为这样做会使滚动更新无法正常工作。

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 模板示例

本节为不同 Amazon 资源的 Application Auto Scaling 扩展策略和计划操作提供了 Amazon CloudFormation 模板示例。

重要

当模板中包含 Application Auto Scaling 代码段时,您应使用 DependSon 属性声明对通过模板创建的特定可扩展资源的依赖关系。这将覆盖默认并行度,并指示 Amazon CloudFormation 按指定的顺序对资源进行操作。否则,可能会在完全设置资源之前应用扩展配置。

声明 Aurora 数据库集群的扩展策略

在此代码段中,您将注册一个名为 my-db-clusterAWS::RDS::DBCluster 资源。AWS::ApplicationAutoScaling::ScalableTarget 资源指示应动态扩展数据库集群,使其具有一到八个 Aurora 副本。您还可以使用 AWS::ApplicationAutoScaling::ScalingPolicy 资源将目标跟踪扩展策略应用到集群。

在此配置中,RDSReaderAverageCPUUtilization 预定义指标用于根据 Aurora 数据库集群中所有 Aurora 副本上 40% 的平均 CPU 利用率来调整该 Aurora 数据库集群。该配置将缩减冷却时间指定为 10 分钟,并将扩展冷却时间指定为 5 分钟。

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

声明 DynamoDB 表的扩展策略

此代码段演示了如何使用 AWS::ApplicationAutoScaling::ScalingPolicy 资源创建 TargetTrackingScaling 策略类型的策略,并将其应用于 AWS::DynamoDB::Table 资源。AWS::ApplicationAutoScaling::ScalableTarget 资源声明一个应用此策略的可扩展目标,其最小写入容量单位为 5,最大写入容量单位为 15。扩展策略会扩展表的写入容量吞吐量,以 DynamoDBWriteCapacityUtilization 预定义指标将目标利用率维持在 50%。

此示例利用 Fn::JoinRef 内部函数,使用在同一模板中指定的 AWS::DynamoDB::Table 资源的逻辑名称来构建 ResourceId 属性。

注意

有关如何为 DynamoDB 资源创建 Amazon CloudFormation 模板的更多信息,请参阅 Amazon Database Blog 上的博客文章“How to use Amazon 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

声明 Amazon ECS 服务的扩展策略

此代码段演示了如何使用 AWS::ApplicationAutoScaling::ScalingPolicy 资源创建策略并将其应用于 AWS::ECS::Service 资源。AWS::ApplicationAutoScaling::ScalableTarget 资源声明一个应用此策略的可扩展目标。Application Auto Scaling 可以扩展最少 1 个任务、最多 2 个任务的任务数。

此示例创建两个 TargetTrackingScaling 策略类型的扩展策略。这些策略用于根据服务的平均 CPU 和内存使用率扩展 ECS 服务。此示例利用 Fn::JoinRef 内部函数,使用在同一模板中指定的 AWS::ECS::Cluster (myContainerCluster) 和 AWS::ECS::Service (myService) 资源的逻辑名称来构建 ResourceId 属性。

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

以下示例将具有 ALBRequestCountPerTarget 预定义指标的目标跟踪扩展策略应用到 ECS 服务。该策略用于在每个目标的请求计数(每分钟)超过目标值时向 ECS 服务添加容量。由于 DisableScaleIn 的值设置为 true,因此,目标跟踪策略不会从可扩展目标中删除容量。

此示例利用 Fn::JoinFn::GetAtt 内部函数,使用在同一模板中指定的 AWS::ElasticLoadBalancingV2::LoadBalancer (myLoadBalancer) 和 AWS::ElasticLoadBalancingV2::TargetGroup (myTargetGroup) 资源的逻辑名称来构建 ResourceLabel 属性。

可扩展目标的 MaxCapacityMinCapacity 属性以及扩展策略的 TargetValue 属性引用您在创建或更新堆栈时传递给模板的参数值。

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

声明 Lambda 函数的计划操作

此代码段使用 AWS::ApplicationAutoScaling::ScalableTarget 资源,为名为 BLUE 的函数别名 (AWS::Lambda::Alias) 注册预配置并发。它还使用 cron 表达式创建具有重复计划的计划操作。

它在 RoleARN 属性中使用 Fn::JoinRef 内部函数以指定服务相关角色的 ARN。此示例利用 Fn::Sub 内部函数,使用在同一模板中指定的 AWS::Lambda::FunctionAWS::Serverless::Function 资源的逻辑名称来构建 ResourceId 属性。

您无法在指向未发布版本 ($LATEST) 的别名上分配预配置并发。

注意

有关如何为 Lambda 资源创建 Amazon CloudFormation 模板的更多信息,请参阅Amazon Compute Blog 上的博客文章“Scheduling Amazon 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 * * ? *)'

声明 Spot 队列的计划操作

此代码段演示了如何使用 AWS::ApplicationAutoScaling::ScalableTarget 资源创建计划操作并将其应用于 AWS::EC2::SpotFleet 资源。此示例利用 Fn::JoinRef 内部函数,使用在同一模板中指定的 AWS::EC2::SpotFleet 资源的逻辑名称来构建 ResourceId 属性。

注意

Spot 队列请求必须使用 maintain 作为请求类型。一次性请求或 Spot 型限制不支持自动扩展。

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 * * ? *)'