演练:使用 Amazon CloudFormation Designer 创建基本 Web 服务器 - Amazon CloudFormation
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

演练:使用 Amazon CloudFormation Designer 创建基本 Web 服务器

Amazon CloudFormation Designer 能够以图形化的方式展示您的模板,帮助您了解模板中的资源及其之间的关系。通过使用集成的 JSON 和 YAML 编辑器,可以直接在 Amazon CloudFormation 控制台中轻松修改模板。为说明如何使用这两个组件,我们将使用 Amazon CloudFormation Designer 在 VPC 中构建一个基本 Web 服务器。然后,我们保存此模板并使用它创建 CloudFormation 堆栈。

在此演练中,您将完成以下步骤:

  1. 添加并连接资源。

    当您首次打开 Amazon CloudFormation Designer 时,将从空白模板开始工作。我们将使用 Amazon CloudFormation Designer 开始填充此模板:将 VPC、EC2 实例等资源拖放到模板中。我们还将在它们之间创建链接。例如,我们将使用 Amazon CloudFormation Designer 在互联网网关与 VPC 之间创建连接。

  2. 添加模板参数、映射和输出。

    我们将使用 Amazon CloudFormation Designer 集成编辑器添加其他模板组件以使模板更有用。例如,我们将向模板添加参数,以便您可以在创建堆栈时指定输入值。这样,您就不必总是为了可能经常变化的属性值而编辑模板了。

  3. 指定资源属性。

    我们将再使用 集成编辑器 为资源指定配置设置。

  4. 预置资源

    创建堆栈前,您的任何模板资源都不会启动和运行。我们将使用您刚刚创建的模板启动一个 CloudFormation 堆栈,它将配置在模板中定义的所有资源。

    注意

    CloudFormation 是一种免费服务;但是,您需要为您在堆栈使用的 Amazon 资源付费,费用按每种资源的现价收取。有关 Amazon 定价的详细信息,请参阅 http://aws.amazon.com 上每种产品的详细信息页。

先决条件

本演练假定您具有 Amazon Virtual Private Cloud (Amazon VPC)、Amazon Elastic Compute Cloud (Amazon EC2) 和 CloudFormation 方面的工作经验。为了解上下文,每个过程提供了有关每项资源的一些基本信息。

另外,在开始前,请确保您在要创建堆栈的区域中拥有一个 Amazon EC2 密钥对。有关更多信息,请参阅 Amazon EC2 用户指南(适用于 Linux 实例)中的 Amazon EC2 密钥对

步骤 1:添加并连接资源

我们将使用 Amazon CloudFormation Designer 拖放界面添加 Amazon EC2 实例和网络资源,如 VPC、子网、路由表和互联网网关。添加所有资源后,我们将在它们之间创建连接。例如,我们会将 Internet 网关关联到某个 VPC。

