使用 Amazon CloudFormation 配置 Amazon EC2 Auto Scaling 资源 - Amazon CloudFormation
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

使用 Amazon CloudFormation 配置 Amazon EC2 Auto Scaling 资源

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

创建单个实例自动扩缩组

此示例显示具有单个实例的 AWS::AutoScaling::AutoScalingGroup 资源以帮助您开始使用。自动扩缩组的 VPCZoneIdentifier 属性指定三个不同可用区中的一系列现有子网。在创建堆栈之前,必须先从您的账户指定适用的子网 ID。LaunchTemplate 属性引用具有在模板其他位置中定义的逻辑名称 myLaunchTemplateAWS::EC2::LaunchTemplate 资源。

注意

有关启动模板的示例,请参阅 Amazon EC2 代码段部分中的 使用 Amazon CloudFormation 创建启动模板AWS::EC2::LaunchTemplate 资源中的示例部分。

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 - subnetIdAz3 LaunchTemplate: LaunchTemplateId: !Ref myLaunchTemplate Version: !GetAtt myLaunchTemplate.LatestVersionNumber MaxSize: '1' MinSize: '1'

创建具有附加负载均衡器的自动扩缩组

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

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

  2. LaunchTemplate 属性指定具有逻辑名称 myLaunchTemplateAWS::EC2::LaunchTemplate 资源。

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

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: - !Ref myPublicSubnet1 - !Ref myPublicSubnet2 LaunchTemplate: LaunchTemplateId: !Ref myLaunchTemplate Version: !GetAtt myLaunchTemplate.LatestVersionNumber MaxSize: '5' MinSize: '1' TargetGroupARNs: - !Ref myTargetGroup

另请参阅

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

创建具有通知的自动扩缩组

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

自动扩缩组的 VPCZoneIdentifier 属性指定三个不同可用区中的一系列现有子网。在创建堆栈之前,必须先从您的账户指定适用的子网 ID。LaunchTemplate 属性引用在同一模板中的其他位置声明的 AWS::EC2::LaunchTemplate 资源的逻辑名称。

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 - subnetIdAz3 LaunchTemplate: LaunchTemplateId: !Ref logicalName Version: !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

创建使用 CreationPolicyUpdatePolicy 的自动扩缩组

以下示例演示如何将 CreationPolicy 属性UpdatePolicy 属性添加到 AWS::AutoScaling::AutoScalingGroup 资源。

示例创建策略会阻止自动扩缩组达到 CREATE_COMPLETE 状态,直到 Amazon CloudFormation 在该组准备就绪后收到 Count 个成功信号。要发出自动扩缩组已就绪的信号,在实例上运行添加到启动模板用户数据(未显示)的 cfn-signal 帮助程序脚本。如果实例未在规定的 Timeout 内发送信号,则 CloudFormation 假设未创建实例、资源创建失败和 CloudFormation 回滚堆栈。

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

WaitOnResourceSignals 属性设置为 true。CloudFormation 必须在指定的 PauseTime 内收到每个新实例的信号,才能继续更新。在执行堆栈更新时,以下 EC2 Auto Scaling 进程会暂停:HealthCheckReplaceUnhealthyAZRebalanceAlarmNotificationScheduledActions。注意:不要暂停 LaunchTerminateAddToLoadBalancer (如果自动扩缩组正在与 Elastic Load Balancing 一起使用)进程类型,因为这样做会使滚动更新无法正常工作。

自动扩缩组的 VPCZoneIdentifier 属性指定三个不同可用区中的一系列现有子网。在创建堆栈之前,必须先从您的账户指定适用的子网 ID。LaunchTemplate 属性引用在同一模板中的其他位置声明的 AWS::EC2::LaunchTemplate 资源的逻辑名称。

JSON

{ "Resources":{ "myASG":{ "CreationPolicy":{ "ResourceSignal":{ "Count":"3", "Timeout":"PT15M" } }, "UpdatePolicy":{ "AutoScalingRollingUpdate":{ "MinInstancesInService":"1", "MaxBatchSize":"1", "PauseTime":"PT12M5S", "WaitOnResourceSignals":"true", "SuspendProcesses":[ "HealthCheck", "ReplaceUnhealthy", "AZRebalance", "AlarmNotification", "ScheduledActions" ] } }, "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: '1' MaxBatchSize: '1' PauseTime: PT12M5S WaitOnResourceSignals: true SuspendProcesses: - HealthCheck - ReplaceUnhealthy - AZRebalance - AlarmNotification - ScheduledActions Type: AWS::AutoScaling::AutoScalingGroup Properties: VPCZoneIdentifier: - subnetIdAz1 - subnetIdAz2 - subnetIdAz3 LaunchTemplate: LaunchTemplateId: !Ref logicalName Version: !GetAtt logicalName.LatestVersionNumber MaxSize: '5' MinSize: '3'

创建分步扩缩策略

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

AWS::CloudWatch::Alarm 资源 CPUAlarmHigh 指定扩缩策略 ASGScalingPolicyHigh 作为警报处于 ALARM 状态时要运行的操作(AlarmActions)。Dimensions 属性引用在同一模板中的其他位置声明的 AWS::AutoScaling::AutoScalingGroup 资源的逻辑名称。

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: !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

另请参阅

有关扩展策略的更多示例模板,请参阅 AWS::AutoScaling::ScalingPolicy 资源中的示例部分。

混合实例组示例

示例:使用基于属性的实例类型选择创建 Auto Scaling 组

此示例显示了 AWS::AutoScaling::AutoScalingGroup 资源,其中包含使用基于属性的实例类型选择启动混合实例组的信息。您指定了 VCpuCount 属性的最小值和最大值,以及 MemoryMiB 属性的最小值。自动扩缩组使用的所有实例类型都必须与您所需实例的属性匹配。

自动扩缩组的 VPCZoneIdentifier 属性指定三个不同可用区中的一系列现有子网。在创建堆栈之前,必须先从您的账户指定适用的子网 ID。LaunchTemplate 属性引用在同一模板中的其他位置声明的 AWS::EC2::LaunchTemplate 资源的逻辑名称。

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 - subnetIdAz3 MixedInstancesPolicy: LaunchTemplate: LaunchTemplateSpecification: LaunchTemplateId: !Ref logicalName Version: !GetAtt logicalName.LatestVersionNumber Overrides: - InstanceRequirements: VCpuCount: Min: 2 Max: 4 MemoryMiB: Min: 2048 MaxSize: '5' MinSize: '1'

启动配置示例

创建启动配置

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

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-02354e95b3example InstanceType: t3.micro SecurityGroups: - !Ref logicalName - myExistingEC2SecurityGroup

使用启动配置创建自动扩缩组

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

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 - subnetIdAz3 LaunchConfigurationName: !Ref mySimpleConfig MaxSize: '1' MinSize: '1'