184

I am creating a AWS Lambda python deployment package. I am using one external dependency requests. I installed the external dependency using the AWS documentation. Below is my Python code.

import requests

print('Loading function')

s3 = boto3.client('s3')


def lambda_handler(event, context):
    #print("Received event: " + json.dumps(event, indent=2))

    # Get the object from the event and show its content type
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']).decode('utf8')
    try:
        response = s3.get_object(Bucket=bucket, Key=key)
        s3.download_file(bucket,key, '/tmp/data.txt')
        lines = [line.rstrip('\n') for line in open('/tmp/data.txt')]
        for line in lines:
            col=line.split(',')
            print(col[5],col[6])
        print("CONTENT TYPE: " + response['ContentType'])
        return response['ContentType']
    except Exception as e:
        print(e)
        print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
        raise e

Created the Zip the content of the project-dir directory and uploaded to the lambda(Zip the directory content, not the directory). When I am execute the function I am getting the below mentioned error.

START RequestId: 9e64e2c7-d0c3-11e5-b34e-75c7fb49d058 Version: $LATEST
**Unable to import module 'lambda_function': No module named lambda_function**

END RequestId: 9e64e2c7-d0c3-11e5-b34e-75c7fb49d058
REPORT RequestId: 9e64e2c7-d0c3-11e5-b34e-75c7fb49d058  Duration: 19.63 ms  Billed Duration: 100 ms     Memory Size: 128 MB Max Memory Used: 9 MB
9
  • Is it your full code? By the error it seems somewhere something would want to import lambda_function which is not found. Perhaps you want from future import lambda_function? Or just pip install lambda_function on cmd line. Commented Feb 11, 2016 at 13:45
  • @Berci Am running this python codein AWS platform . I cannot use pip. anywhere in my code am using lambda_function. IF i copy paste the same code in AWS console it will work Commented Feb 11, 2016 at 13:49
  • See the last comment on this thread — maybe applies to you? Commented Feb 11, 2016 at 13:50
  • 5
    My guess is that "handler" option in your function is incorrect. Check that your filename called "lambda_function.py" and handler method is "lambda_handler" Commented Feb 11, 2016 at 15:19
  • 6
    I spent hours, finally came to know that you have to zip the content of your directory ( including lambda_function.py ) and not the directory. Commented Sep 4, 2016 at 2:57

32 Answers 32

1
2
0

One thing that took me some research to realize was that I had python3.10 locally and thus in the folder structure, and lambda only accepts up to python3.9. In my case, I could just change the name of the folder from python3.10 to python 3.9 (python/lib/python3.9/site-packages), but you should just make sure you're using a version of python that is allowed by lambda.

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

Comments

0

simple soluton

  1. create a zip of content for example I have a lambda function named as

    testfunction then the .zip will be zip -r testfunction.zip lambda_function.py other_conent.py

  2. deploy the changes after every change

Comments

1
2

Your Answer

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