跳至內容

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}'
專家

已提問 3 年前檢視次數 1949 次

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
專家

已回答 3 年前

專家

已審閱 2 年前

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

專家

已回答 3 年前

  • Thanks. I did read the examples but those have no Tags or where property value would be anything but simple string.

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。