向模板添加资源
  1. 打开 Amazon CloudFormation Designer(https://console.aws.amazon.com/cloudformation/designer)。

  2. 在页面下半部分的 集成编辑器 中,选择编辑 ( )。

  3. 将模板名称更改为 BasicWebServerInVPC,然后按 Enter

    目前,我们拥有一个无效的空白模板。在后续步骤中,我们将添加资源使其有效。

  4. Resource types 窗格中,从 EC2 类别拖动 VPC 资源类型并放到 Canvas 窗格上。

    资源按资源类别组织。我们添加的所有资源都处于 EC2 类别中。

    Amazon CloudFormation Designer 会立即修改模板以包含 VPC 资源,其结果类似于下面的 JSON 代码段。

    "Resources": { "VPC431KO": { "Type": "AWS::EC2::VPC", "Properties": {}, "Metadata": { "AWS::CloudFormation::Designer": { "id": "445730ea-0d11-45ba-b6ac-12345EXAMPLE" } } } }

    YAML 代码段应该类似于下列内容。

    Resources: VPC431KO: Type: 'AWS::EC2::VPC' Properties: {} Metadata: 'AWS::CloudFormation::Designer': id: 9430b008-7a03-41ed-b63e-12345EXAMPLE

    注意,我们仍需指定 VPC 属性,如 VPC 的 CIDR 块。此操作稍后进行。对于将要添加的所有资源,都需要执行此操作。

  5. 重命名 VPC。

    注意

    重命名资源时,更改的是其逻辑 ID,即模板中引用的名称(不是 CloudFormation 创建此资源时分配的名称)。有关更多信息,请参阅资源

    1. 选择 VPC 资源。

    2. 在 集成编辑器 中,选择编辑图标 ( )。

    3. 将名称更改为 VPC,然后选择 Enter

    接下来,我们将向此 VPC 添加资源。

  6. 拖动 VPC 资源的一角来放大它,使其大到足以容纳几项更多的资源。

    我们需要添加一个子网,因为您无法将托管此网站的 EC2 实例直接添加到 VPC 中;实例必须位于子网中。

  7. 在 VPC 中添加一个 Subnet 资源类型,并将之重命名为 PublicSubnet

    我们将使用此子网来分配 VPC 中的 IP 地址范围(您可以将之关联到其他 Amazon 资源,如 Amazon EC2 实例)。

    在 VPC 中添加子网时,Amazon CloudFormation Designer 会将子网自动关联到 VPC。此关联是一种容器模型,即容器内的资源自动关联到容器资源。

  8. PublicSubnet 资源内添加一个 Instance 资源类型,并将之重命名为 WebServerInstance

    此实例是一种虚拟计算环境,您将在其中托管一个基本网站。与子网和 VPC 的情形一样,在子网中添加此实例会使其自动关联到此子网。

  9. 在 VPC 中添加一个 SecurityGroup 资源类型,并将之重命名为 WebServerSecurityGroup

    安全组是一种虚拟防火墙,它负责控制 Web 服务器实例的入站和出站流量。VPC 中的实例也需要它。此外,我们还需要将 Web 服务器实例与此安全组关联 (在后面指定实例的属性时进行)。

  10. 在 VPC 外的任何地方添加一个 InternetGateway 资源类型,并将之重命名为 InternetGateway

    Internet 网关使 VPC 内的实例能够与 Internet 进行通信。如果没有 Internet 网关,任何人都无法访问您的网站。

    虽然您可以将 Internet 网关拖放到 VPC 内,但这不会创建与 VPC 的关联。Internet 网关不遵守容器模型;您必须拖动一个从 Internet 网关到 VPC 的连接,如下一步所述。

  11. InternetGateway 资源和 VPC 资源之间创建连接。

    1. InternetGateway 资源上,将鼠标悬停在 Internet 网关连接 (AWS::EC2::VPCGatewayAttachment) 上。

    2. 拖动一个连接到 VPC。

      有效目标资源的边界会改变颜色。在本例中,VPC 是唯一有效的目标资源。此连接会创建一个将 Internet 网关与 VPC 关联的连接资源。

  12. 接下来,我们需要添加路由表和路由,以指定如何引导来自子网内部的网络流量。在 VPC 内添加一个 RouteTable,并将之重命名为 PublicRouteTable

    这会为 VPC 关联一个新的路由表。

  13. 要向路由表添加路由规则,请在 PublicRouteTable 资源内添加一个 Route 资源类型,并将之重命名为 PublicRoute

    我们将使用此路由指定将流量定向到何处。

  14. 对于公有路由,我们需要使用 Internet 网关作为目标。使用 GatewayId 创建从 PublicRoute 资源到 Internet 网关的连接 (采用与在 Internet 网关与 VPC 之间创建连接相同的方式)。

    在将互联网网关与 VPC 关联之前,CloudFormation 无法将路由与互联网网关关联。这意味着我们需要创建对 Internet 网关-VPC 连接的显式依赖关系,如下一步所述。有关更多信息,请参阅DependsOn 属性

  15. PublicRoute 资源与 Internet 网关-VPC 连接之间创建显式的依赖关系。

    1. PublicRoute 资源上,将鼠标指针悬停在 DependsOn 点上。

    2. 拖动一个连接到 Internet 网关-VPC 连接 (AWS::EC2::VPCGatewayAttachment)。

      通过 DependsOn 连接,Amazon CloudFormation Designer 能够创建原始资源依赖于目标资源的依赖关系(DependsOn 属性)。在本例中,Amazon CloudFormation Designer 添加一个 DependsOn 属性到 PublicRoute 资源,并指定网关-VPC 连接为依赖关系。

  16. 再创建一个从 WebServerInstance 资源到 PublicRoute 资源的依赖关系。

    WebServerInstance 资源依靠公有路由将流量路由到 Internet。如果没有此公有路由,当实例配置和应用程序部署完成时,实例无法发送信号 (使用 cfn-signal 帮助程序脚本) 通知 CloudFormation。

  17. 拖动从 PublicRouteTable 资源到 PublicSubnet 资源的连接以关联路由表和子网。

    现在,公有子网将使用公有路由表来定向流量。

  18. 在 Amazon CloudFormation Designer 工具栏中,使用 File(文件)菜单(文件图标)将模板保存在本地。

    Amazon CloudFormation Designer 会将模板保存到您的硬盘上。稍后您可以使用此模板创建堆栈。我们建议您定期保存模板,以免丢失更改。

在此步骤中,我们在模板中添加了七个资源,并将它们的逻辑 ID 重命名为易记名称。我们还为大多数资源建立了视觉连接,以创建关联和依赖关系。但在使用此模板创建堆栈前,我们还需要创建其他几个连接 (如将实例与安全组关联),并为每个资源指定属性。在下一步中,我们将演练使用 Amazon CloudFormation Designer 集成编辑器修改模板的其他组件,如输入参数。

步骤 2:添加参数、映射和输出

指定资源属性前,我们需要添加其他模板组件,以便在多个环境中复用此模板。在该步骤中,我们将使用 Amazon CloudFormation Designer 集成编辑器添加参数、映射和输出。然后,我们就可以在指定资源属性时引用这些参数和映射了。此演练提供示例 JSON 和 YAML,您可以将它复制粘贴到 集成编辑器 中。

添加参数

参数是您在创建堆栈时指定的输入值。它们可用于传入值,让您不必在模板中使用硬编码值。例如,您不必在模板中硬编码 Web 服务器的实例类型;您可以在创建堆栈时使用参数指定实例类型。利用这种方法,您可以使用同一个模板创建多个具有不同实例类型的 Web 服务器。有关更多信息,请参阅参数

  1. 单击 Amazon CloudFormation Designer 画布上的空白区域。

    根据您的选择, 集成编辑器 会显示可以编辑的模板级或资源级组件。在模板级,您可以编辑模板的所有其他部分,如模板参数、映射、输出,但资源部分除外。在资源级,您可以编辑资源属性和属性。

    单击画布上的空白区域可编辑模板级组件。要编辑资源级组件,请选中某个资源。

  2. 在集成编辑器窗格中,在 Components(组件)视图中选择 Parameters(参数)选项卡。

  3. 复制下面代码段中的参数并将其粘贴到 集成编辑器 中。

    下面的 JSON 代码段添加了用于指定 Web 服务器实例类型的参数、用于对 Web 服务器进行 SSH 访问的 Amazon EC2 密钥对名称,以及可用于通过 SSH 访问 Web 服务器的 IP 地址范围。

    { "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." }, "KeyName": { "Description": "Name of an EC2 KeyPair to enable SSH access to the instance.", "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 access the web server using SSH.", "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." } } }

    下面是 YAML 格式的相同代码段。

    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. KeyName: Description: Name of an EC2 KeyPair to enable SSH access to the instance. 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 access the web server using SSH.' 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.
