【问题标题】:Access AWS auto-generated URL for deployed resources访问 AWS 为已部署资源自动生成的 URL
【发布时间】:2018-07-04 10:51:08
【问题描述】:

有没有办法在部署完成之前访问自动生成的已部署资源的 URL? (如 db 主机、lambda 函数 URL 等)

部署完成后我可以访问它们,但有时我需要在构建堆栈时访问它们。 (例如,在其他资源中使用它们)。

什么是处理这个用例的好解决方案?我正在考虑将它们从 CloudFormation 模板输出到 SSM 参数存储中,但我不确定这是否可能。

感谢您的任何建议或指导!

【问题讨论】:

    标签: amazon-web-services serverless-framework serverless


    【解决方案1】:

    如果“在其他资源中使用它们”是指另一个无服务器服务或另一个 CloudFormation 堆栈,则使用 CloudFormation 输出导出您感兴趣的值。然后使用 CloudFormation ImportValue 函数在另一个堆栈中引用该值。

    https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/outputs-section-structure.htmlhttps://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-importvalue.html

    在无服务器框架中,您可以使用 https://serverless.com/framework/docs/providers/aws/guide/variables/#reference-cloudformation-outputs 访问 CloudFormation 输出值

    如果您想在同一个堆栈中使用自动生成的值,那么只需使用 CloudFormation GetAtt 函数。见https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-getatt.html

    例如,我有一个 CloudFormation 堆栈,它输出 ElasticSearch 集群的 URL。

    Resources:
        Search:
            Type: AWS::Elasticsearch::Domain
            Properties: <redacted>
    
    Outputs:
        SearchUrl:
            Value: !GetAtt Search.DomainEndpoint
        Export:
            Name: myapp:search-url
    

    假设 CloudFormation 堆栈名称是“mystack”,那么在我的 Serverless 服务中,我可以通过以下方式引用 SearchUrl:

    custom:
        searchUrl: ${cf:mystack.SearchUrl}     
    

    【讨论】:

      【解决方案2】:

      要添加到 bwinant 的答案,如果您想引用位于另一个区域的另一个堆栈中的变量,${cf:&lt;stack name&gt;.&lt;output name&gt;} 不起作用。有一个插件可以实现这一点,名为serverless-plugin-cloudformation-cross-region-variables。你可以这样使用它

      plugins:
        - serverless-plugin-cloudformation-cross-region-variables
      
      custom:
        myVariable: ${cfcr:ca-central-1:my-other-stack:MyVariable}
      

      【讨论】:

        猜你喜欢
        • 2021-05-13
        • 2021-01-14
        • 1970-01-01
        • 2020-01-04
        • 2021-04-02
        • 1970-01-01
        • 2016-03-07
        • 2018-08-30
        • 2018-02-06
        相关资源
        最近更新 更多