pythonaws-lambdaaws-codebuildaws-codestar

How to re-use code in a CodeStar project?


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

So far I have customised the generated project to have two lambdas. The structure is as follows:

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

It works using "sam local start-api" and by testing using the AWS dashboard.

However, I did not manage to reuse code between "lambda1" and "lambda2" other than copy pasting ".py" files on each lambda to be used as modules. Tried editing requirements.txt to point to a git repository with the lib without success.

Another attempt was to try to add a "Layer" using the "Lambda Dashboard" which ended up rewriting the lambda code.

With all that said, any suggestions on how to re-use code in a CodeStar project?


Solution

  • In order to create layers one have to edit the CodeStar project Cloudformation template:

    Other ways to reuse code other than having the same files copy&pasted to each lambda is to postpone that to the build.

    Having a folder structure like:

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

    And modifying the build script:

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