添加映射

映射是与一组名称值对关联的一组键。它们可用于根据输入参数值指定值。在此演练中,我们将使用映射基于实例类型和创建堆栈的区域为 EC2 实例指定一个 AMI ID。有关更多信息,请参阅映像

  1. 在集成编辑器窗格中,选择映射选项卡。

  2. 将以下 JSON 映射复制粘贴到 集成编辑器 中。

    { "Mappings" : { "AWSInstanceType2Arch" : { "t1.micro" : { "Arch" : "HVM64" }, "t2.nano" : { "Arch" : "HVM64" }, "t2.micro" : { "Arch" : "HVM64" }, "t2.small" : { "Arch" : "HVM64" }, "t2.medium" : { "Arch" : "HVM64" }, "t2.large" : { "Arch" : "HVM64" }, "m1.small" : { "Arch" : "HVM64" }, "m1.medium" : { "Arch" : "HVM64" }, "m1.large" : { "Arch" : "HVM64" }, "m1.xlarge" : { "Arch" : "HVM64" }, "m2.xlarge" : { "Arch" : "HVM64" }, "m2.2xlarge" : { "Arch" : "HVM64" }, "m2.4xlarge" : { "Arch" : "HVM64" }, "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" : "HVM64" }, "c1.xlarge" : { "Arch" : "HVM64" }, "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" } }, "AWSRegionArch2AMI" : { "us-east-1" : {"HVM64" : "ami-0ff8a91507f77f867", "HVMG2" : "ami-0a584ac55a7631c0c"}, "us-west-2" : {"HVM64" : "ami-a0cfeed8", "HVMG2" : "ami-0e09505bc235aa82d"}, "us-west-1" : {"HVM64" : "ami-0bdb828fd58c52235", "HVMG2" : "ami-066ee5fd4a9ef77f1"}, "eu-west-1" : {"HVM64" : "ami-047bb4163c506cd98", "HVMG2" : "ami-0a7c483d527806435"}, "eu-west-2" : {"HVM64" : "ami-f976839e", "HVMG2" : "NOT_SUPPORTED"}, "eu-west-3" : {"HVM64" : "ami-0ebc281c20e89ba4b", "HVMG2" : "NOT_SUPPORTED"}, "eu-central-1" : {"HVM64" : "ami-0233214e13e500f77", "HVMG2" : "ami-06223d46a6d0661c7"}, "ap-northeast-1" : {"HVM64" : "ami-06cd52961ce9f0d85", "HVMG2" : "ami-053cdd503598e4a9d"}, "ap-northeast-2" : {"HVM64" : "ami-0a10b2721688ce9d2", "HVMG2" : "NOT_SUPPORTED"}, "ap-northeast-3" : {"HVM64" : "ami-0d98120a9fb693f07", "HVMG2" : "NOT_SUPPORTED"}, "ap-southeast-1" : {"HVM64" : "ami-08569b978cc4dfa10", "HVMG2" : "ami-0be9df32ae9f92309"}, "ap-southeast-2" : {"HVM64" : "ami-09b42976632b27e9b", "HVMG2" : "ami-0a9ce9fecc3d1daf8"}, "ap-south-1" : {"HVM64" : "ami-0912f71e06545ad88", "HVMG2" : "ami-097b15e89dbdcfcf4"}, "us-east-2" : {"HVM64" : "ami-0b59bfac6be064b78", "HVMG2" : "NOT_SUPPORTED"}, "ca-central-1" : {"HVM64" : "ami-0b18956f", "HVMG2" : "NOT_SUPPORTED"}, "sa-east-1" : {"HVM64" : "ami-07b14488da8ea02a0", "HVMG2" : "NOT_SUPPORTED"}, "cn-north-1" : {"HVM64" : "ami-0a4eaf6c4454eda75", "HVMG2" : "NOT_SUPPORTED"}, "cn-northwest-1" : {"HVM64" : "ami-6b6a7d09", "HVMG2" : "NOT_SUPPORTED"} } } }

    下面是 YAML 格式的相同映射。

    Mappings: AWSInstanceType2Arch: t1.micro: Arch: HVM64 t2.nano: Arch: HVM64 t2.micro: Arch: HVM64 t2.small: Arch: HVM64 t2.medium: Arch: HVM64 t2.large: Arch: HVM64 m1.small: Arch: HVM64 m1.medium: Arch: HVM64 m1.large: Arch: HVM64 m1.xlarge: Arch: HVM64 m2.xlarge: Arch: HVM64 m2.2xlarge: Arch: HVM64 m2.4xlarge: Arch: HVM64 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: HVM64 c1.xlarge: Arch: HVM64 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 AWSRegionArch2AMI: us-east-1: HVM64: ami-0ff8a91507f77f867 HVMG2: ami-0a584ac55a7631c0c us-west-2: HVM64: ami-a0cfeed8 HVMG2: ami-0e09505bc235aa82d us-west-1: HVM64: ami-0bdb828fd58c52235 HVMG2: ami-066ee5fd4a9ef77f1 eu-west-1: HVM64: ami-047bb4163c506cd98 HVMG2: ami-0a7c483d527806435 eu-west-2: HVM64: ami-f976839e HVMG2: NOT_SUPPORTED eu-west-3: HVM64: ami-0ebc281c20e89ba4b HVMG2: NOT_SUPPORTED eu-central-1: HVM64: ami-0233214e13e500f77 HVMG2: ami-06223d46a6d0661c7 ap-northeast-1: HVM64: ami-06cd52961ce9f0d85 HVMG2: ami-053cdd503598e4a9d ap-northeast-2: HVM64: ami-0a10b2721688ce9d2 HVMG2: NOT_SUPPORTED ap-northeast-3: HVM64: ami-0d98120a9fb693f07 HVMG2: NOT_SUPPORTED ap-southeast-1: HVM64: ami-08569b978cc4dfa10 HVMG2: ami-0be9df32ae9f92309 ap-southeast-2: HVM64: ami-09b42976632b27e9b HVMG2: ami-0a9ce9fecc3d1daf8 ap-south-1: HVM64: ami-0912f71e06545ad88 HVMG2: ami-097b15e89dbdcfcf4 us-east-2: HVM64: ami-0b59bfac6be064b78 HVMG2: NOT_SUPPORTED ca-central-1: HVM64: ami-0b18956f HVMG2: NOT_SUPPORTED sa-east-1: HVM64: ami-07b14488da8ea02a0 HVMG2: NOT_SUPPORTED cn-north-1: HVM64: ami-0a4eaf6c4454eda75 HVMG2: NOT_SUPPORTED cn-northwest-1: HVM64: ami-6b6a7d09 HVMG2: NOT_SUPPORTED
