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 http://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html. 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
Kindly help me to debug the error.
Error was due to file name of the lambda function. While creating the lambda function it will ask for Lambda function handler. You have to name it as your Python_File_Name.Method_Name. In this scenario I named it as lambda.lambda_handler (lambda.py is the file name).
Please find below the snapshot.
If you are uploading a zip file. Make sure that you are zipping the contents of the directory and not the directory itself.
Another source of this problem is the permissions on the file that is zipped. It MUST be at least world-wide readable. (min chmod 444
)
I ran the following on the python file before zipping it and it worked fine.
chmod u=rwx,go=r
I found Nithin's answer very helpful. Here is a specific walk-through:
Look-up these values:
- The name of the lambda_handler function in your python script. The
name used in the AWS examples is "lambda_handler" looking
like "def lambda_handler(event, context)". In this case, the value is
"lambda_handler"
- In the Lambda dashboard, find the name of the Handler in the "Handler" text-box in the "Configuration" section in the lambda dashboard for
the function (shown in Nithin's screenshot). My default name was
"lambda_function.lambda_handler".
- The name of your python script. Let's say it's "cool.py"
With these values, you would need to rename the handler (shown in the screenshot) to "cool.lambda_handler". This is one way to get rid of the "Unable to import module 'lambda_function'" errorMessage. If you were to rename the handler in your python script to "sup" then you'd need to rename the handler in the lambda dashboard to "cool.sup"
There are just so many gotchas when creating deployment packages for AWS Lambda (for Python). I have spent hours and hours on debugging sessions until I found a formula that rarely fails.
I have created a script that automates the entire process and therefore makes it less error prone. I have also wrote tutorial that explains how everything works. You may want to check it out:
Hassle-Free Python Lambda Deployment [Tutorial + Script]
I found this hard way after trying all of the solutions above. If you're using sub-directories in the zip file, ensure you include the __init__.py
file in each of the sub-directories and that worked for me.
I had the error too.
Turn out that my zip file include the code parent folder. When I unzip
and inspect the zip file, the lambda_function
file is under parent folder ./lambda
.
Use the zip
command, fix the error:
zip -r ../lambda.zip ./*
No need to do that mess.
use python-lambda
https://github.com/nficano/python-lambda
with single command pylambda deploy
it will automatically deploy your function
I ran into the same issue, this was an exercise as part of a tutorial on lynda.com if I'm not wrong.
The mistake I made was not selecting the runtime as Python 3.6 which is an option in the lamda function console.
You need to zip all the requirements, use this script
#!/usr/bin/env bash
rm package.zip
mkdir package
pip install -r requirements.txt --target package
cat $1 > package/lambda_function.py
cd package
zip -r9 "../package.zip" .
cd ..
rm -rf package
use with:
package.sh <python_file>
@nithin,
AWS released layers
concept inside Lambda
functions. You can create your layer and there you can upload as much as libraries and then you can connect the layer with the lambda functions.
For more details: https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html