步骤 1:下载 AWS CloudFormation 模板 - AWS Service Catalog

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

步骤 1:下载 AWS CloudFormation 模板

您可以使用 AWS CloudFormation 模板来配置和预配置产品组合和产品。模板是 JSON 或 YAML 格式的文本文件,描述了您希望预配置的资源。有关更多信息,请参阅 https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-formats.html 用户指南 中的AWS CloudFormation模板格式。您可以使用 AWS CloudFormation 编辑器或自己选择的文本编辑器创建和保存模板。在本教程中,我们提供了一个简单模板来帮助您入门。此模板会启动为 SSH 访问而配置的单个 Linux 实例。

注意

使用 AWS CloudFormation 模板需要特殊权限。在您开始之前,确保您拥有正确的权限。有关更多信息,请参阅 入门库 中的先决条件。

模板下载

为本教程提供的示例模板,development-environment.template,可在以下网址获得:https://awsdocs.s3.amazonaws.com/servicecatalog/development-environment.template

模板概述

示例模板的文本如下所示:

{ "AWSTemplateFormatVersion" : "2010-09-09", "Description" : "AWS Service Catalog sample template. Creates an Amazon EC2 instance running the Amazon Linux AMI. The AMI is chosen based on the region in which the stack is run. This example creates an EC2 security group for the instance to give you SSH access. **WARNING** This template creates an Amazon EC2 instance. You will be billed for the AWS resources used if you create a stack from this template.", "Parameters" : { "KeyName": { "Description" : "Name of an existing EC2 key pair for SSH access to the EC2 instance.", "Type": "AWS::EC2::KeyPair::KeyName" }, "InstanceType" : { "Description" : "EC2 instance type.", "Type" : "String", "Default" : "t2.micro", "AllowedValues" : [ "t2.micro", "t2.small", "t2.medium", "m3.medium", "m3.large", "m3.xlarge", "m3.2xlarge" ] }, "SSHLocation" : { "Description" : "The IP address range that can SSH to the EC2 instance.", "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." } }, "Metadata" : { "AWS::CloudFormation::Interface" : { "ParameterGroups" : [{ "Label" : {"default": "Instance configuration"}, "Parameters" : ["InstanceType"] },{ "Label" : {"default": "Security configuration"}, "Parameters" : ["KeyName", "SSHLocation"] }], "ParameterLabels" : { "InstanceType": {"default": "Server size:"}, "KeyName": {"default": "Key pair:"}, "SSHLocation": {"default": "CIDR range:"} } } }, "Mappings" : { "AWSRegionArch2AMI" : { "us-east-1" : { "HVM64" : "ami-08842d60" }, "us-west-2" : { "HVM64" : "ami-8786c6b7" }, "us-west-1" : { "HVM64" : "ami-cfa8a18a" }, "eu-west-1" : { "HVM64" : "ami-748e2903" }, "ap-southeast-1" : { "HVM64" : "ami-d6e1c584" }, "ap-northeast-1" : { "HVM64" : "ami-35072834" }, "ap-southeast-2" : { "HVM64" : "ami-fd4724c7" }, "sa-east-1" : { "HVM64" : "ami-956cc688" }, "cn-north-1" : { "HVM64" : "ami-ac57c595" }, "eu-central-1" : { "HVM64" : "ami-b43503a9" } } }, "Resources" : { "EC2Instance" : { "Type" : "AWS::EC2::Instance", "Properties" : { "InstanceType" : { "Ref" : "InstanceType" }, "SecurityGroups" : [ { "Ref" : "InstanceSecurityGroup" } ], "KeyName" : { "Ref" : "KeyName" }, "ImageId" : { "Fn::FindInMap" : [ "AWSRegionArch2AMI", { "Ref" : "AWS::Region" }, "HVM64" ] } } }, "InstanceSecurityGroup" : { "Type" : "AWS::EC2::SecurityGroup", "Properties" : { "GroupDescription" : "Enable SSH access via port 22", "SecurityGroupIngress" : [ { "IpProtocol" : "tcp", "FromPort" : "22", "ToPort" : "22", "CidrIp" : { "Ref" : "SSHLocation"} } ] } } }, "Outputs" : { "PublicDNSName" : { "Description" : "Public DNS name of the new EC2 instance", "Value" : { "Fn::GetAtt" : [ "EC2Instance", "PublicDnsName" ] } }, "PublicIPAddress" : { "Description" : "Public IP address of the new EC2 instance", "Value" : { "Fn::GetAtt" : [ "EC2Instance", "PublicIp" ] } } } }
模板资源

模板声明在启动产品时将创建的资源。它包含以下部分:

  • AWSTemplateFormatVersion(可选)-用于创建此AWS模板的模板格式的版本。最新的模板格式版本是 2010-09-09,并且它是目前唯一的有效值。

  • 描述(可选)- 模板的描述。

  • 参数(可选)- 用户必须指定的用于启动产品的参数。对于每个参数,模板包含一个说明,还有键入的值必须满足的约束。有关约束的更多信息,请参阅 使用 AWS Service Catalog 约束

    使用 KeyName 参数可以指定 Amazon Elastic Compute Cloud (Amazon EC2) 密钥对名称,最终用户使用 AWS Service Catalog 启动您的产品时必须提供该名称。您将在接下来的步骤中创建密钥对。

  • 元数据(可选)- 提供有关模板的其他信息的对象。AWS::CloudFormation: Interfac e 键定义了最终用户控制台视图如何显示参数。ParameterGroups 属性定义如何对参数分组以及这些组的标题。ParameterLabels 属性定义容易记住的参数名称。当用户指定参数来启动基于此模板的产品时,最终用户控制台视图在标题 Server size: 下显示标记为 Instance configuration 的参数,并在标题 Key pair: 下显示标记为 CIDR range:Security configuration 的参数。

  • 映射(可选)- 可用来指定条件参数值的密钥和关键值的映射,与查找表类似。您可以使用 “资源” 和 “输出” 部分中的 Fn:: FindInMap 内部函数将键与相应的值进行匹配。上述模板包含了对应每项的 AWS 区域和亚马逊机器映像(AMI)列表。AWS Service Catalog 使用此映射根据用户在 AWS Management Console 中选择的 AWS 区域确定要使用的 AMI。

  • 资源(必需)- 堆栈资源及其属性。您可引用模板的资源输出部分中的资源。在上述模板中,我们指定了一个运行 Amazon Linux 的 EC2 实例和一个允许 SSH 对该实例访问权限的安全组。EC2 实例资源的属性部分使用用户键入的信息来配置实例类型和 SSH 访问的密钥名称。

    AWS CloudFormation 使用当前 AWS 区域从之前定义的映射中选择 AMI ID 并向其分配安全组。安全组已配置为允许端口 22 上来自用户指定的 CIDR IP 地址范围的入站访问。

  • 输出(可选)- 告知用户产品启动完成的时间的文本。提供的模板获得已启动实例的公有 DNS 名称并将其显示给用户。用户需要此 DNS 名称来使用 SSH 连接到实例。

    有关模板剖析页面的更多信息,请参阅《AWS CloudFormation 用户指南》中的模板参考