Mappings
可选的 Mappings
部分将密钥与对应的一组命名值相匹配。例如,如果您想根据区域设置值,您可以创建将区域名称用作密钥且其中含有您想为每个特定区域指定的值的映射。您使用 Fn::FindInMap
内部函数来检索映射中的值。
不得在 Mappings
部分包含参数、伪参数或内部函数。
语法
Mappings
部分包括键名称 Mappings
。映射中的键必须为文字字符串。值可以是 String
或 List
类型。以下示例显示了 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
示例
基本映射
以下示例显示的是带映射 Mappings
的 RegionMap
部分,该映射包含五个映射到含单字符串值的名称-值对的密钥。密钥为区域名称。每个名称值对都是该键所表示区域内的 T 系列中可用的一种实例类型。
名称值对中有一个名称(示例中的 InstanceType
)和一个值。您可以通过给值命名以将多组值映射到密钥中。
JSON
"Mappings" : { "RegionMap" : { "us-east-1" : {"InstanceType": "t2.micro"}, "us-west-1" : {"InstanceType": "t2.micro"}, "eu-west-1" : {"InstanceType": "t2.micro"}, "eu-north-1" : {"InstanceType": "t3.micro"}, "me-south-1" : {"InstanceType": "t3.micro"} } }
YAML
Mappings: RegionMap: us-east-1: InstanceType: t2.micro us-west-1: InstanceType: t2.micro eu-west-1: InstanceType: t2.micro eu-north-1: InstanceType: t3.micro me-south-1: InstanceType: t3.micro
带有多个值的映射
以下示例演示了映射到两组值的区域键:一个名为 MyAMI1,另一个名为 MyAMI2。
JSON
"AMIIDMap" : { "us-east-1" : {"MyAMI1" : "ami-0ff8a91507f77f867", "MyAMI2" : "ami-0a584ac55a7631c0c"}, "us-west-1" : {"MyAMI1" : "ami-0bdb828fd58c52235", "MyAMI2" : "ami-066ee5fd4a9ef77f1"}, "eu-west-1" : {"MyAMI1" : "ami-047bb4163c506cd98", "MyAMI2" : "ami-0a7c483d527806435"}, "ap-southeast-1" : {"MyAMI1" : "ami-08569b978cc4dfa10", "MyAMI2" : "ami-0be9df32ae9f92309"}, "ap-northeast-1" : {"MyAMI1" : "ami-06cd52961ce9f0d85", "MyAMI2" : "ami-053cdd503598e4a9d"} }
YAML
AMIIDMap: us-east-1: MyAMI1: ami-0ff8a91507f77f867 MyAMI2: ami-0a584ac55a7631c0c us-west-1: MyAMI1: ami-0bdb828fd58c52235 MyAMI2: ami-066ee5fd4a9ef77f1 eu-west-1: MyAMI1: ami-047bb4163c506cd98 MyAMI2: ami-0a7c483d527806435 ap-southeast-1: MyAMI1: ami-08569b978cc4dfa10 MyAMI2: ami-0be9df32ae9f92309 ap-northeast-1: MyAMI1: ami-06cd52961ce9f0d85 MyAMI2: ami-053cdd503598e4a9d
从映射返回值
您可以使用 Fn::FindInMap
函数根据指定的密钥返回命名的值。以下示例模板包含由 FindInMap
函数分配其 ImageId
属性的 Amazon EC2 资源。FindInMap
函数将键指定为创建堆栈的区域(使用 AWS::Region
伪参数),并将 MyAMI1
指定为要映射到的值的名称。有关伪参数的更多信息,请参阅伪参数参考。
JSON
{ "AWSTemplateFormatVersion" : "2010-09-09", "Mappings" : { "AMIIDMap" : { "us-east-1" : {"MyAMI1" : "ami-0ff8a91507f77f867", "MyAMI2" : "ami-0a584ac55a7631c0c"}, "us-west-1" : {"MyAMI1" : "ami-0bdb828fd58c52235", "MyAMI2" : "ami-066ee5fd4a9ef77f1"}, "eu-west-1" : {"MyAMI1" : "ami-047bb4163c506cd98", "MyAMI2" : "ami-0a7c483d527806435"}, "ap-northeast-1" : {"MyAMI1" : "ami-06cd52961ce9f0d85", "MyAMI2" : "ami-053cdd503598e4a9d"}, "ap-southeast-1" : {"MyAMI1" : "ami-08569b978cc4dfa10", "MyAMI2" : "ami-0be9df32ae9f92309"} } }, "Resources" : { "myEC2Instance" : { "Type" : "AWS::EC2::Instance", "Properties" : { "ImageId" : { "Fn::FindInMap" : [ "AMIIDMap", { "Ref" : "AWS::Region" }, "MyAMI1" ]}, "InstanceType" : "t2.micro" } } } }
YAML
AWSTemplateFormatVersion: "2010-09-09" Mappings: AMIIDMap: us-east-1: MyAMI1: ami-0ff8a91507f77f867 MyAMI2: ami-0a584ac55a7631c0c us-west-1: MyAMI1: ami-0bdb828fd58c52235 MyAMI2: ami-066ee5fd4a9ef77f1 eu-west-1: MyAMI1: ami-047bb4163c506cd98 MyAMI2: ami-0a7c483d527806435 ap-northeast-1: MyAMI1: ami-06cd52961ce9f0d85 MyAMI2: ami-053cdd503598e4a9d ap-southeast-1: MyAMI1: ami-08569b978cc4dfa10 MyAMI2: ami-0be9df32ae9f92309 Resources: myEC2Instance: Type: "AWS::EC2::Instance" Properties: ImageId: !FindInMap [AMIIDMap, !Ref "AWS::Region", MyAMI1] InstanceType: t2.micro
输入参数和 Fn::FindInMap
您可以对 Fn::FindInMap
函数使用输入参数来引用映射中的特定值。例如,假定您有一个映射到特定安全组 ID 的区域和环境类型的列表。您可以使用输入参数(EnvironmentType
)来选择堆栈使用的安全组 ID。要确定区域,您可以使用 AWS::Region
伪参数,该参数用于获取创建堆栈时所在的亚马逊云科技区域。
JSON
{ "AWSTemplateFormatVersion" : "2010-09-09", "Parameters":{ "EnvironmentType":{ "Description":"The environment type (Dev or Prod)", "Type":"String", "Default":"Dev", "AllowedValues":[ "Dev", "Prod" ] } }, "Mappings":{ "SecurityGroupMap":{ "us-east-1": { "Dev": "sg-12345678", "Prod": "sg-abcdef01" }, "us-west-2" : { "Dev" : "sg-ghijkl23", "Prod" : "sg-45678abc" } } }, "Resources":{ "Ec2Instance":{ "Type":"AWS::EC2::Instance", "Properties":{ "ImageId": "ami-0a70b9d193ae8a799", "InstanceType": "t2.micro", "SecurityGroupIds": [{ "Fn::FindInMap" : [ "SecurityGroupMap", { "Ref" : "AWS::Region" }, { "Ref" : "EnvironmentType" } ]}] } } } }
YAML
AWSTemplateFormatVersion: "2010-09-09" Parameters: EnvironmentType: Description: The environment type (Dev or Prod) Type: String Default: Dev AllowedValues: - Dev - Prod Mappings: SecurityGroupMap: us-east-1: Dev: "sg-12345678" Prod: "sg-abcdef01" us-west-2: Dev: "sg-ghijkl23" Prod: "sg-45678abc" Resources: Ec2Instance: Type: 'AWS::EC2::Instance' Properties: ImageId: ami-0a70b9d193ae8a799 InstanceType: t2.micro SecurityGroupIds: - !FindInMap [SecurityGroupMap, !Ref "AWS::Region", !Ref EnvironmentType]
相关资源
在开发使用 Fn::FindInMap
函数的模板时,这些相关主题可能对您有所帮助。