AWS CloudFormation
User Guide (API Version 2010-05-15)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。请点击 Amazon AWS 入门,可查看中国地区的具体差异

映像

可选的 Mappings 部分将密钥与对应的一组命名值相匹配。例如,如果您想根据区域设置值,您可以创建将区域名称用作密钥且其中含有您想为每个特定区域指定的值的映射。您使用 Fn::FindInMap 内部函数来检索映射中的值。

不得在 Mappings 部分包含参数、伪参数或内部函数。

语法

Mappings 部分包括键名称 Mappings。映射中的键和值必须为文字字符串。 以下示例显示了 Mappings 部分,其中包含一个名为 Mapping01(逻辑名称)的映射。

在映射中,每个映射是一个键,后跟另一个映射。键用于标识名称值对映射,且在映射中必须唯一。名称只能包含字母数字字符 (A-Za-z0-9)。

JSON

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

YAML

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

示例

基本映射

以下示例显示的是带映射 RegionMapMappings 部分,该映射包含五个映射到含单字符串值的名称-值对的密钥。密钥为区域名称。对于密钥所表示区域内的 32 位 AMI,每个名称-值对均为 AMI ID。

名称-值对中有一个名称(示例中的 32)和一个值。您可以通过给值命名以将多组值映射到密钥中。

JSON

"Mappings" : { "RegionMap" : { "us-east-1" : { "32" : "ami-6411e20d"}, "us-west-1" : { "32" : "ami-c9c7978c"}, "eu-west-1" : { "32" : "ami-37c2f643"}, "ap-southeast-1" : { "32" : "ami-66f28c34"}, "ap-northeast-1" : { "32" : "ami-9c03a89d"} } }

YAML

Mappings: RegionMap: us-east-1: "32": "ami-6411e20d" us-west-1: "32": "ami-c9c7978c" eu-west-1: "32": "ami-37c2f643" ap-southeast-1: "32": "ami-66f28c34" ap-northeast-1: "32": "ami-9c03a89d"

带有多个值的映射

以下示例中显示了映射至两组值(一组名为 32,另一组为 64)的区域密钥。

JSON

"RegionMap" : { "us-east-1" : { "32" : "ami-6411e20d", "64" : "ami-7a11e213" }, "us-west-1" : { "32" : "ami-c9c7978c", "64" : "ami-cfc7978a" }, "eu-west-1" : { "32" : "ami-37c2f643", "64" : "ami-31c2f645" }, "ap-southeast-1" : { "32" : "ami-66f28c34", "64" : "ami-60f28c32" }, "ap-northeast-1" : { "32" : "ami-9c03a89d", "64" : "ami-a003a8a1" } }

YAML

RegionMap: us-east-1: "32": "ami-6411e20d" "64": "ami-7a11e213" us-west-1: "32": "ami-c9c7978c" "64": "ami-cfc7978a" eu-west-1: "32": "ami-37c2f643" "64": "ami-31c2f645" ap-southeast-1: "32": "ami-66f28c34" "64": "ami-60f28c32" ap-northeast-1: "32": "ami-9c03a89d" "64": "ami-a003a8a1"

从映射返回值

您可以使用 Fn::FindInMap 函数根据指定的密钥返回命名的值。以下示例模板包含由 FindInMap 函数分配其 ImageId 属性的 Amazon EC2 资源。FindInMap 函数将密钥指定为创建堆栈所在的区域(使用 AWS::Region 伪参数)并将 32 指定为待映射值的名称。

JSON

{ "AWSTemplateFormatVersion" : "2010-09-09", "Mappings" : { "RegionMap" : { "us-east-1" : { "32" : "ami-6411e20d", "64" : "ami-7a11e213" }, "us-west-1" : { "32" : "ami-c9c7978c", "64" : "ami-cfc7978a" }, "eu-west-1" : { "32" : "ami-37c2f643", "64" : "ami-31c2f645" }, "ap-southeast-1" : { "32" : "ami-66f28c34", "64" : "ami-60f28c32" }, "ap-northeast-1" : { "32" : "ami-9c03a89d", "64" : "ami-a003a8a1" } } }, "Resources" : { "myEC2Instance" : { "Type" : "AWS::EC2::Instance", "Properties" : { "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "32"]}, "InstanceType" : "m1.small" } } } }

YAML

AWSTemplateFormatVersion: "2010-09-09" Mappings: RegionMap: us-east-1: "32": "ami-6411e20d" "64": "ami-7a11e213" us-west-1: "32": "ami-c9c7978c" "64": "ami-cfc7978a" eu-west-1: "32": "ami-37c2f643" "64": "ami-31c2f645" ap-southeast-1: "32": "ami-66f28c34" "64": "ami-60f28c32" ap-northeast-1: "32": "ami-9c03a89d" "64": "ami-a003a8a1" Resources: myEC2Instance: Type: "AWS::EC2::Instance" Properties: ImageId: !FindInMap [RegionMap, !Ref "AWS::Region", 32] InstanceType: m1.small

输入参数和 FindInMap

您可以对 Fn::FindInMap 函数使用输入参数来引用映射中的特定值。例如,假定您有一个映射到特定 AMI ID 的区域和环境类型的列表。您可以使用输入参数 (EnvironmentType) 来选择堆栈使用的 AMI ID。要确定区域,您可以使用 AWS::Region 伪参数,该参数获取创建堆栈的 AWS 区域。

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]

本页内容: