Fn::FindInMap Function
What you'll learn: How to retrieve specific values from your CloudFormation mappings based on keys, enabling environment-aware and region-specific resource configurations.
What Is Fn::FindInMap?
Fn::FindInMap is a CloudFormation intrinsic function that looks up values inside the Mappings section of your template. Think of it like using a two-level dictionary: you provide a map name, a top-level key, and a second-level key, and CloudFormation returns the corresponding value.
This is the function that makes mappings useful—without it, your mappings would just sit there doing nothing.
How It Works
The function takes three arguments:
- MapName – which mapping to search
- TopLevelKey – the first key (often a region or environment)
- SecondLevelKey – the nested key (the specific value you want)
Syntax (YAML):
!FindInMap [MapName, TopLevelKey, SecondLevelKey]
Example:
Mappings:
RegionMap:
us-east-1:
AMI: ami-0abcdef1234567890
eu-west-1:
AMI: ami-0fedcba0987654321
Resources:
MyInstance:
Type: AWS::EC2::Instance
Properties:
ImageId: !FindInMap [RegionMap, !Ref "AWS::Region", AMI]
Here, FindInMap retrieves the correct AMI for whatever region the stack is deployed in—automatically adapting your template to different environments.
Real-World Use Case
You maintain one template but need different instance types per environment: t2.micro for dev, m5.large for production. Store these in a mapping, then use FindInMap with an environment parameter to select the right size.
Key Takeaway: Fn::FindInMap retrieves values from mappings using a map name and two keys, enabling dynamic, region- or environment-specific configurations without hardcoding values.