How can I create a re-usable CloudFormation template for my Lambda functions?

Question:

I have 9 lambda functions as part of a single CloudFormation project.

I feel like the CloudFormation template for each Lambda function is starting to become the same in many places & feels like it’s repetitive YAML code.

Is there a way to create a single Lambda configuration & reuse the same code by parameterising input?

Asked By: ramsunka

||

Answers:

Yes, CloudFormation Nested Stacks can help you declare shared Lambda configuration as a component which you can then use for multiple resources.

Instead of copying & pasting the same configurations for your Lambda functions, you can create a dedicated child "Lambda" template.

Then, you can point to your Lambda CloudFormation (CF) stack multiple times from one encapsulating CloudFormation template, typically called your "root stack".

Parameters, Outputs & !GetAtt will be the core tools to help you share data between the child & root stack, as well as parameterising aspects like function name, memory, runtime etc.


On an organisational scale, AWS CloudFormation Modules will be more suitable, which allow you to encapsulate and re-use resource configurations on a larger scale.

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.

Answered By: Ermiya Eskandary

Ermiya Eskandary, can you share re-usable cloudformation template for lambdadunction??

Answered By: Dara kiran