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
전문가
Kallu
질문됨 10달 전1338회 조회
2개 답변
1
수락된 답변

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
전문가
Kallu
답변함 10달 전
profile picture
전문가
검토됨 한 달 전
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
전문가
답변함 10달 전
  • Thanks. I did read the examples but those have no Tags or where property value would be anything but simple string.

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