步驟 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輯器或您選擇的文字編輯器來建立和儲存範本。在本教程中,我們提供了一個簡單的模板,因此您可以開始使用。範本會啟動針對安全殼層存取設定的單一 Linux 執行個體。

注意

使用AWS CloudFormation範本需要特殊權限。開始之前,請確定您擁有正確的權限。如需詳細資訊,請參閱中的先決條件入門資源庫

範本下載

本教學課程所提供的範例範本可在 https://awsdocs.s3.amazonaws.com/servicecatalog/development-environment.template 取得。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 年 9 月 09 日,是目前唯一有效的值。

  • 說明 (選擇性) — 範本的描述。

  • 參數 (選用) — 您的使用者必須指定才能啟動產品的參數。範本包含每個參數的描述和限制,輸入的值必須符合這些限制。如需限制條件的詳細資訊,請參閱 使用 AWS Service Catalog 限制

    KeyName參數可讓您指定 Amazon Elastic Compute Cloud (Amazon EC2) key pair 名稱,最終使用AWS Service Catalog者在啟動產品時必須提供這個名稱。您將在下一個步驟中建立金鑰對。

  • 中繼資料 (選用) — 提供有關範本其他資訊的物件。:: AWSCloudFormation: 介面鍵定義一般使用者主控台檢視如何顯示參數。ParameterGroups 屬性定義這些參數如何分組以及為這些群組加上標題。ParameterLabels 屬性定義容易記住的參數名稱。當使用者指定參數以啟動以此範本為基礎的產品時,最終使用者主控台檢視將在標題 Server size: 之下顯示標記為 Instance configuration 的參數,它在標題 Key pair: 之下顯示標記為 CIDR range:Security configuration 的參數。

  • 對應 (選用) — 索引鍵與關聯值的對映,可用來指定條件參數值,類似於查閱表。您可以使用「資源」和「輸出」區段中的 Fn:: FindInMap 內建函數來比對索引鍵與對應的值。上面的範本包括AWS區域清單和對應於每個區域的 Amazon 機器映像 (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使用指南中的範本參考