ForEach loop variable and AWS::StackName are not working within the same Sub -function

0

UPDATE 17/12/23; This issue was fixed around mid-December'23. See https://github.com/aws-cloudformation/cfn-language-discussion/issues/151

I wanted to take new Fn::ForEach loop for a spin (pun intented) but got stuck with YAML formating issue, I think. Below is a snippet from a Cloudformation template demostrating the issue. If I deploy this, it will fail with error Template format error: Unresolved resource dependencies [X] in the Resources block of the template. Cause for the error is reference to ${X} in Value: !Sub '${AWS::StackName} VPC ${X}'. Removing it will fix the error but there must be a way to format YAML to make Fn::ForEach work with Tags too?

Resources:
  'Fn::ForEach::Network':
  - X
  - [ A, B ]
  - VPC${X}:
      Type: AWS::EC2::VPC
      Properties:
        CidrBlock: 10.0.0.0/16
        Tags:
          - Key: Name
            Value: !Sub '${AWS::StackName} VPC ${X}'
profile picture
EXPERT
Kallu
asked 9 months ago1283 views
2 Answers
0
Accepted Answer

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
profile picture
EXPERT
Kallu
answered 9 months ago
0

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

profile pictureAWS
EXPERT
answered 9 months ago
  • Thanks. I did read the examples but those have no Tags or where property value would be anything but simple string.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions