如何在CloudFormation的TemplateBody中传递变量?遇RegionName依赖错误
解决CloudFormation TemplateBody中RegionName变量未解析的错误
错误原因
你在TemplateBody内嵌的模板中使用了RegionName变量,但这个变量既不是模板定义的参数,也不是CloudFormation的内置伪参数,导致CloudFormation无法识别它,抛出"Unresolved resource dependencies [RegionName]"错误。
解决方法
直接使用CloudFormation内置的伪参数AWS::Region替代自定义的RegionName,该伪参数无需额外定义,可直接在模板中引用,会自动获取当前部署的AWS区域。
修改后的完整TemplateBody内容如下:
TemplateBody: | Resources: CWEvent: Type: AWS::Events::Rule Properties: State: ENABLED Name: Rule Targets: - Arn: !Sub "arn:aws:events:${AWS::Region}:1234:event-bus/My-Bus" Id: 'Event' RoleArn: !Join - "" - - "arn:aws:iam::" - !Ref "AWS::AccountId" - ":role/" - "Role" EventPattern: source: - "aws.ec2" detail-type: - "AWS API Call via CloudTrail" detail: eventSource: - "ec2.amazonaws.com" eventName: - "RestoreSnapshotFromRecycleBin"
额外说明
如果你的场景需要指定特定区域而非使用当前部署区域,可以在TemplateBody的内嵌模板中先定义Parameters块声明RegionName参数,然后在父模板中通过Parameters传递该参数值,但使用内置伪参数AWS::Region是最简洁的方式,尤其适用于跟随部署区域的场景。
内容的提问来源于stack exchange,提问作者SUBHAS PATIL
相关产品推荐
相关产品推荐

