Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do add python libraries to AWS Lambda?

I just made my first function that fetches data from an excel sheet in Google Sheets. I got an error:

"errorMessage": "Unable to import module 'lambda_function': No module named 'googleapiclient'"

so i googled how to upload python modules (https://www.youtube.com/watch?v=HBt8MXHcaPI) and it said to create a virtual env in something like VSCode, pip install the libraries that i'll need, then zip them and add them as a layer to Lambda.

I did that, twice. (It just looked like a whole bunch of libraries were being installed, so i looked up how to remove all of them (pip freeze | xargs pip uninstall -y) and tried again). So here's the starting point and after doing pip install google-api-python-client enter image description here enter image description here enter image description here

I guess i'm a little confused whether i should be zipping up literally all of that, or just the stuff that has google in the name. I tried it both ways and neither seemed to work. I'm still getting that error.

like image 388
Raksha Avatar asked Oct 31 '25 12:10

Raksha


2 Answers

To use any 3rd party library in lambda you can use a lambda layer.

install the dependency using following command

pip3 install <your_package> -t .

zip the package

zip -r your_pkg_layer.zip .

create the layer in aws and upload the zip, after that add the layer to your lambda function

you can follow this blog in medium.

like image 194
Tasnuva Avatar answered Nov 02 '25 02:11

Tasnuva


I recommend that you look at AWS SAM

AWS SAM is an extension of CloudFormation that simplifies the development of serverless applications.

To deploy a AWS Lambda function using AWS Serverless Application Model (SAM), you need to follow these steps:

  • Create a SAM template: This is a YAML file that defines the AWS resources you want to deploy, including the Lambda function and its dependencies.

  • Package the function: Package the function code and any dependencies into a .zip file. (For this you'll need a requirements.txt file with all the dependencies your code needs)

  • Deploy the function: Use the AWS CLI command deploy to deploy the SAM template and function code to AWS. The command will create or update a CloudFormation stack, which creates or updates the specified AWS resources.

Example SAM template:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: main.handler
      Runtime: python3.8
      CodeUri: .
      Description: "This is my SAM function"
      MemorySize: 128
      Timeout: 3

Example AWS CLI command:

sam build --debug --template-file template.yaml
sam package --s3-bucket your_s3_bucket --s3-prefix sam-deployments \
                  --output-template-file packaged-template.yaml
sam deploy -t packaged-template.yaml --stack-name your_stack_name \
          --region your_aws_region --capabilities CAPABILITY_IAM 

A common folder structure for a Lambda project using AWS Serverless Application Model (SAM) would look something like this:

my-lambda-project/
├── main.py # Lambda function code
├── template.yaml # SAM template
├── requirements.txt # Python dependencies
like image 42
rocha-p Avatar answered Nov 02 '25 02:11

rocha-p



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!