映射項目 - AWS CloudFormation

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

映射項目

選用的 Mappings 區段會比對索引鍵與對應的一組命名值。例如,如果您想要根據區域來設定值,則可以建立映射,而此映射使用區域名稱做為索引鍵,並包含您要為每個特定區域指定的值。您可以使用 Fn::FindInMap 內部函數來擷取映射中的值。

您不能在 Mappings 區段中包含參數、虛擬參數或內部函數。

語法

Mappings 區段由索引鍵名稱 Mappings 組成。映射中的索引鍵必須是常值字串。值可以是 StringList 類型。下列範例所顯示的 Mappings 區段包含名為 Mapping01 (邏輯名稱) 的單一映射。

在映射內,每個映射都是後面接著另一個映射的索引鍵。該金鑰必須是名稱值對的映射和映射中唯一的映射。

JSON

"Mappings" : { "Mapping01" : { "Key01" : { "Name" : "Value01" }, "Key02" : { "Name" : "Value02" }, "Key03" : { "Name" : "Value03" } } }

YAML

Mappings: Mapping01: Key01: Name: Value01 Key02: Name: Value02 Key03: Name: Value03

範例

基本映射

下列範例顯示具有映射 MappingsRegionMap 區段,內含映射至包含單一字串值之名稱值對的五個索引鍵。索引鍵是區域名稱。每個名稱/值對都是以索引鍵代表之區域中 HVM64 AMI 的 AMI ID。

名稱/值對會有名稱 (在此範例中為 HVM64) 和值。藉由命名這些值,您可以將多組的值映射至一個索引鍵。

JSON

"Mappings" : { "RegionMap" : { "us-east-1" : { "HVM64" : "ami-0ff8a91507f77f867"}, "us-west-1" : { "HVM64" : "ami-0bdb828fd58c52235"}, "eu-west-1" : { "HVM64" : "ami-047bb4163c506cd98"}, "ap-southeast-1" : { "HVM64" : "ami-08569b978cc4dfa10"}, "ap-northeast-1" : { "HVM64" : "ami-06cd52961ce9f0d85"} } }

YAML

Mappings: RegionMap: us-east-1: "HVM64": "ami-0ff8a91507f77f867" us-west-1: "HVM64": "ami-0bdb828fd58c52235" eu-west-1: "HVM64": "ami-047bb4163c506cd98" ap-southeast-1: "HVM64": "ami-08569b978cc4dfa10" ap-northeast-1: "HVM64": "ami-06cd52961ce9f0d85"

具有多個值的映射

下列範例的區域索引鍵映射至兩組值:一個名為 HVM64,另一個名為 HVMG2。

JSON

"RegionMap" : { "us-east-1" : {"HVM64" : "ami-0ff8a91507f77f867", "HVMG2" : "ami-0a584ac55a7631c0c"}, "us-west-1" : {"HVM64" : "ami-0bdb828fd58c52235", "HVMG2" : "ami-066ee5fd4a9ef77f1"}, "eu-west-1" : {"HVM64" : "ami-047bb4163c506cd98", "HVMG2" : "ami-0a7c483d527806435"}, "ap-northeast-1" : {"HVM64" : "ami-06cd52961ce9f0d85", "HVMG2" : "ami-053cdd503598e4a9d"}, "ap-southeast-1" : {"HVM64" : "ami-08569b978cc4dfa10", "HVMG2" : "ami-0be9df32ae9f92309"} }

YAML

RegionMap: us-east-1: HVM64: ami-0ff8a91507f77f867 HVMG2: ami-0a584ac55a7631c0c us-west-1: HVM64: ami-0bdb828fd58c52235 HVMG2: ami-066ee5fd4a9ef77f1 eu-west-1: HVM64: ami-047bb4163c506cd98 HVMG2: ami-0a7c483d527806435 ap-northeast-1: HVM64: ami-06cd52961ce9f0d85 HVMG2: ami-053cdd503598e4a9d ap-southeast-1: HVM64: ami-08569b978cc4dfa10 HVMG2: ami-0be9df32ae9f92309

從映射傳回值

