"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 Antwort
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
EXPERTE
beantwortet vor einem Jahr
  • 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.

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen