AWS CloudFormation
User Guide (API Version 2010-05-15)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。请点击 Amazon AWS 入门,可查看中国地区的具体差异

Auto Scaling 模板代码段

Auto Scaling 启动配置资源

此示例显示的是 Auto Scaling AWS::AutoScaling::LaunchConfiguration 资源。SecurityGroups 属性指定名为 myEC2SecurityGroup 的 AWS::EC2::SecurityGroup 资源和名为 myExistingEC2SecurityGroup 的现有 EC2 安全组。BlockDeviceMappings 属性列出了两个设备:一个是映射至 /dev/sdk 的 50GB EBS 卷,一个是映射至 /dev/sdc 的虚拟设备 ephemeral0。

JSON

"SimpleConfig" : { "Type" : "AWS::AutoScaling::LaunchConfiguration", "Properties" : { "ImageId" : "ami-6411e20d", "SecurityGroups" : [ { "Ref" : "myEC2SecurityGroup" }, "myExistingEC2SecurityGroup" ], "InstanceType" : "m1.small", "BlockDeviceMappings" : [ { "DeviceName" : "/dev/sdk", "Ebs" : {"VolumeSize" : "50"} }, { "DeviceName" : "/dev/sdc", "VirtualName" : "ephemeral0" } ] } }

YAML

SimpleConfig: Type: AWS::AutoScaling::LaunchConfiguration Properties: ImageId: ami-6411e20d SecurityGroups: - Ref: myEC2SecurityGroup - myExistingEC2SecurityGroup InstanceType: m1.small BlockDeviceMappings: - DeviceName: "/dev/sdk" Ebs: VolumeSize: '50' - DeviceName: "/dev/sdc" VirtualName: ephemeral0

Auto Scaling 组资源

此示例显示的是 Auto Scaling AWS::AutoScaling::AutoScalingGroup 资源。AvailabilityZones 属性指定创建 Auto Scaling 组 EC2 实例的可用区。在此示例中,Fn::GetAZs 函数调用{ "Fn::GetAZs" : "" }指定创建堆栈地区的所有可用区。LoadBalancerNames 属性列出了将流量路由到 Auto Scaling 组所用的 LoadBalancers。在此示例中,一个 LoadBalancer 被指定 AWS::ElasticLoadBalancing::LoadBalancer 资源 LB。

JSON

"MyServerGroup" : { "Type" : "AWS::AutoScaling::AutoScalingGroup", "Properties" : { "AvailabilityZones" : { "Fn::GetAZs" : ""}, "LaunchConfigurationName" : { "Ref" : "SimpleConfig" }, "MinSize" : "1", "MaxSize" : "3", "LoadBalancerNames" : [ { "Ref" : "LB" } ] } }

YAML

MyServerGroup: Type: AWS::AutoScaling::AutoScalingGroup Properties: AvailabilityZones: Fn::GetAZs: '' LaunchConfigurationName: Ref: SimpleConfig MinSize: '1' MaxSize: '3' LoadBalancerNames: - Ref: LB

Auto Scaling 策略被 CloudWatch 警报触发

此示例所示为扩展 Auto Scaling 组 asGroup 所用 AWS::AutoScaling::ScalingPolicy 资源。AdjustmentType 属性指定 ChangeInCapacity,这表示 ScalingAdjustment 代表待添加(如果 ScalingAdjustment 为正值)或待删除(如果它为负值)实例的数量。在该实例中,ScalingAdjustment 为 1;因此,执行策略时,策略将从 1 开始逐渐增大 EC2 实例的数量。

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

JSON

"ScaleUpPolicy" : { "Type" : "AWS::AutoScaling::ScalingPolicy", "Properties" : { "AdjustmentType" : "ChangeInCapacity", "AutoScalingGroupName" : { "Ref" : "asGroup" }, "Cooldown" : "1", "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": "ScaleUpPolicy" } ], "Namespace": "AWS/EC2", "Dimensions": [ { "Name": "AutoScalingGroupName", "Value": { "Ref": "asGroup" } } ], "ComparisonOperator": "GreaterThanThreshold", "MetricName": "CPUUtilization" } }

