3

I am trying to put this scraping code on AWS Lambda, when i try to test the code i get the following error:

"errorMessage": "Unable to import module 'lambda_function': No module named 'lambda_function'",
  "errorType": "Runtime.ImportModuleError"

The code:

from datetime import datetime
from functions import MultiplePageScraper, CleanTable, upload_unique, pages
from sqlalchemy import create_engine
now = datetime.now()
date = now.date()

# Credentials:
host = xxxxx
user = "admin"
password = xxxxx
port = 3306
database = "vehicles"
# Create connection
mydb = create_engine("mysql+pymysql://" + user + ':' + password + '@' + host + ':' + str(port) + '/' + database , echo=False)

# Target Url :
URL = "https://www.usedcars.co.ke/nairobi/cars-for-sale?page="

def lambda_handler(event, context):
    # Retrive the data and clean it
    page_count = pages(URL)
    data = MultiplePageScraper(URL, int(page_count))
    clean_data = CleanTable(data)
    # Check for duplicates scraped Vs Database
    upload_data = upload_unique(clean_data, mydb)
    # Upload data to RDS database
    if upload_data.shape[0] > 1:
        upload_data.to_sql(name='CARS', con=mydb, if_exists = 'append', index=False)
        print(f'Uploaded {upload_data.shape[0]} unique rows to the database!')
    else:
        print('No new cars to upload.')

I have no idea what the error means, any one can help?

2
  • 2
    You need to update the handler in the Runtime settings for the Lambda function. For example: if the name of the python module for the lambda is cars.py, set the handler in Runtime settings to cars.lambda_handler. Commented Mar 21, 2021 at 16:34
  • @OluwafemiSule Thanks for the tip. It worked. Now after packaging up the code with all dependencies and uploading it to lambda i get the following error: "errorMessage": "Unable to import module 'Get_Data': cannot import name 'etree' from 'lxml' (/var/task/lxml/__init__.py)", "errorType": "Runtime.ImportModuleError" Any ideas what might be the problem ? Commented Mar 22, 2021 at 10:02

1 Answer 1

3

By default, a new python lambda function will look for lambda_function.py file and call lambda_handler method:

enter image description here

You can change it using the runtime settings for your function.

Sign up to request clarification or add additional context in comments.

3 Comments

@ Marcin thanks. Now after packaging up the code with all dependencies and uploading it to lambda i get the following error: "errorMessage": "Unable to import module 'Get_Data': cannot import name 'etree' from 'lxml' (/var/task/lxml/__init__.py)", "errorType": "Runtime.ImportModuleError" Any ideas what might be the problem ?
@YussufAbdirizak Is this some custom python module? You can create new question specific to this new issue.
Make sure that you put the library that you import for Python inside the /python folder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.