By using AWS re:Post, you agree to the Terms of Use

How to create a lambda layer for a CodeStar project?

0

Hello everyone,

I have created a CodeStar project (Python Webservice) and I am using vscode to edit it.

So far I have customized the CodeStar generated project to:

  • buildspec.yml
  • functions/lambda1
  • functions/lambda2
  • template-configuration.json
  • template.yml

It builds using "sam local build", and when changes are pushed to CodeCommit as well.

However, I did not managed to reuse code between "lambda1" and "lambda2". If I try to add a "Layer" using the "Lambda Dashboard" it rewrites the code I have added using git.

Any suggestions on how to re-use code in a CodeStar project?

Edited by: robsondepaula on Feb 11, 2019 11:32 AM

asked 4 years ago71 views
3 Answers
0

Thank you for your feedback. The reason that the Lambda Layer which you create through the Lambda console is deleted upon deployments is because the Lambda functions are managed by your project's CloudFormation stack. Any changes performed outside of the stack will be overwritten when that stack is deployed again.

If you'd like to create a Lambda Layer within your project, it is best to use the AWS::Lambda::LayerVersion CloudFormation resource to do so https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-layerversion.html. You will also need to update your existing Lambda function resource to reference the new Layer https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html#cfn-lambda-function-layers.

However, Lambda Layers may not work the best for your use-case, as source code changes will not update an existing Lambda Layer, so a new Layer has to be created each time you make changes to your code. Instead, we recommend that your two Lambda functions be located within the same code base and reuse code directly from within the repository. Let me know if you are still confused or if this does not work for you.

answered 4 years ago
0

hello, robsondepaula.

I faced same situation.
I wanted to re-use common codes in lambdas, and manage them codecommit and cloudformation.

I thought it can achieve making lambda layer.
But I gave up because to make lambda layer, I need to upload source code S3.

Now, I usually do following way to achieve that.

your_project---- lambda1 -- lambda1_code.py
                     |
                     |--- lambda2 -- lambda2_code.py
                     |
                     ---- common_modules -- common_module.py

Then, write this in buildspec.yml before do aws cloudformation package(maybe install phase is better?)

 - cp ./common_modules/common_module.py ./lambda1/
 - cp ./common_modules/common_module.py ./lambda2/

In this way, it performs likes lambda layer, and it can be managed by codecommit and cloudformation.

For your information.

answered 4 years ago
0

Thank you!

answered 3 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions