CloudFormation template Mappings syntax - AWS CloudFormation (original) (raw)
The optional Mappings
section helps you create key-value pairs that can be used to specify values based on certain conditions or dependencies.
One common use case for the Mappings
section is to set values based on the AWS Region where the stack is deployed. This can be achieved by using theAWS::Region
pseudo parameter. The AWS::Region
pseudo parameter is a value that CloudFormation resolves to the region where the stack is created. Pseudo parameters are resolved by CloudFormation when you create the stack.
To retrieve values in a map, you can use the Fn::FindInMap
intrinsic function within the Resources
section of your template.
Syntax
The Mappings
section uses the following syntax:
JSON
"Mappings" : {
"MappingLogicalName" : {
"Key1" : {
"Name" : "Value1"
},
"Key2" : {
"Name" : "Value2"
},
"Key3" : {
"Name" : "Value3"
}
}
}
YAML
Mappings:
MappingLogicalName:
Key1:
Name: Value1
Key2:
Name: Value2
Key3:
Name: Value3
MappingLogicalName
is the logical name for the mapping.- Within the mapping, each map is a key followed by another mapping.
- The key must be a map of name-value pairs and unique within the mapping.
- The name-value pair is a label, and the value to map. By naming the values, you can map more than one set of values to a key.
- The keys in mappings must be literal strings.
- The values can be of type
String
orList
.
Note
You can't include parameters, pseudo parameters, or intrinsic functions in theMappings
section.
If the values in a mapping aren't currently used by your stack, you cannot update the mapping alone. You must include changes that add, modify, or delete resources.
Examples
Basic mapping
The following example shows a Mappings
section with a mapRegionMap
, which contains five keys that map to name-value pairs containing single string values. The keys are region names. Each name-value pair is an instance type from the T family that's available in the region represented by the key. The name-value pairs have a name (InstanceType
in the example) and a value.
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
Mapping with multiple values
The following example has region keys that are mapped to two sets of values: one namedMyAMI1
and the other 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
Return a value from a mapping
You can use the Fn::FindInMap
function to return a named value based on a specified key. The following example template contains an Amazon EC2 resource whoseImageId
property is assigned by the FindInMap
function. TheFindInMap
function specifies key as the region where the stack is created (using the AWS::Region
pseudo parameter) and MyAMI1
as the name of the value to map to. For more information about pseudo parameters, see Get AWS values using pseudo parameters.
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
Input parameter andFn::FindInMap
You can use an input parameter with the Fn::FindInMap
function to refer to a specific value in a map. For example, suppose you have a list of regions and environment types that map to a specific security group ID. You can select the security group ID that your stack uses by using an input parameter (EnvironmentType
). To determine the region, use the AWS::Region
pseudo parameter, which gets the AWS Region in which you create the stack.
This example also declares a Systems Manager parameter type that provides a Systems Manager parameter alias (/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2
) as the default value for the ImageId
property of the EC2 instance. This is a value that CloudFormation resolves as the AMI ID value for the latest Amazon Linux 2 AMI in the region where the stack is created.
JSON
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Parameters" : {
"LatestAmiId" : {
"Description" : "The latest Amazon Linux 2 AMI from the Parameter Store",
"Type" : "AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>",
"Default" : "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2"
},
"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" : {
"Ref" : "LatestAmiId"
},
"InstanceType" : "t2.micro",
"SecurityGroupIds" : [{ "Fn::FindInMap" : [ "SecurityGroupMap", { "Ref" : "AWS::Region" }, { "Ref" : "EnvironmentType" } ]}]
}
}
}
}
YAML
AWSTemplateFormatVersion: 2010-09-09
Parameters:
LatestAmiId:
Description: The latest Amazon Linux 2 AMI from the Parameter Store
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
Default: '/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2'
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: !Ref LatestAmiId
InstanceType: t2.micro
SecurityGroupIds:
- !FindInMap [SecurityGroupMap, !Ref "AWS::Region", !Ref EnvironmentType]
These related topics can be helpful as you develop templates that use theFn::FindInMap
function.