问题描述
我的 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,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 ECSClusterStackNameParameter ] }
解决方法
现在我看到了,我想。在第二次导入时,您需要删除 !Sub
前面的破折号:
- Fn::ImportValue:
!Sub "${ECSClusterStackNameParameter}-ListenerArn"