本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
下列範例顯示要包含在範本中的不同程式碼片段,以搭配 Amazon EC2 Auto Scaling 使用。
程式碼片段類別
建立單一執行個體 Auto Scaling 群組
此範例顯示具有單一執行個體AWS::AutoScaling::AutoScalingGroup
的資源,可協助您開始使用。Auto Scaling 群組的 VPCZoneIdentifier
屬性指定三個不同可用區域中的現有子網路清單。您必須先從帳戶指定適用的子網路 ID,然後才能建立堆疊。LaunchTemplate
屬性會參考具有範本中其他位置myLaunchTemplate
定義的邏輯名稱AWS::EC2::LaunchTemplate
的資源。
注意
如需啟動範本的範例,請參閱使用 建立啟動範本 AWS CloudFormation《Amazon EC2 程式碼片段》中的 一節,以及》 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'
使用連接的負載平衡器建立 Auto Scaling 群組
此範例顯示多個伺服器上負載平衡AWS::AutoScaling::AutoScalingGroup
的資源。它指定在相同範本中其他地方宣告 AWS 的資源邏輯名稱。
-
VPCZoneIdentifier
屬性會指定建立 Auto Scaling 群組 EC2 執行個體的兩個AWS::EC2::Subnet
資源的邏輯名稱:myPublicSubnet1
和myPublicSubnet2
。 -
LaunchTemplate
屬性會指定邏輯名稱為AWS::EC2::LaunchTemplate
的資源myLaunchTemplate
。 -
TargetGroupARNs
屬性針對將流量路由到 Auto Scaling 群組所使用的 Application Load Balancer 或 Network Load Balancer,列出目標群組。在此範例中,指定一個目標群組,即邏輯名稱為AWS::ElasticLoadBalancingV2::TargetGroup
的資源myTargetGroup
。
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
另請參閱
如需根據適用於 Application Load Balancer 的 ALBRequestCountPerTarget
預先定義指標建立具有目標追蹤擴展政策之 Auto Scaling 群組的詳細範例,請參閱 AWS::AutoScaling::ScalingPolicy
資源中的範例章節。
使用通知建立 Auto Scaling 群組
此範例顯示AWS::AutoScaling::AutoScalingGroup
資源,可在發生指定事件時傳送 Amazon SNS 通知。NotificationConfigurations
屬性會指定 AWS CloudFormation 傳送通知的 SNS 主題,以及會導致 AWS CloudFormation 傳送通知的事件。NotificationTypes
發生 指定的事件時, AWS CloudFormation 會將通知傳送至 指定的 SNS 主題TopicARN
。當您啟動堆疊時, 會 AWS CloudFormation 建立在相同範本中宣告AWS::SNS::Subscription
的資源 (snsTopicForAutoScalingGroup
)。
Auto Scaling 群組的 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
建立使用 CreationPolicy
和 的 Auto Scaling 群組 UpdatePolicy
下列範例示範如何將 CreationPolicy 屬性和 UpdatePolicy 屬性新增至 AWS::AutoScaling::AutoScalingGroup
資源。
範例建立政策可防止 Auto Scaling 群組達到CREATE_COMPLETE
狀態,直到群組準備就緒時 AWS CloudFormation 收到成功訊號Count
的數量為止。若要發出 Auto Scaling 群組已就緒的訊號,需在執行個體上執行新增到啟動範本使用者資料 (未顯示) 的 cfn-signal 協助程式指令碼。如果執行個體未在指定的 Timeout
內傳送訊號,CloudFormation 將假設未建立執行個體、資源建立失敗,且 CloudFormation 將復原堆疊。
範例更新政策使用 AutoScalingRollingUpdate
屬性指示 CloudFormation,以執行滾動更新。滾動更新根據 MaxBatchSize
和以 PauseTime
為基礎的更新批次間的暫停時間,小量批次變更 Auto Scaling 群組 (在此範例中為依執行個體逐一變更)。MinInstancesInService
屬性指定在 CloudFormation 更新舊執行個體時,Auto Scaling 群組中必須為服務中的執行個體的最少數量。
WaitOnResourceSignals
屬性會設定為 true
。CloudFormation 必須在指定的 PauseTime
內先接到每個新執行個體的訊號,才會繼續更新。當堆疊正在更新時,會暫停以下 EC2 Auto Scaling 程序:HealthCheck
、ReplaceUnhealthy
、AZRebalance
、AlarmNotification
和 ScheduledActions
。注意:請勿暫停 Launch
、Terminate
或 AddToLoadBalancer
(如果 Auto Scaling 群組與 Elastic Load Balancing 搭配使用) 程序類型,因為這麼做會讓滾動更新無法正常運作。
Auto Scaling 群組的 VPCZoneIdentifier
屬性指定三個不同可用區域中的現有子網路清單。您必須先從帳戶指定適用的子網路 ID,然後才能建立堆疊。LaunchTemplate
屬性參考相同範本中其他地方宣告AWS::EC2::LaunchTemplate
的資源邏輯名稱。
JSON
{
"Resources":{
"myASG":{
"CreationPolicy":{
"ResourceSignal":{
"Count":"3",
"Timeout":"PT15M"
}
},
"UpdatePolicy":{
"AutoScalingRollingUpdate":{
"MinInstancesInService":"3",
"MaxBatchSize":"1",
"PauseTime":"PT12M5S",
"WaitOnResourceSignals":"true",
"SuspendProcesses":[
"HealthCheck",
"ReplaceUnhealthy",
"AZRebalance",
"AlarmNotification",
"ScheduledActions",
"InstanceRefresh"
]
}
},
"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: '3'
MaxBatchSize: '1'
PauseTime: PT12M5S
WaitOnResourceSignals: true
SuspendProcesses:
- HealthCheck
- ReplaceUnhealthy
- AZRebalance
- AlarmNotification
- ScheduledActions
- InstanceRefresh
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
VPCZoneIdentifier:
- subnetIdAz1
- subnetIdAz2
- subnetIdAz3
LaunchTemplate:
LaunchTemplateId: !Ref logicalName
Version: !GetAtt logicalName
.LatestVersionNumber
MaxSize: '5'
MinSize: '3'
建立步驟擴展政策
此範例顯示使用步驟擴展政策擴展 Auto Scaling 群組AWS::AutoScaling::ScalingPolicy
的資源。AdjustmentType
屬性指定 ChangeInCapacity
,這表示 ScalingAdjustment
代表要新增 (如果 ScalingAdjustment
為正數) 或要刪除 (如果該屬性為負數) 的執行個體數量。在本範例中,ScalingAdjustment
為 1。因此,在超過警示閾值時,政策即會在群組中逐次遞增 1 個 EC2 執行個體。
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
屬性的下限值。Auto Scaling 群組使用的任何執行個體類型都必須與所需執行個體屬性相符。
Auto Scaling 群組的 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'
啟動組態範例
建立啟動組態
此範例顯示 Auto Scaling 群組AWS::AutoScaling::LaunchConfiguration
的資源,您可以在其中指定 ImageId
、 InstanceType
和 SecurityGroups
屬性的值。SecurityGroups
屬性會指定範本中其他位置指定的AWS::EC2::SecurityGroup
資源邏輯名稱,以及名為 的現有 EC2 安全群組myExistingEC2SecurityGroup
。
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
建立使用啟動組態的 Auto Scaling 群組
此範例顯示具有單一執行個體AWS::AutoScaling::AutoScalingGroup
的資源。Auto Scaling 群組的 VPCZoneIdentifier
屬性指定三個不同可用區域中的現有子網路清單。您必須先從帳戶指定適用的子網路 ID,然後才能建立堆疊。LaunchConfigurationName
屬性參考具有範本中mySimpleConfig
定義的邏輯名稱AWS::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'