Hi All, I need a feedback. We have many python ser...
# seed
s
Hi All, I need a feedback. We have many python services and needed to share codes between them. Now earlier i used to use gemfury to maintain a separate private python library, and use that via layers in our services, but it makes the development complex and extremely time consuming. As to test the new common code in cloud, i need to publish there, update layer and deploy services. (of course I can do some local invocations, but its exact same as cloud) Now after moving to seed, i am using deployment phases, in stage one, i have created a service called
layers
and inside that have a folder structure like this :
Copy code
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 :
Copy code
../layers/smiles
And it works in seed unittests. For local development, I do the following
Copy code
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 :
Copy code
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 :
Copy code
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.
f
Hey @Sourav Sarkar, the simpliest way would be not to use layer. I’m not super familiar w/ Python, but can you import
smiles
directly in
serviceA
?
Or is that u want to install smile’s
requirements.txt
only once instead of once per service to speed up the build?