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
demandé il y a 10 mois1338 vues
2 réponses
1
Réponse acceptée

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
répondu il y a 10 mois
profile picture
EXPERT
vérifié il y a un mois
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
répondu il y a 10 mois
  • Thanks. I did read the examples but those have no Tags or where property value would be anything but simple string.

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions