amazon-web-servicesaws-lambdaaws-lambda-layersaws-lambda-containers

Test an AWS lambda locally using Docker container image


i have been having a hard time getting this thing to work right i am trying to invoke the Lambda locally which is running on docker container but i have following issue

Docker File

FROM public.ecr.aws/lambda/python:3.8


COPY myfunction.py ./

CMD ["myfunction.lambda_handler"]

Python File myfunction.py

import json
import sys

def lambda_handler(event, context):
    print("Hello AWS!")
    print("event = {}".format(event))
    return {
        'statusCode': 200,
    }

Step 1:

docker build --tag custom .

output

C:\Users\s.shah\IdeaProjects\YoutubeVideos\Learn>docker build --tag custom .
[+] Building 0.5s (7/7) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                                                                                                                                                                                                            0.0s
 => => transferring dockerfile: 31B                                                                                                                                                                                                                                                                                                                             0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                                                                                                                                               0.0s
 => => transferring context: 2B                                                                                                                                                                                                                                                                                                                                 0.0s
 => [internal] load metadata for public.ecr.aws/lambda/python:3.8                                                                                                                                                                                                                                                                                               0.5s
 => [internal] load build context                                                                                                                                                                                                                                                                                                                               0.0s
 => => transferring context: 35B                                                                                                                                                                                                                                                                                                                                0.0s
 => [1/2] FROM public.ecr.aws/lambda/python:3.8@sha256:d5a4b8f3f7394358bfe2cb51677f3d14af59c08adf831332cb4501f56dfd64cc                                                                                                                                                                                                                                         0.0s
 => CACHED [2/2] COPY myfunction.py ./                                                                                                                                                                                                                                                                                                                          0.0s
 => exporting to image                                                                                                                                                                                                                                                                                                                                          0.0s
 => => exporting layers                                                                                                                                                                                                                                                                                                                                         0.0s
 => => writing image sha256:6a339ad8416cd93632ae4418e89409dae2e8a684de7990746b613b223a974899                                                                                                                                                                                                                                                                    0.0s
 => => naming to docker.io/library/custom    

Step 2:

docker run -p 9000:8080 random-letter:latest

output

INFO[0000] exec '/var/runtime/bootstrap' (cwd=/var/task, handler=)

Step 3:

curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'

{"errorMessage": "Unable to unmarshal input: Expecting value: line 1 column 1 (char 0)", "errorType": "Runtime.UnmarshalError", "stackTrace": []}

INFO[0057] extensionsDisabledByLayer(/opt/disable-extensions-jwigqn8j) -> stat /opt/disable-extensions-jwigqn8j: no such file or directory
WARN[0057] Cannot list external agents                   error="open /opt/extensions: no such file or directory"
START RequestId: d49a7179-7ec5-4122-933f-be04abfed953 Version: $LATEST
Traceback (most recent call last):able to unmarshal input: Expecting value: line 1 column 1 (char 0)
END RequestId: d49a7179-7ec5-4122-933f-be04abfed953
REPORT RequestId: d49a7179-7ec5-4122-933f-be04abfed953  Init Duration: 0.23 ms  Duration: 65.39 ms      Billed Duration: 100 ms Memory Size: 3008 MB    Max Memory Used: 3008 MB

Any help would be great


Solution

  • Answer

    https://github.com/lambci/docker-lambda/issues/208 curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d "{"""msg""":"""hello"""}"

    this works