本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
步驟 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://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
參數可讓您指定最終使用者在啟動產品時必須提供 AWS Service Catalog 的 Amazon Elastic Compute Cloud (Amazon EC2) 金鑰對名稱。您將在下一個步驟中建立金鑰對。 -
中繼資料 (選用) – 提供範本額外資訊的物件。AWS::CloudFormation::Interface 金鑰會定義最終使用者主控台檢視顯示參數的方式。
ParameterGroups
屬性定義這些參數如何分組以及為這些群組加上標題。ParameterLabels
屬性定義容易記住的參數名稱。當使用者指定參數以啟動以此範本為基礎的產品時,最終使用者主控台檢視將在標題Server size:
之下顯示標記為Instance configuration
的參數,它在標題Key pair:
之下顯示標記為CIDR range:
及Security configuration
的參數。 -
映射 (選用) – 索引鍵和關聯值的映射,可用來指定條件參數值,類似於查詢資料表。您可以使用資源和輸出區段中的 Fn::FindInMap 內部函數,將索引鍵與對應的值配對。上述範本包含區域清單,以及對應至每個 AWS 區域的 Amazon Machine Image (AMI)。 AWS Service Catalog 使用此映射,根據使用者在 中選擇 AWS 的區域來決定要使用的 AMI AWS Management Console。
-
資源 (必要) – 堆疊資源及其屬性。您可以參考範本的資源和輸出區段中的資源。在上述範本中,我們會指定執行 Amazon Linux 的 EC2 執行個體,以及允許 SSH 存取執行個體的安全群組。EC2 執行個體資源的屬性區段會使用使用者類型的資訊來設定執行個體類型和 SSH 存取的金鑰名稱。
AWS CloudFormation 使用目前的 AWS 區域,從先前定義的映射中選取 AMI ID,並將安全群組指派給該區域。安全群組經過設定以允許從使用者指定的 CIDR IP 地址範圍對內存取連接埠 22。
-
輸出 (選用) – 告知使用者產品啟動完成的文字。提供的範本取得啟動執行個體的公有 DNS 名稱並顯示給使用者。使用者需要 DNS 名稱以使用 SSH 連接到執行個體。
如需範本結構頁面的詳細資訊,請參閱AWS CloudFormation 《 使用者指南》中的範本參考。