Fn::FindInMap enhancements - AWS CloudFormation (original) (raw)
The AWS::LanguageExtensions
transform enhances the functionality of theFn::FindInMap
intrinsic function in CloudFormation templates.
The Fn::FindInMap
function is used to retrieve a value from a mapping defined in the Mappings
section of a CloudFormation template. However, the standardFn::FindInMap
function has limitations, such as the inability to handle missing mappings or use a Fn::FindInMap
function with some intrinsic functions embedded inside it.
The AWS::LanguageExtensions
transform addresses these limitations by introducing the following enhancements:
- Default value support – You can specify a default value to be returned if a mapping is not found.
- Intrinsic function support – You can also use a wider range of intrinsic functions to define the fields of
Fn::FindInMap
than with the standardFn::FindInMap
function.
Declaration
JSON
{ "Fn::FindInMap" : [
"MapName",
"TopLevelKey",
"SecondLevelKey",
{"DefaultValue": "DefaultValue"}
]
}
YAML
Syntax for the full function name:
Fn::FindInMap:
- MapName
- TopLevelKey
- SecondLevelKey
- DefaultValue: DefaultValue
Syntax for the short form:
!FindInMap
- MapName
- TopLevelKey
- SecondLevelKey
- DefaultValue: DefaultValue
Parameters
DefaultValue
The value that Fn::FindInMap
will resolve to if theTopLevelKey
and/or SecondLevelKey
can not be found from the MapName
map. This field is optional.
All parameters MapName
, TopLevelKey
,SecondLevelKey
, and DefaultValue
can be an intrinsic function as long as it's able to resolve to a valid value during the transform.
Examples
The following examples demonstrate how to define the fields ofFn::FindInMap
when you add the AWS::LanguageExtensions
transform.
Using a default value
The following is an example of using a default value in theFn::FindInMap
function.
JSON
{
//...
"Transform": "AWS::LanguageExtensions",
//...
"Fn::FindInMap": [
"RegionMap",
{ "Ref": "AWS::Region" },
"InstanceType",
{ "DefaultValue": "t3.micro" }
]
//...
}
YAML
Transform: 'AWS::LanguageExtensions'
#...
!FindInMap
- 'RegionMap'
- !Ref 'AWS::Region'
- 'InstanceType'
- DefaultValue: t3.micro
#...
Using intrinsic functions to define the top level key
The following is an example of using a Fn::FindInMap
function with the Fn::Select
and Fn::Split
intrinsic functions embedded inside it to define the top level key.
JSON
{
//...
"Transform": "AWS::LanguageExtensions",
//...
"Fn::FindInMap": [
"MyMap",
{
"Fn::Select": [
0,
{
"Fn::Split": [
"|",
{ "Ref": "InputKeys" }
]
}
]
},
"SecondKey"
]
//...
}
YAML
Transform: 'AWS::LanguageExtensions'
#...
!FindInMap: [MyMap, !Select [0, !Split [|, !Ref InputKeys]], SecondKey]
#...
Supported functions
You can use the following functions in the parameters of Fn::FindInMap:
enhancements:
[Fn::FindInMap](./intrinsic-function-reference-findinmap.html)
[Fn::Join](./intrinsic-function-reference-join.html)
[Fn::Sub](./intrinsic-function-reference-sub.html)
[Fn::If](./intrinsic-function-reference-conditions.html#intrinsic-function-reference-conditions-if)
[Fn::Select](./intrinsic-function-reference-select.html)
[Fn::Length](./intrinsic-function-reference-length.html)
[Fn::ToJsonString](./intrinsic-function-reference-ToJsonString.html)
[Fn::Split](./intrinsic-function-reference-split.html)
- Unless it’s used for the default value,Fn::Split
has to be used in conjunction with intrinsic functions that produce a string, such as[Fn::Join](./intrinsic-function-reference-join.html)
or[Fn::Select](./intrinsic-function-reference-select.html)
.[Ref](./intrinsic-function-reference-ref.html)
Related resources
For more information and examples that show how to use the Fn::FindInMap
intrinsic function, see Fn::FindInMap.
For more information about the AWS::LanguageExtensions
transform, seeAWS::LanguageExtensions transform.