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

演练:创建可扩展、负载均衡的 Web 服务器

本模板可创建使用 Auto Scaling 和 Elastic Load Balancing 的示例网站,并配置为使用多个可用区。模板还包含 CloudWatch 警报,在超过定义的阈值时,这些警报将执行 Auto Scaling 策略以在 Auto Scaling 组中添加或删除实例。

本模板可创建一个或多个 Amazon EC2 实例。如果您通过本模板创建堆栈,那么会针对 AWS 资源向您收取相应费用。

注意

此模板假设您的账户支持 EC2-VPC 平台。换言之,您有一个允许实例访问 Internet 的默认 VPC。如果您没有默认 VPC,则可以创建一个。有关更多信息,请参阅 Amazon EC2 用户指南(适用于 Linux 实例) 中的 Amazon EC2 和 Amazon Virtual Private Cloud

您可以从以下网址获取此示例模板的最新版本:https://s3.amazonaws.com/cloudformation-templates-us-east-1/AutoScalingMultiAZWithNotifications.template

Auto Scaling Multi-AZ 模板

{ "AWSTemplateFormatVersion" : "2010-09-09", "Description" : "AWS CloudFormation Sample Template AutoScalingMultiAZWithNotifications: Create a multi-az, load balanced and Auto Scaled sample web site running on an Apache Web Serever. The application is configured to span all Availability Zones in the region and is Auto-Scaled based on the CPU utilization of the web servers. Notifications will be sent to the operator email address on scaling events. The instances are load balanced with a simple health check against the default web page. **WARNING** This template creates one or more Amazon EC2 instances and an Elastic Load Balancer. You will be billed for the AWS resources used if you create a stack from this template.", "Parameters" : { "InstanceType" : { "Description" : "WebServer EC2 instance type", "Type" : "String", "Default" : "t2.small", "AllowedValues" : [ "t1.micro", "t2.nano", "t2.micro", "t2.small", "t2.medium", "t2.large", "m1.small", "m1.medium", "m1.large", "m1.xlarge", "m2.xlarge", "m2.2xlarge", "m2.4xlarge", "m3.medium", "m3.large", "m3.xlarge", "m3.2xlarge", "m4.large", "m4.xlarge", "m4.2xlarge", "m4.4xlarge", "m4.10xlarge", "c1.medium", "c1.xlarge", "c3.large", "c3.xlarge", "c3.2xlarge", "c3.4xlarge", "c3.8xlarge", "c4.large", "c4.xlarge", "c4.2xlarge", "c4.4xlarge", "c4.8xlarge", "g2.2xlarge", "g2.8xlarge", "r3.large", "r3.xlarge", "r3.2xlarge", "r3.4xlarge", "r3.8xlarge", "i2.xlarge", "i2.2xlarge", "i2.4xlarge", "i2.8xlarge", "d2.xlarge", "d2.2xlarge", "d2.4xlarge", "d2.8xlarge", "hi1.4xlarge", "hs1.8xlarge", "cr1.8xlarge", "cc2.8xlarge", "cg1.4xlarge"] , "ConstraintDescription" : "must be a valid EC2 instance type." }, "OperatorEMail": { "Description": "EMail address to notify if there are any scaling operations", "Type": "String", "AllowedPattern": "([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)", "ConstraintDescription": "must be a valid email address." }, "KeyName" : { "Description" : "The EC2 Key Pair to allow SSH access to the instances", "Type" : "AWS::EC2::KeyPair::KeyName", "ConstraintDescription" : "must be the name of an existing EC2 KeyPair." }, "SSHLocation" : { "Description" : "The IP address range that can be used to SSH to the EC2 instances", "Type": "String", "MinLength": "9", "MaxLength": "18", "Default": "0.0.0.0/0", "AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})", "ConstraintDescription": "must be a valid IP CIDR range of the form x.x.x.x/x." } }, "Mappings" : { "Region2Examples" : { "us-east-1" : { "Examples" : "https://s3.amazonaws.com/cloudformation-examples-us-east-1" }, "us-west-2" : { "Examples" : "https://s3-us-west-2.amazonaws.com/cloudformation-examples-us-west-2" }, "us-west-1" : { "Examples" : "https://s3-us-west-1.amazonaws.com/cloudformation-examples-us-west-1" }, "eu-west-1" : { "Examples" : "https://s3-eu-west-1.amazonaws.com/cloudformation-examples-eu-west-1" }, "eu-west-2" : { "Examples" : "https://s3-eu-west-2.amazonaws.com/cloudformation-examples-eu-west-2" }, "eu-central-1" : { "Examples" : "https://s3-eu-central-1.amazonaws.com/cloudformation-examples-eu-central-1" }, "ap-southeast-1" : { "Examples" : "https://s3-ap-southeast-1.amazonaws.com/cloudformation-examples-ap-southeast-1" }, "ap-northeast-1" : { "Examples" : "https://s3-ap-northeast-1.amazonaws.com/cloudformation-examples-ap-northeast-1" }, "ap-northeast-2" : { "Examples" : "https://s3-ap-northeast-2.amazonaws.com/cloudformation-examples-ap-northeast-2" }, "ap-southeast-2" : { "Examples" : "https://s3-ap-southeast-2.amazonaws.com/cloudformation-examples-ap-southeast-2" }, "ap-south-1" : { "Examples" : "https://s3-ap-south-1.amazonaws.com/cloudformation-examples-ap-south-1" }, "us-east-2" : { "Examples" : "https://s3-us-east-2.amazonaws.com/cloudformation-examples-us-east-2" }, "ca-central-1" : { "Examples" : "https://s3-ca-central-1.amazonaws.com/cloudformation-examples-ca-central-1" }, "sa-east-1" : { "Examples" : "https://s3-sa-east-1.amazonaws.com/cloudformation-examples-sa-east-1" }, "cn-north-1" : { "Examples" : "https://s3.cn-north-1.amazonaws.com.cn/cloudformation-examples-cn-north-1" } } , "AWSInstanceType2Arch" : { "t1.micro" : { "Arch" : "PV64" }, "t2.nano" : { "Arch" : "HVM64" }, "t2.micro" : { "Arch" : "HVM64" }, "t2.small" : { "Arch" : "HVM64" }, "t2.medium" : { "Arch" : "HVM64" }, "t2.large" : { "Arch" : "HVM64" }, "m1.small" : { "Arch" : "PV64" }, "m1.medium" : { "Arch" : "PV64" }, "m1.large" : { "Arch" : "PV64" }, "m1.xlarge" : { "Arch" : "PV64" }, "m2.xlarge" : { "Arch" : "PV64" }, "m2.2xlarge" : { "Arch" : "PV64" }, "m2.4xlarge" : { "Arch" : "PV64" }, "m3.medium" : { "Arch" : "HVM64" }, "m3.large" : { "Arch" : "HVM64" }, "m3.xlarge" : { "Arch" : "HVM64" }, "m3.2xlarge" : { "Arch" : "HVM64" }, "m4.large" : { "Arch" : "HVM64" }, "m4.xlarge" : { "Arch" : "HVM64" }, "m4.2xlarge" : { "Arch" : "HVM64" }, "m4.4xlarge" : { "Arch" : "HVM64" }, "m4.10xlarge" : { "Arch" : "HVM64" }, "c1.medium" : { "Arch" : "PV64" }, "c1.xlarge" : { "Arch" : "PV64" }, "c3.large" : { "Arch" : "HVM64" }, "c3.xlarge" : { "Arch" : "HVM64" }, "c3.2xlarge" : { "Arch" : "HVM64" }, "c3.4xlarge" : { "Arch" : "HVM64" }, "c3.8xlarge" : { "Arch" : "HVM64" }, "c4.large" : { "Arch" : "HVM64" }, "c4.xlarge" : { "Arch" : "HVM64" }, "c4.2xlarge" : { "Arch" : "HVM64" }, "c4.4xlarge" : { "Arch" : "HVM64" }, "c4.8xlarge" : { "Arch" : "HVM64" }, "g2.2xlarge" : { "Arch" : "HVMG2" }, "g2.8xlarge" : { "Arch" : "HVMG2" }, "r3.large" : { "Arch" : "HVM64" }, "r3.xlarge" : { "Arch" : "HVM64" }, "r3.2xlarge" : { "Arch" : "HVM64" }, "r3.4xlarge" : { "Arch" : "HVM64" }, "r3.8xlarge" : { "Arch" : "HVM64" }, "i2.xlarge" : { "Arch" : "HVM64" }, "i2.2xlarge" : { "Arch" : "HVM64" }, "i2.4xlarge" : { "Arch" : "HVM64" }, "i2.8xlarge" : { "Arch" : "HVM64" }, "d2.xlarge" : { "Arch" : "HVM64" }, "d2.2xlarge" : { "Arch" : "HVM64" }, "d2.4xlarge" : { "Arch" : "HVM64" }, "d2.8xlarge" : { "Arch" : "HVM64" }, "hi1.4xlarge" : { "Arch" : "HVM64" }, "hs1.8xlarge" : { "Arch" : "HVM64" }, "cr1.8xlarge" : { "Arch" : "HVM64" }, "cc2.8xlarge" : { "Arch" : "HVM64" } }, "AWSInstanceType2NATArch" : { "t1.micro" : { "Arch" : "NATPV64" }, "t2.nano" : { "Arch" : "NATHVM64" }, "t2.micro" : { "Arch" : "NATHVM64" }, "t2.small" : { "Arch" : "NATHVM64" }, "t2.medium" : { "Arch" : "NATHVM64" }, "t2.large" : { "Arch" : "NATHVM64" }, "m1.small" : { "Arch" : "NATPV64" }, "m1.medium" : { "Arch" : "NATPV64" }, "m1.large" : { "Arch" : "NATPV64" }, "m1.xlarge" : { "Arch" : "NATPV64" }, "m2.xlarge" : { "Arch" : "NATPV64" }, "m2.2xlarge" : { "Arch" : "NATPV64" }, "m2.4xlarge" : { "Arch" : "NATPV64" }, "m3.medium" : { "Arch" : "NATHVM64" }, "m3.large" : { "Arch" : "NATHVM64" }, "m3.xlarge" : { "Arch" : "NATHVM64" }, "m3.2xlarge" : { "Arch" : "NATHVM64" }, "m4.large" : { "Arch" : "NATHVM64" }, "m4.xlarge" : { "Arch" : "NATHVM64" }, "m4.2xlarge" : { "Arch" : "NATHVM64" }, "m4.4xlarge" : { "Arch" : "NATHVM64" }, "m4.10xlarge" : { "Arch" : "NATHVM64" }, "c1.medium" : { "Arch" : "NATPV64" }, "c1.xlarge" : { "Arch" : "NATPV64" }, "c3.large" : { "Arch" : "NATHVM64" }, "c3.xlarge" : { "Arch" : "NATHVM64" }, "c3.2xlarge" : { "Arch" : "NATHVM64" }, "c3.4xlarge" : { "Arch" : "NATHVM64" }, "c3.8xlarge" : { "Arch" : "NATHVM64" }, "c4.large" : { "Arch" : "NATHVM64" }, "c4.xlarge" : { "Arch" : "NATHVM64" }, "c4.2xlarge" : { "Arch" : "NATHVM64" }, "c4.4xlarge" : { "Arch" : "NATHVM64" }, "c4.8xlarge" : { "Arch" : "NATHVM64" }, "g2.2xlarge" : { "Arch" : "NATHVMG2" }, "g2.8xlarge" : { "Arch" : "NATHVMG2" }, "r3.large" : { "Arch" : "NATHVM64" }, "r3.xlarge" : { "Arch" : "NATHVM64" }, "r3.2xlarge" : { "Arch" : "NATHVM64" }, "r3.4xlarge" : { "Arch" : "NATHVM64" }, "r3.8xlarge" : { "Arch" : "NATHVM64" }, "i2.xlarge" : { "Arch" : "NATHVM64" }, "i2.2xlarge" : { "Arch" : "NATHVM64" }, "i2.4xlarge" : { "Arch" : "NATHVM64" }, "i2.8xlarge" : { "Arch" : "NATHVM64" }, "d2.xlarge" : { "Arch" : "NATHVM64" }, "d2.2xlarge" : { "Arch" : "NATHVM64" }, "d2.4xlarge" : { "Arch" : "NATHVM64" }, "d2.8xlarge" : { "Arch" : "NATHVM64" }, "hi1.4xlarge" : { "Arch" : "NATHVM64" }, "hs1.8xlarge" : { "Arch" : "NATHVM64" }, "cr1.8xlarge" : { "Arch" : "NATHVM64" }, "cc2.8xlarge" : { "Arch" : "NATHVM64" } } , "AWSRegionArch2AMI" : { "us-east-1" : {"PV64" : "ami-2a69aa47", "HVM64" : "ami-6869aa05", "HVMG2" : "ami-a41a3fb3"}, "us-west-2" : {"PV64" : "ami-7f77b31f", "HVM64" : "ami-7172b611", "HVMG2" : "ami-caf253aa"}, "us-west-1" : {"PV64" : "ami-a2490dc2", "HVM64" : "ami-31490d51", "HVMG2" : "ami-00347e60"}, "eu-west-1" : {"PV64" : "ami-4cdd453f", "HVM64" : "ami-f9dd458a", "HVMG2" : "ami-e2f7bd91"}, "eu-west-2" : {"PV64" : "NOT_SUPPORTED", "HVM64" : "ami-886369ec", "HVMG2" : "NOT_SUPPORTED"}, "eu-central-1" : {"PV64" : "ami-6527cf0a", "HVM64" : "ami-ea26ce85", "HVMG2" : "ami-d2ff04bd"}, "ap-northeast-1" : {"PV64" : "ami-3e42b65f", "HVM64" : "ami-374db956", "HVMG2" : "ami-4c78d52d"}, "ap-northeast-2" : {"PV64" : "NOT_SUPPORTED", "HVM64" : "ami-2b408b45", "HVMG2" : "NOT_SUPPORTED"}, "ap-southeast-1" : {"PV64" : "ami-df9e4cbc", "HVM64" : "ami-a59b49c6", "HVMG2" : "ami-f3f95990"}, "ap-southeast-2" : {"PV64" : "ami-63351d00", "HVM64" : "ami-dc361ebf", "HVMG2" : "ami-3a122e59"}, "ap-south-1" : {"PV64" : "NOT_SUPPORTED", "HVM64" : "ami-ffbdd790", "HVMG2" : "ami-21a7d34e"}, "us-east-2" : {"PV64" : "NOT_SUPPORTED", "HVM64" : "ami-f6035893", "HVMG2" : "NOT_SUPPORTED"}, "ca-central-1" : {"PV64" : "NOT_SUPPORTED", "HVM64" : "ami-730ebd17", "HVMG2" : "NOT_SUPPORTED"}, "sa-east-1" : {"PV64" : "ami-1ad34676", "HVM64" : "ami-6dd04501", "HVMG2" : "NOT_SUPPORTED"}, "cn-north-1" : {"PV64" : "ami-77559f1a", "HVM64" : "ami-8e6aa0e3", "HVMG2" : "NOT_SUPPORTED"} } }, "Resources" : { "NotificationTopic": { "Type": "AWS::SNS::Topic", "Properties": { "Subscription": [ { "Endpoint": { "Ref": "OperatorEMail" }, "Protocol": "email" } ] } }, "WebServerGroup" : { "Type" : "AWS::AutoScaling::AutoScalingGroup", "Properties" : { "AvailabilityZones" : { "Fn::GetAZs" : ""}, "LaunchConfigurationName" : { "Ref" : "LaunchConfig" }, "MinSize" : "1", "MaxSize" : "3", "LoadBalancerNames" : [ { "Ref" : "ElasticLoadBalancer" } ], "NotificationConfiguration" : { "TopicARN" : { "Ref" : "NotificationTopic" }, "NotificationTypes" : [ "autoscaling:EC2_INSTANCE_LAUNCH", "autoscaling:EC2_INSTANCE_LAUNCH_ERROR", "autoscaling:EC2_INSTANCE_TERMINATE", "autoscaling:EC2_INSTANCE_TERMINATE_ERROR"] } }, "CreationPolicy" : { "ResourceSignal" : { "Timeout" : "PT15M", "Count" : "1" } }, "UpdatePolicy": { "AutoScalingRollingUpdate": { "MinInstancesInService": "1", "MaxBatchSize": "1", "PauseTime" : "PT15M", "WaitOnResourceSignals": "true" } } }, "LaunchConfig" : { "Type" : "AWS::AutoScaling::LaunchConfiguration", "Metadata" : { "Comment" : "Install a simple application", "AWS::CloudFormation::Init" : { "config" : { "packages" : { "yum" : { "httpd" : [] } }, "files" : { "/var/www/html/index.html" : { "content" : { "Fn::Join" : ["\n", [ "<img src=\"", {"Fn::FindInMap" : ["Region2Examples", {"Ref" : "AWS::Region"}, "Examples"]}, "/cloudformation_graphic.png\" alt=\"AWS CloudFormation Logo\"/>", "<h1>Congratulations, you have successfully launched the AWS CloudFormation sample.</h1>" ]]}, "mode" : "000644", "owner" : "root", "group" : "root" }, "/etc/cfn/cfn-hup.conf" : { "content" : { "Fn::Join" : ["", [ "[main]\n", "stack=", { "Ref" : "AWS::StackId" }, "\n", "region=", { "Ref" : "AWS::Region" }, "\n" ]]}, "mode" : "000400", "owner" : "root", "group" : "root" }, "/etc/cfn/hooks.d/cfn-auto-reloader.conf" : { "content": { "Fn::Join" : ["", [ "[cfn-auto-reloader-hook]\n", "triggers=post.update\n", "path=Resources.LaunchConfig.Metadata.AWS::CloudFormation::Init\n", "action=/opt/aws/bin/cfn-init -v ", " --stack ", { "Ref" : "AWS::StackName" }, " --resource LaunchConfig ", " --region ", { "Ref" : "AWS::Region" }, "\n", "runas=root\n" ]]} } }, "services" : { "sysvinit" : { "httpd" : { "enabled" : "true", "ensureRunning" : "true" }, "cfn-hup" : { "enabled" : "true", "ensureRunning" : "true", "files" : ["/etc/cfn/cfn-hup.conf", "/etc/cfn/hooks.d/cfn-auto-reloader.conf"]} } } } } }, "Properties" : { "KeyName" : { "Ref" : "KeyName" }, "ImageId" : { "Fn::FindInMap" : [ "AWSRegionArch2AMI", { "Ref" : "AWS::Region" }, { "Fn::FindInMap" : [ "AWSInstanceType2Arch", { "Ref" : "InstanceType" }, "Arch" ] } ] }, "SecurityGroups" : [ { "Ref" : "InstanceSecurityGroup" } ], "InstanceType" : { "Ref" : "InstanceType" }, "UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [ "#!/bin/bash -xe\n", "yum update -y aws-cfn-bootstrap\n", "/opt/aws/bin/cfn-init -v ", " --stack ", { "Ref" : "AWS::StackName" }, " --resource LaunchConfig ", " --region ", { "Ref" : "AWS::Region" }, "\n", "/opt/aws/bin/cfn-signal -e $? ", " --stack ", { "Ref" : "AWS::StackName" }, " --resource WebServerGroup ", " --region ", { "Ref" : "AWS::Region" }, "\n" ]]}} } }, "WebServerScaleUpPolicy" : { "Type" : "AWS::AutoScaling::ScalingPolicy", "Properties" : { "AdjustmentType" : "ChangeInCapacity", "AutoScalingGroupName" : { "Ref" : "WebServerGroup" }, "Cooldown" : "60", "ScalingAdjustment" : "1" } }, "WebServerScaleDownPolicy" : { "Type" : "AWS::AutoScaling::ScalingPolicy", "Properties" : { "AdjustmentType" : "ChangeInCapacity", "AutoScalingGroupName" : { "Ref" : "WebServerGroup" }, "Cooldown" : "60", "ScalingAdjustment" : "-1" } }, "CPUAlarmHigh": { "Type": "AWS::CloudWatch::Alarm", "Properties": { "AlarmDescription": "Scale-up if CPU > 90% for 10 minutes", "MetricName": "CPUUtilization", "Namespace": "AWS/EC2", "Statistic": "Average", "Period": "300", "EvaluationPeriods": "2", "Threshold": "90", "AlarmActions": [ { "Ref": "WebServerScaleUpPolicy" } ], "Dimensions": [ { "Name": "AutoScalingGroupName", "Value": { "Ref": "WebServerGroup" } } ], "ComparisonOperator": "GreaterThanThreshold" } }, "CPUAlarmLow": { "Type": "AWS::CloudWatch::Alarm", "Properties": { "AlarmDescription": "Scale-down if CPU < 70% for 10 minutes", "MetricName": "CPUUtilization", "Namespace": "AWS/EC2", "Statistic": "Average", "Period": "300", "EvaluationPeriods": "2", "Threshold": "70", "AlarmActions": [ { "Ref": "WebServerScaleDownPolicy" } ], "Dimensions": [ { "Name": "AutoScalingGroupName", "Value": { "Ref": "WebServerGroup" } } ], "ComparisonOperator": "LessThanThreshold" } }, "ElasticLoadBalancer" : { "Type" : "AWS::ElasticLoadBalancing::LoadBalancer", "Properties" : { "AvailabilityZones" : { "Fn::GetAZs" : "" }, "CrossZone" : "true", "Listeners" : [ { "LoadBalancerPort" : "80", "InstancePort" : "80", "Protocol" : "HTTP" } ], "HealthCheck" : { "Target" : "HTTP:80/", "HealthyThreshold" : "3", "UnhealthyThreshold" : "5", "Interval" : "30", "Timeout" : "5" } } }, "InstanceSecurityGroup" : { "Type" : "AWS::EC2::SecurityGroup", "Properties" : { "GroupDescription" : "Enable SSH access and HTTP from the load balancer only", "SecurityGroupIngress" : [ { "IpProtocol" : "tcp", "FromPort" : "22", "ToPort" : "22", "CidrIp" : { "Ref" : "SSHLocation"} }, { "IpProtocol" : "tcp", "FromPort" : "80", "ToPort" : "80", "SourceSecurityGroupOwnerId" : {"Fn::GetAtt" : ["ElasticLoadBalancer", "SourceSecurityGroup.OwnerAlias"]}, "SourceSecurityGroupName" : {"Fn::GetAtt" : ["ElasticLoadBalancer", "SourceSecurityGroup.GroupName"]} } ] } } }, "Outputs" : { "URL" : { "Description" : "The URL of the website", "Value" : { "Fn::Join" : [ "", [ "http://", { "Fn::GetAtt" : [ "ElasticLoadBalancer", "DNSName" ]}]]} } } }

模板演练

该示例模板包括 Auto Scaling 组,其带有负载均衡器、定义入口规则的安全组、CloudWatch 警报和 Auto Scaling 策略。

该模板具备三项输入参数:InstanceType 是 EC2 实例的类型,其用于 Auto Scaling 组,且其默认值为 m1.small;WebServerPort 是 Web 服务器的 TCP 端口,默认值为 8888;KeyName 是 EC2 密钥对的名称,其供 Auto Scaling 组使用。必须在堆栈创建时指定 KeyName(堆栈创建时,必须指定不带默认值的参数)。

AWS::AutoScaling::AutoScalingGroup 资源 WebServerGroup 将声明以下 Auto Scaling 组配置:

  • AvailabilityZones 将指定可以创建 Auto Scaling 组 EC2 实例的可用区。Fn::GetAZs 函数调用 { "Fn::GetAZs" : "" } 会指定创建堆栈所在位置的可用区。

  • MinSizeMaxSize 设置 Auto Scaling 组中的 EC2 实例数的最小值和最大值。

  • LoadBalancerNames 将列出用于将流量路由至 Auto Scaling 组的负载均衡器。该组的 LoadBalancer 是 ElasticLoadBalancer 资源。

AWS::AutoScaling::LaunchConfiguration 资源 LaunchConfig 将声明以下配置,WebServerGroup Auto Scaling 组中的 EC2 实例将运用此配置:

  • KeyName 获取 KeyName 输入参数的值作为要使用的 EC2 密钥对。

  • UserData 为 WebServerPort 参数的 Base64 编码值,该参数将传递给应用程序。

  • SecurityGroups 为 EC2 安全组的列表,EC2 安全组包含 Auto Scaling 组中 EC2 实例的防火墙入口规则。在本示例中,仅有一个安全组,其声明为 AWS::EC2::SecurityGroup 资源:InstanceSecurityGroup。该安全组包含两条入口规则:1) 一条 TCP 入口规则是允许通过所有 IP 地址("CidrIp" : "0.0.0.0/0")访问端口 22(用于 SSH 访问);2) 另一条 TCP 入口规则是允许通过指定复杂均衡器源安全组通过 Elastic Load Balancer 访问 WebServerPort 端口。GetAtt 函数用于通过 ElasticLoadBalancer 资源获取 SourceSecurityGroup.OwnerAlias 和 SourceSecurityGroup.GroupName 属性。有关 Elastic Load Balancing 安全组的更多信息,请参阅在 Amazon EC2-Classic 中管理安全组在 Amazon VPC 中管理安全组

  • ImageId 是一组嵌套映射的评估值。我们将添加映射,因此,模板包括用于选择正确映像 ID 的逻辑。该逻辑基于使用 InstanceType 参数指定的实例类型 (AWSInstanceType2Arch 将实例类型映射到 32 位或 64 位架构) 和将创建堆栈的区域 (AWSRegionArch2AMI 将区域和架构映射到映像 ID):

    { "Fn::FindInMap" : [ "AWSRegionArch2AMI", { "Ref" : "AWS::Region" }, { "Fn::FindInMap" : [ "AWSInstanceType2Arch", { "Ref" : "InstanceType" }, "Arch" ] } ]}

    例如,如果使用此模板在 us-east-2 区域中创建堆栈,并指定 InstanceType 为 m1.small,则 AWS CloudFormation 将 AWSInstanceType2Arch 的内部映射评估为以下内容:

    { "Fn::FindInMap" : [ "AWSInstanceType2Arch", "m1.small", "Arch" ] }

    在 AWSInstanceType2Arch 映射中,m1.small 密钥的架构值将映射到 32,该值用作外部映射的值。密钥是 AWS::Region 评估结果,其为将创建堆栈的区域。在此示例中,AWS::Region 为 us-east-1;因此,将按以下方式评估外部映射:

    Fn::FindInMap" : [ "AWSRegionArch2AMI", "us-east-1", "32"]

    在 AWSRegionArch2AMI 映射中,密钥 us-east-1 的值 32 将映射到 ami-6411e20d。这表示 ImageId would be ami-6411e20d。

AWS::ElasticLoadBalancing::LoadBalancer 资源 ElasticLoadBalancer 将声明以下 LoadBalancer 配置:

  • AvailabilityZones 是 LoadBalancer 可分配流量的可用区列表。在该示例中,Fn::GetAZs 函数调用{ "Fn::GetAZs" : "" } 指定将创建堆栈区域的所有可用区。

  • Listeners 是负载均衡路由配置列表,负载均衡路由配置指定负载均衡器接受请求的端口、负载均衡器转发请求的已注册 EC2 实例上的端口,以及用于路由请求的协议。

  • HealthCheck 是 Elastic Load Balancing 用于检查负载均衡器将流量路由到的 EC2 实例的运行状况的配置。在该示例中,HealthCheck 旨在通过 WebServerPort 在 HTTP 协议上指定的端口来获得 EC2 实例的原地址。如果 WebServerPort 为 8888,那么 { "Fn::Join" : [ "", ["HTTP:", { "Ref" : "WebServerPort" }, "/"]]} 函数调用将评估为字符串HTTP:8888/。它还指定 EC2 实例在两项运行状况检查之间拥有 30 秒的时间间隔。Timeout(超时)将定义为 Elastic Load Balancing 等待来自运行状况检查响应的时间(本示例中为 5 秒)。超时期间到期后,Elastic Load Balancing 将标记 EC2 实例运行状况检查不合格。当 EC2 实例未通过连续 5 次运行状况检查时 (UnhealthyThreshold),Elastic Load Balancing 会停止路由流量值该 EC2 实例,直至该实例连续 3 次运行状况检查情况良好,此时 Elastic Load Balancing 将认定 EC2 实例运行良好,并再次向该实例路由流量。

AWS::AutoScaling::ScalingPolicy 资源 WebServerScaleUpPolicy 为按比例放大 Auto Scaling group WebServerGroup 的 Auto Scaling 策略。AdjustmentType 属性将设定为 ChangeInCapacity。这意味着 ScalingAdjustment 代表待添加的实例数目(如果 ScalingAdjustment 为正值,则添加实例;若为负值,则删除实例)。在该实例中,ScalingAdjustment 为 1;因此,执行策略时,策略将从 1 开始逐渐增大 EC2 实例的数量。Cooldown(冷却时间)属性指定任何策略或触发相关操作前,Auto Scaling 将等待 60 秒。

AWS::CloudWatch::Alarm 资源 CPUAlarmHigh 将指定扩展策略 WebServerScaleUpPolicy 作为警报处于 ALARM 状态 (AlarmActions) 下待执行的操作。警报将监测 WebServerGroup Auto Scaling 组中的 EC2 实例(维度)。警报每隔 300 秒(周期)测量在 WebServerGroup(维度)中实例的平均(统计数据)EC2 实例 CPU 使用率(命名空间和标准名称)。当该值(平均 CPU 使用率超过 300 秒)连续 2 个周期(EvaluationPeriod)保持高于 90%(ComparisonOperator 和阈值)的状态,那么警报将进入 ALARM 状态,并且 CloudWatch 将执行上述的 WebServerScaleUpPolicy 策略 (AlarmActions),按比例放大 WebServerGroup。

CPUAlarmLow 警报测量相同的指标,但有一项警报会在 CPU 使用率低于 75% 时触发(ComparisonOperator 和阈值),并执行 WebServerScaleDownPolicy 策略,将 1 个 EC2 实例从 Auto Scaling 组 WebServerGroup 中删除。