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
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.
4
Answers
I am using two ways of installing a Python module.
1 Use AWS Lambda layers
Resources:
2 Use Docker images
Resources:
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 thelayer
to yourlambda
functionyou can follow this blog in medium.
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:
Example AWS CLI command:
A common folder structure for a Lambda project using AWS Serverless Application Model (SAM) would look something like this:
You can include the third-party libraries directly in your Lambda function .zip deployment. There is no need to create a Lambda layer for those. I suggest trying that first, as creating layers can be a bit more complicated. With everything in a single .zip file you can open the zip file and verify the libraries are in the correct location relative to your Python script.
You should be zipping up everything in there. If pip downloaded all those other libraries because the Google library you are using depends on them, then they all need to be part of your Lambda function as well.