skip to Main Content

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.

4

Answers


  1. I am using two ways of installing a Python module.

    1 Use AWS Lambda layers
    enter image description here

    Resources:

    2 Use Docker images

    • Create a Docker image and upload it to Amazon ECR
    • Create your Lambda function using a container (Docker image)

    Resources:

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. 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
    
    Login or Signup to reply.
  4. 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.

    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.

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search