YAML

ScaleUpPolicy: Type: AWS::AutoScaling::ScalingPolicy Properties: AdjustmentType: ChangeInCapacity AutoScalingGroupName: Ref: asGroup Cooldown: '1' 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: ScaleUpPolicy Namespace: AWS/EC2 Dimensions: - Name: AutoScalingGroupName Value: Ref: asGroup ComparisonOperator: GreaterThanThreshold MetricName: CPUUtilization

带通知的 Auto Scaling 组

此示例显示的是在指定事件发生时发送 Amazon SNS 通知的 AWS::AutoScaling::AutoScalingGroup 资源。NotificationConfigurations 属性指定 AWS CloudFormation 在其中发送通知的 SNS 主题和将导致 AWS CloudFormation 发送通知的事件。当 NotificationTypes 指定的事件发生时,AWS CloudFormation 会发送一条通知至 TopicARN 指定的 SNS 主题中。在此示例中,AWS CloudFormation 会在 autoscaling:EC2_INSTANCE_LAUNCHautoscaling:EC2_INSTANCE_LAUNCH_ERROR 事件发生时发送一条通知至 SNS 主题 topic1 中。

JSON

"MyAsGroupWithNotification" : { "Type" : "AWS::AutoScaling::AutoScalingGroup", "Properties" : { "AvailabilityZones" : { "Ref" : "azList" }, "LaunchConfigurationName" : { "Ref" : "myLCOne" }, "MinSize" : "0", "MaxSize" : "2", "DesiredCapacity" : "1", "NotificationConfigurations" : [ { "TopicARN" : { "Ref" : "topic1" }, "NotificationTypes" : [ "autoscaling:EC2_INSTANCE_LAUNCH", "autoscaling:EC2_INSTANCE_LAUNCH_ERROR", "autoscaling:EC2_INSTANCE_TERMINATE", "autoscaling:EC2_INSTANCE_TERMINATE_ERROR" ] } ] } }

YAML

MyAsGroupWithNotification: Type: AWS::AutoScaling::AutoScalingGroup Properties: AvailabilityZones: Ref: azList LaunchConfigurationName: Ref: myLCOne MinSize: '0' MaxSize: '2' DesiredCapacity: '1' NotificationConfigurations: - TopicARN: Ref: topic1 NotificationTypes: - autoscaling:EC2_INSTANCE_LAUNCH - autoscaling:EC2_INSTANCE_LAUNCH_ERROR - autoscaling:EC2_INSTANCE_TERMINATE - autoscaling:EC2_INSTANCE_TERMINATE_ERROR

带 UpdatePolicy 的 Auto Scaling

此示例显示了如何将 UpdatePolicy 用于 Auto Scaling 组。

JSON

"ASG1" : { "UpdatePolicy" : { "AutoScalingRollingUpdate" : { "MinInstancesInService" : "1", "MaxBatchSize" : "1", "PauseTime" : "PT12M5S" } }, "Type" : "AWS::AutoScaling::AutoScalingGroup", "Properties" : { "AvailabilityZones" : { "Fn::GetAZs" : { "Ref" : "AWS::Region" } }, "LaunchConfigurationName" : { "Ref" : "ASLC" }, "MaxSize" : "3", "MinSize" : "1" } }

YAML

ASG1: UpdatePolicy: AutoScalingRollingUpdate: MinInstancesInService: '1' MaxBatchSize: '1' PauseTime: PT12M5S Type: AWS::AutoScaling::AutoScalingGroup Properties: AvailabilityZones: Fn::GetAZs: Ref: AWS::Region LaunchConfigurationName: Ref: ASLC MaxSize: '3' MinSize: '1'