What is FindInMap in AWS CloudFormation?

AWS Cloud Formation Part 4: What is FindInMap, and is it useful?

Tek Loon
AWS in Plain English

--

Photo by Annie Spratt on Unsplash

This story is part of the AWS CloudFormation Series. If you are interested in this topic, here is the list of the stories and their links.

In this story, I will be sharing about FindInMap function in CloudFormation. The story break into 3 parts:

  • What is !FindInMap?
  • When to use !FindInMap & What Problem It is Trying To Solve?
  • Example Use Case with Code

What is !FindInMap?

!FindInMap is used together with Mappings section defined in the CloudFormation stack.

Mappings allow you to define a set of mapping in the CloudFormation template and !FindInMap is a function where you can use to retrieve the value that you defined in the mapping.

Key to have in Mappings for AWS CloudFormation

Something worth mentioning is we must follow the rules provided in the above screenshot when defining the mapping which is we must have MapName, TopLevelKey and SecondLevelKey .

Both correct and wrong example is provided below to have a better understanding.

Correct Example

Mappings:
EC2ImageIdMap: # -> EC2ImageIdMap is MapName
us-east-2: # -> us-east-2 is TopLevel Key
HVM64: "ami-***" # -> HVM64 is SecondLevelKey

Wrong Example

This example is lacking the SecondLevelKey which will give you errors when you use the template for deployment later.

Mappings:
EC2ImageIdMap: # -> EC2ImageIdMap is MapName
us-east-2: "ami-***" # -> TopLevelKey

When to use !FindInMap & What Problem It Can Solve?

The question is Why we need this? To be honest, I actually can’t really think of a scenario where I would use this. However, after visiting the AWS Documentation, I realized the mappings function in CloudFormation has a very practical use-case for EC2 Instance ImageId.

What Problem It is Trying To Solve?

The below CloudFormation template launch an EC2 Instance in the ap-southeast-1 region with the Amazon Linux 2 Image Id.

ImageId is unique and different across the region. The same image such as Amazon Linux 2 differs between regions.

So, the above template could only be used in the Asia Pacific Southeast Singapore region. If you would like to deploy the same stack in US Ohio, you need to change the ImageIdto the one in the US Ohio region.

However, this causes the below disadvantages:

Hard to maintain if your application is deployed to several regions.

It is hard to maintain due to the below reasons:

  • I could have a dedicated copy of the CloudFormation template for each region. However, multiple copies of the CloudFormation template is not really good for maintenance purpose because once we trying to update our template, we have to do it to all of the templates.
  • I could change the EC2 Instance ImageId according to the region when I deploy. However, this is inconvenient as you keep changing the ImageId every time when you deploy to another region

In order to tackle the above disadvantages, we can use:

  • Mappings to define the mapping of ImageId in different regions.
  • Use FindInMap to get the correct ImageId based on the region we’re deploying.

Example Use Case with Code

The below code shows that I define the mappings of EC2 Instance ImageId for us-east-2 and ap-southeast-1 region.

Next, I will use !FindInMap in the ImageId property and we will use !Ref 'AWS::Region' to get the region we’re deploying to.

Conclusion

In this story, I shared about

  • What is !FindInMap and Some Pattern to Follow When Defining the Mapping where we need MapName , TopLevelKey and SecondLevelKey .
  • A good practical use case that resolve the difference of ImageId across regions by using Mappings and !FindInMap .
  • Lastly, I also shared the code sample on Mappings and !FindInMap

Thanks for reading.

More content at plainenglish.io

--

--

Coder and Writer. If you enjoy my stories— support me by https://www.buymeacoffee.com/tekloon so I can keep writing articles for the community.