【问题标题】:How to get resource out of the CfnStack?如何从 CfnStack 中获取资源?
【发布时间】:2021-08-28 22:19:35
【问题描述】:

我使用 CfnInclude 将 cloudformation 模板导入我的 cdk 代码:

template= _cfn_include.CfnInclude(self,
     'template',
      template_file = "aws-waf-security-automations.yaml")

之后我使用 get_resource() 从这个模板中获取资源之一:

waf = template.get_resource("WebACLStack")

问题在于该资源不是 WafWebACL 类型,而是 CfnStack 类型。在模板中,这个资源是这样描述的:

WebACLStack:
Type: 'AWS::CloudFormation::Stack'
DependsOn: CheckRequirements
Properties:
  TemplateURL: !Sub
    - 'https://${S3Bucket}.s3.amazonaws.com/${KeyPrefix}/aws-waf-security-automations-webacl.template'
    -
      S3Bucket: !FindInMap ["SourceCode", "General", "TemplateBucket"]
      KeyPrefix: !FindInMap ["SourceCode", "General", "KeyPrefix"]
  Parameters:
    ActivateAWSManagedRulesParam: !Ref ActivateAWSManagedRulesParam ...

如何从 CfnStack 中获取 WAFWebACL? 据我了解,WAFWebACL 是 CfnStack 内的嵌套堆栈。

Obs.:我从这个 url 得到这个模板:https://docs.aws.amazon.com/solutions/latest/aws-waf-security-automations/template.html

Obs2:模板输出如下:

 WAFWebACL:
   Description: AWS WAF WebACL
   Value: !GetAtt WebACLStack.Outputs.WAFWebACL

那么我可以从输入中获取 WAFWebACL 资源吗?

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation aws-cdk


    【解决方案1】:

    你需要先加载嵌套堆栈:

    from aws_cdk.aws_wafv2 import CfnWebACL
    
    web_acl_stack = template.load_nested_stack("WebACLStack", template_file="aws-waf-security-automations-webacl.template")
    waf: CfnWebACL = web_acl_stack.get_resource("WAFWebACL")
    

    我认为您还需要将嵌套堆栈的堆栈模板保留在本地,并在加载嵌套堆栈时使用正确的路径,而不仅仅是“aws-waf-security-automations-webacl.template”。

    (我假设您使用的是 Python)

    【讨论】:

    • 是的,我使用的是 Python,但是这个“as CfnWebACL”是用于打字稿的,对吧?它以相同的方式在 python 中转换为一个类?
    • 对不起,我自己弄糊涂了,现在应该是正确的 Python 代码了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    • 2020-05-04
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    • 2018-01-25
    相关资源
    最近更新 更多