| « PreviousNext » | |
![]() ![]() ![]() | Did this page help you? Yes | No | Tell us about it... |
You use Parameters, Mappings, and the
Fn::FindInMap function to enable conditional parameter functionality in your
template:
Add one parameter to your Parameters section for every mapping you want to include. The parameter is how you pass in the desired mapping key.
Create the mappings that contain the key options and key values.
Use the Fn::FindInMap function as the value for the resource property or output
you want to assign conditionally.
Note
You can set a default value for both parameters and mappings. You should set a default for at least
one of these. If Fn::FindInMap cannot resolve a key and value at runtime, an error will
prevent the stack from being created.
Consider this example. Suppose you want the cfn-describe-stacks command to print the AMI
name of the AMI you want to run based on a particular region. You could do this with the following:
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "TemplateName - ShortMapExample.template",
"Parameters" : {
"Region" : {
"Default" : "us-east-1",
"Description" : " 'us-east-1' | 'us-west-1' | 'eu-west-1' | 'ap-southeast-1' "
}
},
"Mappings" : {
"RegionMap" : {
"us-east-1" : {
"AMI" : "ami-76f0061f"
},
"us-west-1" : {
"AMI" : "ami-655a0a20"
},
"eu-west-1" : {
"AMI" : "ami-7fd4e10b"
},
"ap-southeast-1" : {
"AMI" : "ami-72621c20"
}
}
},
"Resources" : {
...other resources...
},
"Outputs" : {
"OutVal" : {
"Description" : "Return the name of the AMI matching the RegionMap key",
"Value" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "Region" }, "AMI" ]}
}
}
}The parameter Region accepts a string value, ideally one of the region identifiers in
the template. The Mappings section declares the RegionMap mapping. Each mapping key
assigns a value to the AMI attribute. The Outputs section declares the
OutVal output, which gets its value based on the value returned from
Fn:FindInMap.
The following shows the value assigned to OutVal based on the listed command:
| Command Lines | Value Assigned to OutVal |
|---|---|
cfn-create-stack MyTestStack -f ShortRegionExample.template --parameters "Region=us-west-1" ... cfn-describe-stacks MyTestStack |
ami-655a0a20 |
cfn-create-stack MyTestStack -f ShortRegionExample.template --parameters "Region=eu-west-1" ... cfn-describe-stacks MyTestStack |
ami-7fd4e10b |
cfn-create-stack MyTestStack -f ShortMapExample.template ... cfn-describe-stacks MyTestStack |
ami-76f0061f |
In the first two cases, the value specified as part of the --parameters option
determines the value of OutVal. In the third example, a mapping key is not specified,
so the default region, us-west-2, will be used.