0

I am getting "Lambda can't find the file lambda_function.py. Make sure that your handler upholds the format: file-name.method." error just above the AWS Lambda function code block. You can see the warning here

But as you can see in this code block, my file name is lambda_function and my function name is lambda_handler.

import boto3
import os
import logging
import uuid
from webdriver_screenshot import WebDriverScreenshot

logger = logging.getLogger()
logger.setLevel(logging.INFO)

s3 = boto3.client('s3')

def lambda_handler(event, context):
    logger.info('## ENVIRONMENT VARIABLES')
    logger.info(os.environ)
 
    screenshot_file = "{}-{}".format(''.join(filter(str.isalpha, os.environ['URL'])), str(uuid.uuid4()))
    driver = WebDriverScreenshot()

    logger.info('Generate fixed height screenshot')
    driver.save_screenshot(os.environ['URL'], '/tmp/{}-fixed.png'.format(screenshot_file), height=1024)

    logger.info('Generate full height screenshot')    
    driver.save_screenshot(os.environ['URL'], '/tmp/{}-full.png'.format(screenshot_file))

    driver.close()

    if all (k in os.environ for k in ('BUCKET','DESTPATH')):
        ## Upload generated screenshot files to S3 bucket.
        s3.upload_file('/tmp/{}-fixed.png'.format(screenshot_file), 
                    os.environ['BUCKET'], 
                    '{}/{}-fixed.png'.format(os.environ['DESTPATH'], screenshot_file))
        s3.upload_file('/tmp/{}-full.png'.format(screenshot_file), 
                    os.environ['BUCKET'], 
                    '{}/{}-full.png'.format(os.environ['DESTPATH'], screenshot_file))

My code folder is like this and the main python code is in src folder. In AWS Lambda, my folder is looking like this. I do not understand what is the problem.

Even worse, when I try to test the code in AWS Lambda environment, this error is showing up. It says:

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

But I have the boto3 module in the layer, it is uploaded from my S3 Bucket. It is succesfully added to the function

The only thing I am suspecting, I may wrote environment variables wrong as shown here: environment variables due to I am a beginner in coding and AWS Lambda, I just copied both the PATH and PYTHONPATH from somewhere.

I don't know what to do next and need some support here.

Thanks.

2 Answers 2

1

I faced the same problem when I tried to use an external module in lambda_function, in my case was OpenCV as lambda layer. For making Opencv to work I have to change environment variables, making the key PYTHONPATH to have the value /opt/ as is describe it in this tutorial. Then I have the same problem you describe Unable to import module 'lambda_function': No module named 'boto3', what is kind of strange as boto3 suppose to be integrate in the path of lambda. In my understanding, is just a conflict with the directories.

The way it solves for me was changing the value of the environment variables from /opt/ to /opt:/var/runtime. Hopefully it helps you.

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

Comments

0

No worries, I can help you with this. For the first Lambda can't find the file lambda_function.py issue, try prepending src/ to the handler value in the function configuration since your Python scripts are not located in the top-level directory. For the second issue related to lambda layers, it's true that you messed up the PYTHONPATH environment variable. If you are using layers, you don't need to overwrite this variable. Just leave it unchanged.

8 Comments

Thanks for fast response. I really appreaciate that. I removed the edited environment variables. Wrote as I found in the first place "PATH: /var/task/bin" and "PYTHONPATH: /var/task/src:/var/task/lib". For the first part, should I write, "src/lambda_function.lambda_handler" in the runtime settings or do I have to add that in the different place? Sorry if I am asking stupid questions, I am very beginner at this. Thanks.
Yep, you should enter src/lambda_function.lambda_handler @bilalsedef
I did as you suggested, now I have another error. "errorMessage": "Unable to import module 'src/lambda_function'" Unable to import module 'src/lambda_function': No module named 'boto3'
Try printing out PYTHONPATH in the code and see if it gets overwritten.
No change, still getting the boto3 error. Even if I comment boto3 lines out.
|

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.