Sub topic 7
Lookup tables with Mappings and FindInMap
This chapter assumes you can write a template with Parameters and Resources and you know Ref and Fn::Sub. It covers the Mappings section, Fn::FindInMap, and the errors you are most likely to hit.
What a mapping is
A mapping is a static lookup table declared in the template. It has three levels:
- The mapping name.
- A top-level key.
- A second-level key that points to a value.
The value must be a string or a list of strings. Numbers are accepted as scalar values, and CloudFormation treats them as strings when it reads the mapping. Mapping names and keys must be alphanumeric, with the one exception that hyphens are allowed in keys, as in us-east-1.
A mapping cannot contain intrinsic functions. Everything in Mappings is fixed when you write the template. Only the lookup is dynamic.
Mappings:
EnvSettings:
dev:
InstanceType: t3.micro
VolumeGiB: "8"
prod:
InstanceType: m5.large
VolumeGiB: "100"Looking values up with Fn::FindInMap
Fn::FindInMap takes three arguments: the mapping name, the top-level key, and the second-level key. The keys can be literals, or they can come from Ref to a parameter or to a pseudo parameter such as AWS::Region.
AWSTemplateFormatVersion: "2010-09-09"
Description: Launch template sized by environment
Parameters:
EnvName:
Type: String
Default: dev
AllowedValues:
- dev
- prod
Mappings:
EnvSettings:
dev:
InstanceType: t3.micro
VolumeGiB: "8"
prod:
InstanceType: m5.large
VolumeGiB: "100"
Resources:
AppLaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateName: !Sub "${AWS::StackName}-${EnvName}"
LaunchTemplateData:
InstanceType: !FindInMap [EnvSettings, !Ref EnvName, InstanceType]
BlockDeviceMappings:
- DeviceName: /dev/xvda
Ebs:
VolumeSize: !FindInMap [EnvSettings, !Ref EnvName, VolumeGiB]
VolumeType: gp3
Outputs:
TemplateId:
Description: ID of the launch template
Value: !Ref AppLaunchTemplateWhen you deploy with EnvName=prod, CloudFormation resolves both lookups before it creates anything. The launch template gets m5.large and a 100 GiB volume. The AllowedValues list on the parameter keeps the key inside the set that the mapping defines.
Region-keyed mappings
Because AWS::Region is a legal key source, one template can carry per-Region values. A mapping value can also be a list, and you can read an element of it with Fn::Select.
Mappings:
RegionConfig:
us-east-1:
Zones: [us-east-1a, us-east-1b]
eu-west-1:
Zones: [eu-west-1a, eu-west-1b]
Resources:
Queue:
Type: AWS::SQS::Queue
Properties:
Tags:
- Key: PrimaryZone
Value: !Select [0, !FindInMap [RegionConfig, !Ref "AWS::Region", Zones]]Deploy this in a Region that has no entry, such as eu-west-3, and the stack fails.
Common errors
- Missing key. If a lookup has no matching entry, the operation fails with an error that begins
Template error: Unable to get mapping for, followed by the mapping and key names. A create fails at validation time. An update fails and rolls back. A typo in a key, or a Region you did not list, is the usual cause. - Case sensitivity. Keys are case-sensitive, so
Proddoes not matchprod. - Dynamic values. You cannot use
Fn::GetAttor a resourceRefas a key. A lookup key must be known before resources exist, so only parameters, pseudo parameters, and literals work. - Mapping misuse. Do not put secrets in a mapping. The template is readable by anyone who can read the stack.
- Limits. A template can hold up to 200 mappings. Each mapping can have up to 200 top-level keys and 200 attributes per key. Mappings count toward the template size limit.
Mappings versus other options
| Need | Use |
|---|---|
| Fixed values that vary by a small set of keys | Mappings |
| A value the caller supplies | Parameters |
| Turning a whole resource or property on or off | Conditions |
| A value that changes often, such as the latest AMI | SSM parameter types in Parameters |
Mappings are the right fit when the values are known at authoring time and you want them reviewed in version control along with the template. Conditions often sit next to mappings. For example, Fn::FindInMap can return a string that an Fn::Equals condition compares.
Exercise
A template has a mapping named SizeMap with top-level keys small and large, each holding a second-level key Cpu. Write the property value that reads Cpu for the size chosen by a parameter named Size. Then say what happens if the caller passes Size=medium and the parameter has no AllowedValues.
Show answer
Use !FindInMap [SizeMap, !Ref Size, Cpu]. With Size=medium there is no matching top-level key, so the lookup fails with "Template error: Unable to get mapping for SizeMap::medium::Cpu". The stack operation fails, and an update rolls back. Adding AllowedValues: [small, large] to the parameter rejects the bad value earlier, before any resource changes.