访问已部署资源的 AWS 自动生成的 URL



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

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

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

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

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

查看 https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/outputs-section-structure.html 和 https://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",那么在我的无服务器服务中,我可以通过以下方式引用SearchUrl:

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

为了补充 bwinant 的答案,如果您想引用位于另一个区域的另一个堆栈中的变量,${cf:<stack name>.<output name>}不起作用。有一个插件可以实现这一点,称为无服务器插件云形成跨区域变量。你可以这样使用它

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

最新更新