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

질문됨 5년 전459회 조회
3개 답변
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.

AWS
답변함 5년 전
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.

답변함 5년 전
0

Thank you!

답변함 5년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