本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 設定 Amazon EC2 Auto Scaling 資源CloudFormation
下列範例示範要在範本中包含的不同程式碼片段,以與 Amazon EC2 Auto Scaling 搭配使用。
程式碼片段類別
建立單一執行個體 Auto Scaling 群組
此範例顯示具有單一執行個體的 AWS::AutoScaling::AutoScalingGroup 資源,以協助您開始使用。Auto Scaling 群組的 VPCZoneIdentifier 屬性指定三個不同可用區域中的現有子網路清單。您必須先從帳戶指定適用的子網路 ID,然後才能建立堆疊。LaunchTemplate 屬性參考 AWS::EC2::LaunchTemplate 資源,該資源具有範本中其他處所定義的邏輯名稱 myLaunchTemplate。
注意
如需啟動範本的範例,請參閱 Amazon EC2 程式碼片段章節中的 透過 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-subnetIdAz3LaunchTemplate: LaunchTemplateId: !RefmyLaunchTemplateVersion: !GetAttmyLaunchTemplate.LatestVersionNumber MaxSize: '1' MinSize: '1'
透過連接的負載平衡器建立 Auto Scaling 群組
此範例顯示 AWS::AutoScaling::AutoScalingGroup 資源,適用於多個伺服器之間的負載平衡。它指定在相同範本中其他位置宣告AWS的資源邏輯名稱。
-
VPCZoneIdentifier屬性指定兩個AWS::EC2::Subnet資源的邏輯名稱 (將在其中建立 Auto Scaling 群組的 EC2 執行個體):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: - !RefmyPublicSubnet1- !RefmyPublicSubnet2LaunchTemplate: LaunchTemplateId: !RefmyLaunchTemplateVersion: !GetAttmyLaunchTemplate.LatestVersionNumber MaxSize: '5' MinSize: '1' TargetGroupARNs: - !RefmyTargetGroup
另請參閱
如需根據適用於 Application Load Balancer 的 ALBRequestCountPerTarget 預先定義指標建立具有目標追蹤擴展政策之 Auto Scaling 群組的詳細範例,請參閱 AWS::AutoScaling::ScalingPolicy 資源中的範例章節。
透過通知建立 Auto Scaling 群組
此範例顯示的 AWS::AutoScaling::AutoScalingGroup 資源會在指定的事件發生時傳送 Amazon SNS 通知。NotificationConfigurations 屬性指定 SNS 主題,其中 CloudFormation會傳送通知,以及會導致 CloudFormation傳送通知的事件。當 指定的事件NotificationTypes發生時, CloudFormation會傳送通知至 指定的 SNS 主題TopicARN。當您啟動堆疊時, 會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-subnetIdAz3LaunchTemplate: LaunchTemplateId: !ReflogicalNameVersion: !GetAttlogicalName.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 和 UpdatePolicy 的 Auto Scaling 群組
下列範例顯示如何將 CreationPolicy 和 UpdatePolicy 屬性新增至 AWS::AutoScaling::AutoScalingGroup 資源。
範例建立政策可防止 Auto Scaling 群組達到CREATE_COMPLETE狀態,直到群組準備就緒時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 群組正在與 ELB 搭配使用) 程序類型,因為這樣做可防止滾動更新正常運作。
Auto Scaling 群組的 VPCZoneIdentifier 屬性指定三個不同可用區域中的現有子網路清單。您必須先從帳戶指定適用的子網路 ID,然後才能建立堆疊。LaunchTemplate 屬性參考在相同範本中其他位置宣告的 AWS::EC2::LaunchTemplate 資源邏輯名稱。
如需 CreationPolicy 和 UpdatePolicy 屬性的詳細資訊,請參閱資源屬性參考。
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-subnetIdAz3LaunchTemplate: LaunchTemplateId: !ReflogicalNameVersion: !GetAttlogicalName.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: !ReflogicalNamePolicyType: 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: !ReflogicalNameComparisonOperator: 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-subnetIdAz3MixedInstancesPolicy: LaunchTemplate: LaunchTemplateSpecification: LaunchTemplateId: !ReflogicalNameVersion: !GetAttlogicalName.LatestVersionNumber Overrides: - InstanceRequirements: VCpuCount: Min:2Max:4MemoryMiB: Min:2048MaxSize: '5' MinSize: '1'
啟動組態範例
建立啟動組態
此範例顯示 Auto Scaling 群組的 AWS::AutoScaling::LaunchConfiguration 資源,您在此資源中為 ImageId、InstanceType 和 SecurityGroups 屬性指定值。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-02354e95b3exampleInstanceType:t3.microSecurityGroups: - !ReflogicalName-myExistingEC2SecurityGroup
使用啟動組態建立 Auto Scaling 群組
此範例顯示具有單一執行個體的 AWS::AutoScaling::AutoScalingGroup 資源。Auto Scaling 群組的 VPCZoneIdentifier 屬性指定三個不同可用區域中的現有子網路清單。您必須先從帳戶指定適用的子網路 ID,然後才能建立堆疊。LaunchConfigurationName 屬性參考 AWS::AutoScaling::LaunchConfiguration 資源,該資源具有範本中所定義的邏輯名稱 mySimpleConfig。
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-subnetIdAz3LaunchConfigurationName: !RefmySimpleConfigMaxSize: '1' MinSize: '1'