如何为我的 Lambda 函数创建可重复使用的 CloudFormation 模板?
How can I create a re-usable CloudFormation template for my Lambda functions?
我有 9 个 lambda 函数作为单个 CloudFormation 项目的一部分。
我觉得每个 Lambda 函数的 CloudFormation 模板在很多地方开始变得相同并且感觉像是重复的 YAML 代码。
有没有办法创建单个 Lambda 配置并通过参数化输入重用相同的代码?
是的,CloudFormation Nested Stacks 可以帮助您将 共享 Lambda 配置声明为一个组件,然后您可以将其用于多个资源。
您可以创建专用的子“Lambda”模板,而不是为您的 Lambda 函数复制和粘贴相同的配置。
然后,您可以从一个封装的 CloudFormation 模板(通常称为“根堆栈”)多次指向您的 Lambda CloudFormation (CF) 堆栈。
Parameters
, Outputs
& !GetAtt
将是帮助您在子堆栈和根堆栈之间共享数据以及函数名称、内存、运行时等参数化方面的核心工具。
在组织规模上,AWS CloudFormation Modules会更合适,它允许您在更大范围内封装和重用资源配置。
Here's how AWS describes them:
Modules are building blocks that can be reused across multiple CloudFormation templates and is used just like a native CloudFormation resource.
...
This means you can create a module that defines your organization’s standards for a Lambda function and then consume that Lambda module in another module that defines the patterns for your serverless Amazon API Gateway implementation.
我有 9 个 lambda 函数作为单个 CloudFormation 项目的一部分。
我觉得每个 Lambda 函数的 CloudFormation 模板在很多地方开始变得相同并且感觉像是重复的 YAML 代码。
有没有办法创建单个 Lambda 配置并通过参数化输入重用相同的代码?
是的,CloudFormation Nested Stacks 可以帮助您将 共享 Lambda 配置声明为一个组件,然后您可以将其用于多个资源。
您可以创建专用的子“Lambda”模板,而不是为您的 Lambda 函数复制和粘贴相同的配置。
然后,您可以从一个封装的 CloudFormation 模板(通常称为“根堆栈”)多次指向您的 Lambda CloudFormation (CF) 堆栈。
Parameters
, Outputs
& !GetAtt
将是帮助您在子堆栈和根堆栈之间共享数据以及函数名称、内存、运行时等参数化方面的核心工具。
在组织规模上,AWS CloudFormation Modules会更合适,它允许您在更大范围内封装和重用资源配置。
Here's how AWS describes them:
Modules are building blocks that can be reused across multiple CloudFormation templates and is used just like a native CloudFormation resource.
...
This means you can create a module that defines your organization’s standards for a Lambda function and then consume that Lambda module in another module that defines the patterns for your serverless Amazon API Gateway implementation.