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

35 Views Asked by At

I am writing a lambda function to create a google docs.

These are the lambda code I trying to run.

import json
from google.oauth2 import service_account
from googleapiclient.discovery import build

def replace_placeholders(service, document_id, replacements):
    requests = []
    for placeholder, replacement in replacements.items():
        requests.append({
            'replaceAllText': {
                'containsText': {
                    'text': placeholder,
                    'matchCase': True,
                },
                'replaceText': replacement,
            }
        })

    service.documents().batchUpdate(documentId=document_id, body={'requests': requests}).execute()

def lambda_handler(event, context):
    # Assuming event contains 'filename' and 'replacements'
    filename = event['filename']
    replacements = event['replacements']

    credentials = service_account.Credentials.from_service_account_info(SERVICE_ACCOUNT_INFO, scopes=['https://www.googleapis.com/auth/documents', 'https://www.googleapis.com/auth/drive'])
    
    docs_service = build('docs', 'v1', credentials=credentials)
    drive_service = build('drive', 'v3', credentials=credentials)
    
    # Copy the template document
    template_id = ''
    copy_title = filename
    drive_response = drive_service.files().copy(fileId=template_id, body={'name': copy_title}).execute()
    new_document_id = drive_response.get('id')

    # Replace placeholders in the new document
    replace_placeholders(docs_service, new_document_id, replacements)

    return {
        'statusCode': 200,
        'body': json.dumps(f'New document created with ID: {new_document_id}')
    }

and this is the error I get:

{
  "errorMessage": "Unable to import module 'lambda_function': No module named 'google'",
  "errorType": "Runtime.ImportModuleError",
  "requestId": "bd4a1871-8feb-4d2d-b07a-01216ad5514a",
  "stackTrace": []
}

I tried to add dependency using a Lambda Layer but it still can't solve the problem.

0

There are 0 best solutions below