添加输出

输出用于声明您需要开放给 describe stacks API 调用或通过 CloudFormation 控制台堆栈的 Outputs 选项卡显示的值。在此演练中,我们将输出网站的 URL,以便您轻松查看创建后的网站。有关更多信息,请参阅输出

  1. 在集成编辑器窗格中,选择输出选项卡。

  2. 将以下 JSON 输出复制粘贴到 集成编辑器 中。

    此输出使用 Fn::GetAtt 内部函数获取 Web 服务器实例的公有 IP。

    { "Outputs": { "URL": { "Value": { "Fn::Join": [ "", [ "http://", { "Fn::GetAtt": [ "WebServerInstance", "PublicIp" ] } ] ] }, "Description": "Newly created application URL" } } }

    下面是 YAML 格式的相同输出。

    Outputs: URL: Value: !Join - '' - - 'http://' - !GetAtt - WebServerInstance - PublicIp Description: Newly created application URL
  3. 再次保存模板,以免丢失更改。您可以安全地将更改保存到在前一部分中创建的文件。

现在,模板参数、映射和输出均已到位,我们可以指定资源属性了。

步骤 3:指定资源属性

很多资源都有必需的属性用于定义资源的配置或设置,例如:要为 Web 服务器使用何种实例类型。与上一步中的做法类似,我们将使用 Amazon CloudFormation Designer 集成编辑器指定资源属性。我们提供了示例 JSON 和 YAML,您可以将它们复制粘贴到 集成编辑器 中。

