Test an AWS lambda locally using Docker container image
Asked Answered
T

2

13

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

Triparted answered 22/6, 2021 at 21:34 Comment(3)
Your issue is probably because you are running on Windows rather than Linux/macOS. Note the difference between what you are curling in your question vs your answer. If you aren't actually consuming the msg value, you can omit it e.g. curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d "{}". ... The key is the double quotes around the {} rather than the single quotes in your question/the AWS docs.Gaiser
Correct! Had the same issue and it works as explained above.Syndetic
thanks @Gaiser !! double quotes work in Windows.Rouvin
T
7

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

Triparted answered 22/6, 2021 at 21:40 Comment(1)
On Windows, you really just need to replace the single quotes around {} with double quotes, so yes this answer works but the msg/hello are not needed.Gaiser
H
3

For anyone using the Postman for invoking the endpoint, you can try the following: enter image description here

Hygrometric answered 25/11, 2021 at 7:51 Comment(1)
Is there an equivalent curl command to what Postman does here? I have been struggling with this for days and it turns out a simple postman get requests works, but nothing else..Overcloud

© 2022 - 2024 — McMap. All rights reserved.