12

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

Soumil Nitin Shah
  • 634
  • 2
  • 7
  • 18
  • 4
    Your issue is probably because you are running on Windows rather than Linux/macOS. Note the difference between what you are `curl`ing 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. – jspinella Sep 01 '21 at 16:54
  • Correct! Had the same issue and it works as explained above. – André Castro Sep 13 '21 at 17:13
  • thanks @jspinella !! double quotes work in Windows. – Yogesh Jan 30 '22 at 21:12

2 Answers2

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

Soumil Nitin Shah
  • 634
  • 2
  • 7
  • 18
  • 2
    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. – jspinella Sep 01 '21 at 16:55
2

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

Osama Bin Saleem
  • 779
  • 1
  • 12
  • 24
  • 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.. – norman123123 Aug 10 '23 at 16:13