Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test an AWS lambda locally using custom container image

I am trying to test the newly added feature of running / invoking lambda with custom container image, so I am building a very simple image from AWS python:3.8 base image as follows:

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


COPY myfunction.py ./

CMD ["myfunction.py"]

And here is myfunction.py

import json
import sys

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

My question is the following: after my build is done:

docker build --tag custom .

how can I now invoke my lambda, given that I do not expose any web endpoints and assuming I am spinning up my custom container with success, (although the handler= part is a little bit unsettling in terms of whether I have configured the handler appropriately)

▶ docker run -p 9000:8080 -it custom
INFO[0000] exec '/var/runtime/bootstrap' (cwd=/var/task, handler=) 

A simple curl of course fails

▶ curl -XGET http://localhost:9000                                                                                                                                                                       
404 page not found
like image 331
pkaramol Avatar asked Feb 27 '26 10:02

pkaramol


1 Answers

It turns out I have to invoke this extremely non-intuitive url

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

However I am still getting this error

WARN[0149] Cannot list external agents                   error="open /opt/extensions: no such file or directory"
START RequestId: f681b2ca-5e35-499d-a262-dd7bc53912f0 Version: $LATEST
Traceback (most recent call last):andler 'py' missing on module 'myfunction'
END RequestId: f681b2ca-5e35-499d-a262-dd7bc53912f0
REPORT RequestId: f681b2ca-5e35-499d-a262-dd7bc53912f0  Init Duration: 1.08 ms  Duration: 248.05 ms     Billed Duration: 300 ms    Memory Size: 3008 MB    Max Memory Used: 3008 MB

edit: Solved by changing the CMD from

CMD ["myfunction.py"]

to

CMD ["myfunction.lambda_handler"]
like image 81
pkaramol Avatar answered Mar 02 '26 04:03

pkaramol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!