"Unable to import module 'lambda_function': No module named 'google'"

0

I am trying to import google modules to my lambda function, but keep getting the error where I am Unable to import module 'lambda_function because there no module named google. I've tried adding a layer with the zipped up file of all of the google library packages that I need into the Lambda function. The function just doesn't detect the library.

This is my lambda_function.py code in Lambda. The import statements are listed above.

import os
import json
import google.auth
import google.oauth2.credentials
from google.oauth2 import service_account
import googleapiclient.discovery
import datetime
import requests


def lambda_handler(event, context):
    name = event['name']
    email = event['email']
    phone = event['phone']
    date = event['date']
    time = event['time']
    permit_type = event['permit-type']
    create_event(name, email, phone, date, time, permit_type)
    return {
        'statusCode': 200,
        'headers': {
            'Content-Type': 'application/json'
        },
        'body': json.dumps({'message': 'Event created successfully!'})
    }

This is the command I used to import the google libraries. pip install google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client

The directory in the lambda function looks like this currently: directory

My hander is lambda_function.lambda_handler

If anyone has some sort of insight or a solution to this, it would honestly be awesome. Thank you in advance!

1 Answer
0

Are you able to create layers this way?
https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html

In your case, I think you need to zip the module and set it to layers with the following command.

pip install google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client -t .
zip -r  ../python .
profile picture
EXPERT
answered a year ago
  • I'll give it a try. Would I need to adjust the command you gave me, or can I just run it as it is?

  • It is recommended to run the command in an empty directory, since the modules are all placed in the directory where the command is executed.

  • I gave it a try and unfortunately it did not work. Im not sure if I did it correctly but after running the commands, I downloaded the zip locally and then created a layer in my function and uploaded the zip to it.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions