Sourav Sarkar
11/12/2021, 1:08 PMlayers
and inside that have a folder structure like this :
serviceA/
- serverless.yml
- requirements.txt
layers -
- smiles/src/
- smiles/setup.py
- smiles/tests
- serverless.yml
- requierements.txt
Now in the serviceA/requirements.txt
I refer the common code via this :
../layers/smiles
And it works in seed unittests.
For local development, I do the following
cd layers/smiles
pip install -e.[dev]
Now i have seen if i mention smiles
in side layers/requirements.txt
then the serverless-python-requirements
is unable to find the code. So after some research I found they have a vendor feature and I added like this :
custom:
pythonRequirements:
dockerizePip: non-linux
layer: true
name: ${self:provider.stage}-smiles-layer
vendor: ./smiles/src
Now the layer can be created both locally and from seed cicd.
now the final step is to attach the layer to the lambda. serviceA
is deployed in later phase, so I thought i can export the layer arn and import it in serviceA
via cloudformation imports. but it works in first deployment, but it does not work in further deployments due to this : https://gnomezgrave.com/2020/06/04/update-cross-stack-aws-lambda-layers/
So I use the layer like this :
layers:
- !Sub arn:aws:lambda:ap-south-1:${AWS::AccountId}:layer:layers-${self:provider.stage}-python-requirements:latest
This solves my code sharing problems and the rules are easy to follow as per my opinion. Question is can it be simplified further ? or better approaches are available.
Frank
smiles
directly in serviceA
?Frank
requirements.txt
only once instead of once per service to speed up the build?