7

I am writing AWS Lambda functions for my android app backend. I have multiple Lambda functions in python on AWS which requires the same libraries. For example, I need to access the database so I use pymysql library in all my lambda functions. But I am not sure whether I am doing it right.

Do I have to include these libraries in every function package that I deploy or is there a better way by which I can reference the libraries I have used in the previous function?

I am following Tutorial: Accessing Amazon RDS in an Amazon VPC. I have 2 functions. I am uploading each function separately with its dependencies in a zip. Zip contains the code and libraries. Libraries takes most of the space making zip size big. Now the second function also requires the same libraries so again making a zip with same libraries feels wrong.

Also some links to where this is mentioned in docs is helpful. I did not find it anywhere in documentation.

4
  • 2
    You have to include the libraries in each Lambda function you deploy. There is no mechanism for sharing them between functions. Commented Jul 22, 2017 at 21:00
  • @MarkB Wont that simply make my functions bulky? My library takes around 5mb and i have around 20 functions using the same library Commented Jul 23, 2017 at 8:26
  • Or is there any way by which i can club multiple functions together in a zip and upload them Commented Jul 23, 2017 at 8:41
  • It doesn't "simply make your functions bulky". It also makes them independent. Each function has to run in a container completely separated from the other functions, so each function has to include all dependencies. Commented Jul 23, 2017 at 14:05

2 Answers 2

4

You can share your code using AWS Lambda Layers. For example define them using AWS::Lambda::LayerVersion or AWS::Serverless::LayerVersion. You can then reference to them in your Python Lambda functions. Here using AWS SAM :

  MyLambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: function_code/
      Handler: app.lambda_handler
      Runtime: python3.6
      Layers:
        - !Ref MySharedLayer
  MySharedLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
      LayerName: SharedLayerName
      Description: Some shared code
      ContentUri: layer_code/
      CompatibleRuntimes:
        - python3.6
      RetentionPolicy: Retain

Each Lambda function will have the shared code available in /opt. It can be then used in the functions.

Sign up to request clarification or add additional context in comments.

Comments

0

Now that the Lambda Layers are released you can easily share libraries and code between your Lambda Functions.

You can create a zip file for the Layer pretty much the same way as you can do so for a Function.
To share pymysql package you will need to create a Lambda Layer on base of the following function:

pymysql-bundle.zip/
  python/lib/python3.7/site-packages/pymysql

Then from your Lambda Function's code you can reference it like this:

from pymysql import ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.