您可以使用 Fn::FindInMap 函數,以根據指定的索引鍵來傳回命名值。下列範例範本包含 Amazon EC2 資源,而其 ImageId 屬性是由 FindInMap 函數指派。該FindInMap函數將 key 指定為創建堆棧的區域(使用AWS::Region 偽參數),並HVM64作為要映射到的值的名稱。

JSON

{ "AWSTemplateFormatVersion" : "2010-09-09", "Mappings" : { "RegionMap" : { "us-east-1" : {"HVM64" : "ami-0ff8a91507f77f867", "HVMG2" : "ami-0a584ac55a7631c0c"}, "us-west-1" : {"HVM64" : "ami-0bdb828fd58c52235", "HVMG2" : "ami-066ee5fd4a9ef77f1"}, "eu-west-1" : {"HVM64" : "ami-047bb4163c506cd98", "HVMG2" : "ami-0a7c483d527806435"}, "ap-northeast-1" : {"HVM64" : "ami-06cd52961ce9f0d85", "HVMG2" : "ami-053cdd503598e4a9d"}, "ap-southeast-1" : {"HVM64" : "ami-08569b978cc4dfa10", "HVMG2" : "ami-0be9df32ae9f92309"} } }, "Resources" : { "myEC2Instance" : { "Type" : "AWS::EC2::Instance", "Properties" : { "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "HVM64"]}, "InstanceType" : "m1.small" } } } }

YAML

AWSTemplateFormatVersion: "2010-09-09" Mappings: RegionMap: us-east-1: HVM64: ami-0ff8a91507f77f867 HVMG2: ami-0a584ac55a7631c0c us-west-1: HVM64: ami-0bdb828fd58c52235 HVMG2: ami-066ee5fd4a9ef77f1 eu-west-1: HVM64: ami-047bb4163c506cd98 HVMG2: ami-0a7c483d527806435 ap-northeast-1: HVM64: ami-06cd52961ce9f0d85 HVMG2: ami-053cdd503598e4a9d ap-southeast-1: HVM64: ami-08569b978cc4dfa10 HVMG2: ami-0be9df32ae9f92309 Resources: myEC2Instance: Type: "AWS::EC2::Instance" Properties: ImageId: !FindInMap [RegionMap, !Ref "AWS::Region", HVM64] InstanceType: m1.small

輸入參數和 Fn::FindInMap

您可以搭配使用輸入參數與 Fn::FindInMap 函數,以參照映射中的特定值。例如,假設您有一份映射至特定 AMI ID 的區域和環境類型清單。您可以使用輸入參數 (EnvironmentType) 來選取您堆疊所使用的 AMI ID。若要判斷區域,請使用AWS::Region偽參數,該參數會取得您在其中建立堆疊的 AWS Region。

JSON

{ "Parameters" : { "EnvironmentType": { "Description": "The environment type", "Type": "String", "Default": "test", "AllowedValues": ["prod", "test"], "ConstraintDescription": "must be a prod or test" } }, "Mappings" : { "RegionAndInstanceTypeToAMIID" : { "us-east-1": { "test": "ami-8ff710e2", "prod": "ami-f5f41398" }, "us-west-2" : { "test" : "ami-eff1028f", "prod" : "ami-d0f506b0" }, ...other regions and AMI IDs... } }, "Resources" : { ...other resources... }, "Outputs" : { "TestOutput" : { "Description" : "Return the name of the AMI ID that matches the region and environment type keys", "Value" : { "Fn::FindInMap" : [ "RegionAndInstanceTypeToAMIID", { "Ref" : "AWS::Region" }, { "Ref" : "EnvironmentType" } ]} } } }

YAML

Parameters: EnvironmentType: Description: The environment type Type: String Default: test AllowedValues: - prod - test ConstraintDescription: must be a prod or test Mappings: RegionAndInstanceTypeToAMIID: us-east-1: test: "ami-8ff710e2" prod: "ami-f5f41398" us-west-2: test: "ami-eff1028f" prod: "ami-d0f506b0" ...other regions and AMI IDs... Resources: ...other resources... Outputs: TestOutput: Description: Return the name of the AMI ID that matches the region and environment type keys Value: !FindInMap [RegionAndInstanceTypeToAMIID, !Ref "AWS::Region", !Ref EnvironmentType]