是否有办法在部署完成之前访问已部署资源的自动生成URL?(如db主机、lambda函数URL等)
我可以在部署完成后访问它们,但有时我需要在构建堆栈时访问它们。(例如在其他资源中使用)。
处理这个用例的好解决方案是什么?我正在考虑从CloudFormation模板将它们输出到SSM参数存储中,但我不确定这是否可能。
谢谢您的建议和指导!
发布于 2018-07-02 20:13:20
如果“在其他资源中使用它们”意味着另一个无服务器服务或另一个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
在无服务器框架中,可以使用CloudFormation访问https://serverless.com/framework/docs/providers/aws/guide/variables/#reference-cloudformation-outputs输出值
如果要在同一堆栈中使用自动生成的值,则只需使用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}
发布于 2018-07-03 18:51:08
要添加bwinant的答案,如果要引用位于另一个区域的另一个堆栈中的变量,则${cf:<stack name>.<output name>}
不工作。有一个插件可以实现这一点,叫做serverless-plugin-cloudformation-cross-region-variables。你可以像这样使用它
plugins:
- serverless-plugin-cloudformation-cross-region-variables
custom:
myVariable: ${cfcr:ca-central-1:my-other-stack:MyVariable}
https://stackoverflow.com/questions/51026602
复制