如何在Cloudformation的TemplateBody部分传递变量

huangapple go评论80阅读模式
英文:

How to pass variables in TemplateBody section of Cloudformation

问题

我尝试在TemplateBody部分传递RegionName,但出现错误:“模板格式错误:在模板的资源块中存在未解析的资源依赖项[RegionName]”。

这是我使用的YAML文件:

      TemplateBody: |
        Resources:
          CWEvent:
            Type: AWS::Events::Rule
            Properties:
              State: ENABLED
              Name: Rule
              Targets:
                - Arn: !Sub "arn:aws:events:${RegionName}: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"        

由于限制,我也无法使用S3,因此TemplateURL不适用。

英文:

Im trying to pass RegionName in the TemplateBody section but getting the error as "Template format error: Unresolved resource dependencies [RegionName] in the Resources block of the template".

Here is the yaml file which I'm using:

      TemplateBody: |
        Resources:
          CWEvent:
            Type: AWS::Events::Rule
            Properties:
              State: ENABLED
              Name: Rule
              Targets:
                - Arn: !Sub "arn:aws:events:${RegionName}: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"

Also I cannot use S3 due to restriction so TemplateURL is out of picture.

答案1

得分: 1

如果您使用了!Sub函数,它应该会自动填充所有变量,因此您的TemplateBody行应该如下所示:

TemplateBody: !Sub |

这将在堆栈部署之前填充您的变量,您可能需要删除模板中其他!Sub函数以使其正常工作。

英文:

If you used the !Sub function it should fill in all your variables for you, so your TemplateBody line should look like

  TemplateBody: !Sub |

This would then fill in your variables before the stack is deployed, you might need to remove the other !Sub's you have inside the template for this to work.

huangapple
  • 本文由 发表于 2023年7月10日 15:54:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定