- 最新
- 投票最多
- 评论最多
After numerous debugging iterations, I got this isolated down to a case where there is a combination of AWS pseudo parameters and loop identifier inside of same substitution. I could reference to regular template parameter with no problem. It feels like there is some conflict during template transformation when both pseudo parameters and foreach loop are being processed?
Tags:
- Key: Name
Value: !Sub '${AWS::StackName} VPC ${X}'
After some more trial-and-error I found that Join
would allow me to use loop and pseudo variables together. To create Name -tag for my VPC I had to use the old-school concatenation of strings instead of Sub
. Below works but feels like a step backwards compared to using Sub
.
Tags:
- Key: Name
Value: !Join
- ' '
- - !Ref AWS::StackName
- 'VPC'
- !Ref X
Hi,
Please, read this page to have examples re. exact formatting of Fn::ForEach for resource generation: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-foreach-example-resource.html
For example:
AWSTemplateFormatVersion: 2010-09-09
Transform: 'AWS::LanguageExtensions'
Resources:
'Fn::ForEach::Topics':
- TopicName
- - Success
- Failure
- Timeout
- Unknown
- 'SnsTopic${TopicName}':
Type: 'AWS::SNS::Topic'
Properties:
TopicName: !Ref TopicName
FifoTopic: true
to obtain
{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"SnsTopicSuccess": {
"Type": "AWS::SNS::Topic",
"Properties": {
"TopicName": "Success",
"FifoTopic": true
}
},
"SnsTopicFailure": {
"Type": "AWS::SNS::Topic",
"Properties": {
"TopicName": "Failure",
"FifoTopic": true
}
},
"SnsTopicTimeout": {
"Type": "AWS::SNS::Topic",
"Properties": {
"TopicName": "Timeout",
"FifoTopic": true
}
},
"SnsTopicUnknown": {
"Type": "AWS::SNS::Topic",
"Properties": {
"TopicName": "Unknown",
"FifoTopic": true
}
}
}
}
Best, Didier
Thanks. I did read the examples but those have no Tags or where property value would be anything but simple string.
Actually it seems that not just any pseudo variable would trigger this issue, but only AWS::StackName. I could use e.g. AWS::Region without problems. If you are interested bit longer version of the above and other things to consider when using ForEach, I wrote this blog https://carriagereturn.nl/aws/cloudformation/foreach/loop/2023/08/07/taking-cfn-loops-for-spin.html