【问题标题】:AWS CloudFormation Fn::ImportValue doesn't like !Join?AWS CloudFormation Fn::ImportValue 不喜欢!加入?
【发布时间】:2021-08-10 21:21:08
【问题描述】:

我的 CloudFormation 模板中有以下资源正在尝试创建侦听器规则。这个想法是,基于传入的 EnvironmentType 和 AWS 区域,我想从导出它的相应 CloudFormation 堆栈中导入侦听器 ARN。

Parameters:
  EnvironmentType:
    Type: String
    Default: "sandbox"
  ECSClusterStackNameParameter:
    Type: String
    Default: "ECS-US-Sandbox"

Mappings:
  production:
    us-east-1:
      stackWithAlbListenerInfo: "ECS-US-Prod"
    eu-north-1:
      stackWithAlbListenerInfo: "ECS-EU-Prod"
  staging:
    us-east-1:
      stackWithAlbListenerInfo: "ECS-US-Staging"
    eu-north-1:
      stackWithAlbListenerInfo: ""
  sandbox:
    us-east-1:
      stackWithAlbListenerInfo: "ECS-US-Sandbox"
    eu-north-1:
      stackWithAlbListenerInfo: ""

Conditions:
  StackExists:
    !Not [ !Equals [ !FindInMap [ !Ref EnvironmentType, !Ref "AWS::Region", stackWithAlbListenerInfo ], ""] ]

Resources:
  AlbListenerRule:
    Type: AWS::ElasticLoadBalancingV2::ListenerRule
    Condition: UseListenerRule
    Properties:
      ListenerArn:
        !If
          - StackExists
          -
            - Fn::ImportValue:
                !Join
                  - "-"
                  - - !FindInMap [ !Ref EnvironmentType, !Ref "AWS::Region", stackWithAlbListenerInfo ]
                    - "ListenerArn"
          - Fn::ImportValue:
              - !Sub "${ECSClusterStackNameParameter}-ListenerArn"

但是,由于此错误,它无法通过验证,并且似乎第一个 Fn::ImportValue: 不喜欢 !Join。但是!Join 返回一个连接的字符串对吗?我错过了什么?

ERROR: Service: marcom-stats-service, cfnUpdate error: com.amazonaws.services.cloudformation.model.AmazonCloudFormationException: Template error: the attribute in Fn::ImportValue must be a string or a function that returns a string (Service: AmazonCloudFormation; Status Code: 400; Error Code: ValidationError; Request ID: 2678552e-cf6c-46e1-b640-a7c07de385c2; Proxy: null)

更新:

虽然 Robert Kossendey 的回答修正了我的错误,但我原来的模板是错误的。这真的是我想做的。我希望它可以帮助某人。

  AlbListenerRule:
    Type: AWS::ElasticLoadBalancingV2::ListenerRule
    Condition: UseListenerRule
    Properties:
      ListenerArn:
        Fn::ImportValue: !Sub
          - ${StackName}-ListenerArn
          - { StackName: !If [ StackExists, !FindInMap [ !Ref EnvironmentType, !Ref "AWS::Region", stackWithAlbListenerInfo ], !Ref ECSClusterStackNameParameter ] }

【问题讨论】:

  • 你能去掉 Fn:ImportValue 前面的破折号吗?我认为 If 函数不希望有一个嵌套列表,所以这也可能导致问题。
  • 谢谢我试过了,得到了同样的错误。

标签: amazon-cloudformation


【解决方案1】:

现在我明白了。在第二次导入时,您需要删除 !Sub 前面的破折号:

- Fn::ImportValue:
  !Sub "${ECSClusterStackNameParameter}-ListenerArn"

【讨论】:

  • 谢谢,就是这样。我看错地方了。
  • 没问题,如果对您有帮助,我们将不胜感激!
猜你喜欢
  • 2018-05-09
  • 1970-01-01
  • 2019-02-13
  • 2020-08-06
  • 1970-01-01
  • 1970-01-01
  • 2012-09-07
  • 2023-03-29
  • 1970-01-01
相关资源
最近更新 更多