You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 22:52:38