指定资源属性
  1. 在 Amazon CloudFormation Designer 画布上,选择 VPC 资源。

    集成编辑器 显示您可以编辑的资源级组件,如资源属性和属性。

  2. 在集成编辑器窗格中,选择属性选项卡。

  3. 复制以下 JSON 代码段,并将其粘贴到集成编辑器中的属性大括号 ({}) 之间。

    此代码段指定 DNS 设置和 VPC 的 CIDR 块。

    "EnableDnsSupport": "true", "EnableDnsHostnames": "true", "CidrBlock": "10.0.0.0/16"

    对于 YAML,在 Properties: 后键入新行,然后粘贴下面的代码段。

    EnableDnsSupport: 'true' EnableDnsHostnames: 'true' CidrBlock: 10.0.0.0/16
    注意

    为提高效率,我们提供了可供您复制粘贴的 JSON 和 YAML 代码段。但请注意,此编辑器具有自动完成功能,可让您方便地手动指定每个属性。有关更多信息,请参阅集成的 JSON 和 YAML 编辑器

  4. 为以下资源重复这一过程:

    PublicSubnet

    在 VPC ID 属性后面添加下面的 CIDR 块属性。Amazon CloudFormation将子网拖放到 VPC 中的时候,Designer 会自动添加 VPC ID 属性。

    注意

    您会看到 Amazon CloudFormation Designer 为您自动创建了其他几个关联。您只需添加以粗体显示的新属性。

    JSON

    "VpcId": { "Ref": "VPC" }, "CidrBlock": "10.0.0.0/24"

    YAML

    VpcId: !Ref VPC CidrBlock: 10.0.0.0/24
    PublicRoute

    添加下面的目标 CIDR 块属性(该属性将所有流量定向到 Internet 网关):

    JSON

    "DestinationCidrBlock": "0.0.0.0/0", "RouteTableId": { "Ref": "PublicRouteTable" }, "GatewayId": { "Ref": "InternetGateway" }

    YAML

    DestinationCidrBlock: 0.0.0.0/0 RouteTableId: !Ref PublicRouteTable GatewayId: !Ref InternetGateway
    WebServerSecurityGroup

    添加下面的入站规则,用以确定哪些流量可以到达 Web 服务器实例。这些规则允许所有的 HTTP 和特定的 SSH 流量 (在创建堆栈时通过参数值指定) 通过。

    JSON

    "VpcId": { "Ref": "VPC" }, "GroupDescription" : "Allow access from HTTP and SSH traffic", "SecurityGroupIngress": [ { "IpProtocol": "tcp", "FromPort": 80, "ToPort": 80, "CidrIp": "0.0.0.0/0" }, { "IpProtocol": "tcp", "FromPort": 22, "ToPort": 22, "CidrIp": { "Ref": "SSHLocation" } } ]

    YAML

    VpcId: !Ref VPC GroupDescription: Allow access from HTTP and SSH traffic SecurityGroupIngress: - IpProtocol: tcp FromPort: 80 ToPort: 80 CidrIp: 0.0.0.0/0 - IpProtocol: tcp FromPort: 22 ToPort: 22 CidrIp: !Ref SSHLocation
    WebServerInstance

    您需要为 Web 服务器实例指定一些属性,出于演示目的,我们只重点介绍部分属性。InstanceTypeImageId 属性使用我们在上一部分中指定的参数和映射值。创建堆栈时,需要以参数值的形式指定实例类型。ImageId 值是基于堆栈区域和指定的实例类型的映射。

    NetworkInterfaces 属性指定 Web 服务器实例的网络设置。该属性让我们能够将安全组和子网关联到实例。虽然 Amazon CloudFormation Designer 使用了 SubnetId 属性来关联实例和子网,但我们需要使用 NetworkInterfaces 属性,因为这是为 Web 服务器提供公有 IP 的唯一方式。指定 NetworkInterfaces 属性时,必须在该属性中指定子网和安全组。

    UserData 属性中,我们指定在实例启动并运行后运行的配置脚本。所有配置信息均在实例的元数据中定义 (我们将在下一步中添加)。

    使用下面的代码段替换所有属性:

    重要

    请不要将此代码段附加到现有属性。

    JSON

    "InstanceType": { "Ref": "InstanceType" }, "ImageId": { "Fn::FindInMap": [ "AWSRegionArch2AMI", { "Ref": "AWS::Region" }, { "Fn::FindInMap": [ "AWSInstanceType2Arch", { "Ref": "InstanceType" }, "Arch" ] } ] }, "KeyName": { "Ref": "KeyName" }, "NetworkInterfaces": [ { "GroupSet": [ { "Ref": "WebServerSecurityGroup" } ], "AssociatePublicIpAddress": "true", "DeviceIndex": "0", "DeleteOnTermination": "true", "SubnetId": { "Ref": "PublicSubnet" } } ], "UserData": { "Fn::Base64": { "Fn::Join": [ "", [ "#!/bin/bash -xe\n", "yum install -y aws-cfn-bootstrap\n", "# Install the files and packages from the metadata\n", "/opt/aws/bin/cfn-init -v ", " --stack ", { "Ref": "AWS::StackName" }, " --resource WebServerInstance ", " --configsets All ", " --region ", { "Ref": "AWS::Region" }, "\n", "# Signal the status from cfn-init\n", "/opt/aws/bin/cfn-signal -e $? ", " --stack ", { "Ref": "AWS::StackName" }, " --resource WebServerInstance ", " --region ", { "Ref": "AWS::Region" }, "\n" ] ] } }

    YAML

    InstanceType: !Ref InstanceType ImageId: !FindInMap - AWSRegionArch2AMI - !Ref 'AWS::Region' - !FindInMap - AWSInstanceType2Arch - !Ref InstanceType - Arch KeyName: !Ref KeyName NetworkInterfaces: - GroupSet: - !Ref WebServerSecurityGroup AssociatePublicIpAddress: 'true' DeviceIndex: '0' DeleteOnTermination: 'true' SubnetId: !Ref PublicSubnet UserData: !Base64 'Fn::Join': - '' - - | #!/bin/bash -xe - | yum install -y aws-cfn-bootstrap - | # Install the files and packages from the metadata - '/opt/aws/bin/cfn-init -v ' - ' --stack ' - !Ref 'AWS::StackName' - ' --resource WebServerInstance ' - ' --configsets All ' - ' --region ' - !Ref 'AWS::Region' - |+ - | # Signal the status from cfn-init - '/opt/aws/bin/cfn-signal -e $? ' - ' --stack ' - !Ref 'AWS::StackName' - ' --resource WebServerInstance ' - ' --region ' - !Ref 'AWS::Region' - |+
  5. 将 Web 服务器配置元数据添加到 WebServerInstance 资源。

    1. 选择 WebServerInstance 资源,然后在集成编辑器窗格中选择元数据选项卡。

    2. 如果以 JSON 格式编写模板:在 Metadata 括号 ({}) 内、AWS::CloudFormation::Designer 右括号后面添加一个逗号 (,)。

    3. AWS::CloudFormation::Designer 属性后添加下面的代码段,指示 cfn-init 帮助程序脚本启动 Web 服务器并创建基本网页。

      JSON

      "AWS::CloudFormation::Init" : { "configSets" : { "All" : [ "ConfigureSampleApp" ] }, "ConfigureSampleApp" : { "packages" : { "yum" : { "httpd" : [] } }, "files" : { "/var/www/html/index.html" : { "content" : { "Fn::Join" : ["\n", [ "<h1>Congratulations, you have successfully launched the AWS CloudFormation sample.</h1>" ]]}, "mode" : "000644", "owner" : "root", "group" : "root" } }, "services" : { "sysvinit" : { "httpd" : { "enabled" : "true", "ensureRunning" : "true" } } } } }

      YAML

      'AWS::CloudFormation::Init': configSets: All: - ConfigureSampleApp ConfigureSampleApp: packages: yum: httpd: [] files: /var/www/html/index.html: content: !Join - |+ - - >- <h1>Congratulations, you have successfully launched the AWS CloudFormation sample.</h1> mode: '000644' owner: root group: root services: sysvinit: httpd: enabled: 'true' ensureRunning: 'true'
  6. 在 Amazon CloudFormation Designer 工具栏上,选择 Validate template(验证模板)( )以检查模板中的语法错误。

    查看并修复 Messages 窗格中的错误,然后再次验证模板。如果未看到错误,则说明您的模板语法上是有效的。

  7. 保存完成的模板,以保存所有更改。

