I am getting this error while uploading code on lambda using docker image. I don't know what is wrong everything seems fine.
My file structure is:
-DockerFile
-myfunction.py
-requirements.txt
myfunction.py file:
try:
import json
import sys
import requests
print("All imports ok ...")
except Exception as e:
print("Error Imports : {} ".format(e))
def lambda_handler(event, context):
print("Hello!")
print("event = {}".format(event))
return {
'statusCode': 200,
}
DockerFile:
FROM public.ecr.aws/lambda/python:3.8
COPY requirements.txt ./
RUN pip3 install -r requirements.txt
COPY myfunction.py ./
CMD ["myfunction.lambda_handler"]
Requirements.txt:
requests==2.25.1
For someone that have the same problem.
FROM public.ecr.aws/lambda/python:3.8
# app.py is the file where is your lambda function
COPY app.py ${LAMBDA_TASK_ROOT}
# Install the function's dependencies using file requirements.txt
# from your project folder.
# To configure the libraries you can check this link:
# https://note.nkmk.me/en/python-pip-install-requirements/
COPY requirements.txt .
RUN pip3 install -r requirements.txt --target "${LAMBDA_TASK_ROOT}"
# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
# app => filename
# handler => the function name where the program starts
CMD [ "app.handler" ]
import json
import time
import os
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def handler(event, context):
token = event["token"]
# HERE WHEREVER YOU WANT THIS IS JUST AN EXAMPLE
print("Use token: ",token)
print("Lambda function ARN:", context.invoked_function_arn)
print("CloudWatch log stream name:", context.log_stream_name)
print("CloudWatch log group name:", context.log_group_name)
print("Lambda Request ID:", context.aws_request_id)
print("Lambda function memory limits in MB:", context.memory_limit_in_mb)
# We have added a 1 second delay so you can see the time remaining in get_remaining_time_in_millis.
time.sleep(1)
print("Lambda time remaining in MS:", context.get_remaining_time_in_millis())
json_region = None
try:
json_region = os.environ['AWS_REGION']
except KeyError:
json_region = "LOCAL ENVIRONMENT"
else:
json_region = "REGION NOT FOUND"
logger.info('Use a the logger')
return {
'statusCode': 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps({
"Region ": json_region
})
}
Build
docker build -t yourcontainername.
Run docker run -p 9000:8080 yourcontainername
Test with CLI
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
Note: If you are using windows use -d "{"""token""":"""mytoken"""}"
If everything goes fine the response is something like:
{"statusCode": 200, "headers": {"Content-Type": "application/json"}, "body": "{\"Region \": \"LOCAL ENVIRONMENT\"}"}
In your case the problem is your Dockerfile
Should be like:
FROM public.ecr.aws/lambda/python:3.8
COPY myfunction.py ${LAMBDA_TASK_ROOT}
COPY requirements.txt .
RUN pip3 install -r requirements.txt --target "${LAMBDA_TASK_ROOT}"
CMD ["myfunction.lambda_handler"]
According with the docs
Install any dependencies under the ${LAMBDA_TASK_ROOT} directory alongside the function handler to ensure that the Lambda runtime can locate them when the function is invoked.
https://docs.aws.amazon.com/lambda/latest/dg/python-image.html#python-image-clients
https://docs.aws.amazon.com/lambda/latest/dg/images-create.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.