How to use aws-lambda-python docker image with example in VSCode for local development

Docker image: https://hub.docker.com/r/amazon/aws-lambda-python

This docker image lacks working example on how to use.

If you just want to test your lambda with this image locally, please read on.

Let's assume the following:

  • Your script is called `my_python.py`
  • The function in this file that you want to execute is called `lambda_handler`
Here is the docker command (I'm using windows):

docker run --rm \
-p 9000:8080 \
--mount type=bind,src=/c/projects/my_lovely_proj,target=/var/task \
amazon/aws-lambda-python \
my_python.lambda_handler

Brief explanation on above:
  • --rm : Remove the container after terminating the script (i.e. Ctrl + C in command line).  So you don't need to run `docker stop <container_id>` and `docker rm <container_id>`
  • -p 9000:8080: map the port of local machine 9000 to container port 8080.  This is super important and will be explained later.
  • --mount: Mount the dir to /var/task in container.  All lambda script will be executed in this /var/task dir
  • amazon/aws-lambda-python: the docker image name
  • my_python.lambda_handler: It is very confusing at first glance.  The format of this string is actually: <script_file_name>.<function_name_to_be_executed>.  By default your lambda function is called `lambda_handler`.  Which means if your function name is called `my_func`, this string will be `my_python.my_func`

Then you would see something like this:

23 Jan 2024 03:28:43,852 [INFO] (rapid) exec '/var/runtime/bootstrap' (cwd=/var/task, handler=)

And nothing return...

You don't know if this is working, you have no idea what is executed, and just simply nothing.


This docker image is actually provide a server-like instance, which means, you need to "call" its API in order to trigger the execution.

In other words, this is not "run a script in a mapped container and finish when done", but "run a server that host your script and you need to call it to execute it".

So what is the API?  The answer is in the above article.

Since I am using Windows + PowerShell in VSCode, here is how to call an API (similar to curl) in a separated shell (PowerShell or terminal):
  • Windows
    • Invoke-WebRequest -Uri "http://localhost:9000/2015-03-31/functions/function/invocations" -Method Post -Body '{}' -ContentType "application/json"
  • Linux
    • curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
As you can see, in order to execute your lambda script in the container, you need to "call" it as a REST API (In AWS terms, it is called "Invoke").  So if you see the word "invoke"/"invocation" or similar, it simply means "calling lambda API".

Then you would see result:

Now you know that your script is triggered/invoke/called successfully.

Then start your development now.

Hope it helps someone.

Comments

Popular posts from this blog

TCPDF How to show/display Chinese Character?

How to fix fancy box/Easy Fancybox scroll not work in mobile

Wordpress Load balancing: 2 web servers 1 MySQL without any Cloud services