现在,您已拥有可在 VPC 中创建基本 Web 服务器的完整 CloudFormation 模板。为创建模板,我们先通过 Amazon CloudFormation Designer 画布窗格添加并连接了模板资源。然后,我们使用 集成编辑器 添加其他模板组件并指定资源属性。在下一步中,我们将使用此模板创建堆栈。

步骤 4:配置资源

要创建堆栈,您可以从 Amazon CloudFormation Designer 中启动 CloudFormation Create Stack Wizard(创建堆栈向导)。我们将使用在上一步中创建的模板创建一个 CloudFormation 堆栈。在 CloudFormation 配置所有资源后,您将拥有一个启动并正常运行的基本网站。

要创建 堆栈,请执行以下操作:
  1. 在 Amazon CloudFormation Designer 工具栏上,选择 Create Stack(创建堆栈)(云图标)。

    Amazon CloudFormation Designer 将在 S3 桶中打开模板,然后启动 CloudFormation Create Stack Wizard(创建堆栈向导)。CloudFormation 使用它在您每次上传模板时创建的同一 S3 桶。

  2. CloudFormation 会自动填充模板 URL;选择 Next

  3. Specify Details 部分的 Stack name 字段中,输入堆栈名称。对于该示例,请使用 BasicWebServerStack

  4. Parameters 部分的 KeyName 字段中,输入要创建堆栈的同一区域中的有效 Amazon EC2 密钥对名称。

  5. 保持其他默认参数值不变,然后选择 Next

  6. 在本演练中,您无需添加标记或指定高级设置,因此请选择 Next

  7. 确保堆栈名称和 Amazon EC2 密钥对名称正确,然后选择 Create

Amazon CloudFormation 可能需要几分钟时间创建堆栈。要监控进度,可查看堆栈事件。有关查看堆栈事件的更多信息,请参阅 在 Amazon Web Services Management Console上查看 Amazon CloudFormation 堆栈数据和资源。堆栈创建后,查看堆栈输出并前往示例网站 URL,确认网站正在运行。有关更多信息,请参阅在 Amazon Web Services Management Console上查看 Amazon CloudFormation 堆栈数据和资源

现在,您已使用 Amazon CloudFormation Designer 成功创建模板并启动了堆栈,您可以在下面的演练中使用此堆栈了:演练:使用 Amazon CloudFormation Designer 修改堆栈的模板(修改模板以创建可扩展的 Web 服务器)。