Reputation: 1906
I'm struggling a little with Fn::Sub
and Fn::FindInMap
in order to create a dynamic value for a resource.
I would like something in the mapping like
Mappings:
Mapping01:
common:
SyncName: "archive-uploader-${AWS::Region}-synchronisation-a2"
Then I'd like to use it something like
Name: !Sub !FindInMap [Mapping01, common, SyncName]
Which I know I can't do because the Sub function cannot take intrinsic functions at the String parameter. But I cannot see a neat way to do this. At the moment I just use Sub and the hardcoded string where I need it.
I'd prefer to have a single place for the string and then use it with Sub where I need it. How can I do that in a CFT?
I don't want to have a large map that just varies the name's region. That's what most of the documentation shows.
Upvotes: 2
Views: 2517
Reputation: 1404
This is fairly straightforward to do. You would need the advanced form of Fn::Sub
First though:
I have assumed this is your use case, in my example below
Parameters:
String1:
Type: String
Default: "archive-uploader-"
String2:
Type: String
Default: "-synchronisation-a2"
Resources:
Name:
Fn::Sub:
- "${str1}${reg}${str2}${finmap}" #these are just placeholder formats
- str1: !Ref String1
reg: !Ref AWS::Region
str2: !Ref String2
finmap: !FindInMap [MapName, MapRootKey, MapValueKey]
!Join
but it may be more convolutedUpvotes: 1
Reputation: 369
I think you could build it up in pieces a little differently and avoid a per-region map. Wouldn't something like this work?
Mappings:
Mapping01:
common:
SyncName: synchronisation-a2
...
Resources:
MyResource:
Type: AWS::whatever
Properties:
Name: !Sub
- "archive-uploader-{region}-{suffix}"
- region: !Ref AWS::Region
suffix: !FindInMap [Mapping01, common, SyncName]
Upvotes: 0
Reputation: 238497
As you pointed out you can't do this in plain CFN. Your only options to work around this is through development of CFN macro or custom resource.
Alternative is simply not to use CFN, there are far superior IaC tools (terraform, AWS CDK), pre-process all templates before applying them, or keep hardcoding these values in your templates.
Upvotes: 1