映像 - Amazon CloudFormation
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

映像

可选的 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 函数根据指定的密钥返回命名的值。以下示例模板包含由 FindInMap 函数分配其 ImageId 属性的 Amazon EC2 资源。FindInMap 函数将密钥指定为创建堆栈所在的区域(使用 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 伪参数,该参数用于获取创建堆栈时所在的 Amazon 区域。

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]