Sub topic 6
Lookup tables with Mappings and FindInMap
This chapter assumes you can read a CloudFormation YAML template, know what a parameter is, and have used Ref. A mapping is a fixed lookup table written into the template. You use it when a value depends on another value that you choose or that CloudFormation supplies, such as an environment name or a Region.
How a mapping is built
A mapping has three levels: the mapping name, a top-level key, and a second-level key. Each second-level key holds one value. The value is a string or a list of strings. A number is allowed, and CloudFormation treats it as a string.
Mappings:
EnvSettings:
dev:
RetentionDays: 7
prod:
RetentionDays: 90Here EnvSettings is the mapping name, dev and prod are top-level keys, and RetentionDays is a second-level key. Mappings are static. You can't compute a key inside the Mappings section, and you can't reference a parameter there.
Reading a value with Fn::FindInMap
Fn::FindInMap takes three arguments: the mapping name, the top-level key, and the second-level key. The first argument is usually a literal. The other two can be literals, or they can be Ref calls to parameters or pseudo parameters.
ExpirationInDays: !FindInMap [EnvSettings, !Ref EnvName, RetentionDays]If EnvName is prod, CloudFormation resolves this to 90. The lookup happens when the stack is created or updated, before any resource is built.
The full example below uses the mapping to set an S3 lifecycle rule.
AWSTemplateFormatVersion: "2010-09-09"
Description: Bucket whose retention depends on the environment
Parameters:
EnvName:
Type: String
Default: dev
AllowedValues:
- dev
- prod
Mappings:
EnvSettings:
dev:
RetentionDays: 7
prod:
RetentionDays: 90
Resources:
AppBucket:
Type: AWS::S3::Bucket
Properties:
LifecycleConfiguration:
Rules:
- Id: ExpireOldObjects
Status: Enabled
ExpirationInDays: !FindInMap [EnvSettings, !Ref EnvName, RetentionDays]The AllowedValues list on the parameter matches the top-level keys of the mapping. This is deliberate. If a user passed staging, the lookup would fail.
Mappings versus conditions
Use a mapping when many values change together and you can list every case ahead of time. Use a condition when you need a yes or no decision, such as whether to create a resource at all. Mappings can't add or remove resources. They only supply values.
Common errors
- Key not found. If the top-level or second-level key doesn't exist, the stack operation fails with a message that the mapping can't be found for that key. Check spelling and case, because keys are case-sensitive.
- Parameter in a mapping. You can't write
!Refinside theMappingssection. Put theRefin theFindInMapcall instead. - Missing Region. A Region-keyed mapping fails in a Region you didn't list. Add the Region, or restrict where the template is used.
- Wrong argument order. The order is mapping name, top-level key, second-level key. Swapping the last two gives a key-not-found error.
Exercise
A template has Mappings: {Sizes: {dev: {Type: t3.micro}, prod: {Type: m5.large}}} and a parameter EnvName. Write the expression that gives the instance type for the chosen environment, and say what happens if EnvName is test.
Show answer
Use !FindInMap [Sizes, !Ref EnvName, Type]. If EnvName is test, there is no test key in Sizes, so the lookup fails and the stack operation reports an error. Restricting the parameter with AllowedValues: [dev, prod] catches this earlier.