How to Run and Test AWS Lambda Function Using Docker in Your Laptop
AWS Lambda is a serverless computing service, which allows developers to build and run applications without having to manage servers, provided by Amazon Web Service.
There are several ways to write and test AWS Lambda. We can write our code directly in the AWS Lambda console, or we can write our code in our laptop, build it and then upload the build package to AWS Lambda console and test it there. Both ways require us to have the AWS account and access to AWS Lambda console, which can be very inconvenient and expensive especially for an organization with many engineers. Therefore, using Docker container to run and test code locally could help eliminate the need for accessing to the AWS Lambda console, in return reducing the cost and development time.
Lambda supports multiple programming language, we’ll use Java as an example in this article.
Step 1) Install Maven
Some IDEs usually have Maven as plugin, I am using Visual Studio code(VSC) with Maven plugin. For more info on how to install Maven, you can visit the link provided by the VSC’s team.
Step 2) Create the project folder and Generate the Maven code template
To create the project folder, we run this command in our terminal
mkdir hello-lambda
To generate a Maven project, open the Command Palette
in VSC and search for Create Java Project
command.
Let’s keep everything else default after that.
This is what the project folder should look like after finishing step 2.
Step 3) Add AWS dependencies and packaging plugins
The AWS SDK allows us to write lambda handler code that lambda function can read. Packaging plugins help us to package our code into a build file that can be run in the Docker container.
To add AWS dependencies to our project, we go to the pom.xml
file and add these dependencies under dependencies
tag.
To add plugin packaging, in the pom.xml
file and add these plugins under plugins
tag.
Step 4) Add example Lambda code
Add an Input.java
file in the same directory of the App.java
.
This file serves as an input object to our lambda function. Then, add the below code to the Input.java
.
In the App.java
folder, we can add simple Java code such as
This lambda code receives the Input object and response with Hello <input payload string>
.
Step 5) Build and Package our code
In our terminal, jump into demo
folder and run
mvn compile dependency:copy-dependencies -DincludeScope=runtime
This command generates a target
folder with classes
and dependency
folders inside.
Step 6) Run and Test our code in Docker Container
In the demo
folder, add a Dockerfile
Then copy the content of the following to our Dockerfile.
Basically, what this Dockerfile does is that it is using lambda runtime image from AWS for Java 11. Then it copies the classes
and dependency
that we generated earlier in the target
folder into the lambda working directory of the Docker container. Finally, it specifies where our Java code is for the command handler
to run.
Now we are ready to build and run our code in the Docker container. Run these two commands. (make sure that you installed Docker and docker is running in the background)
docker build -t lambdacode .
docker run -p 9000:8080 lambdacode
Once it is run, our terminal output should look similar
08 Aug 2022 02:54:36,830 [INFO] (rapid) exec '/var/runtime/bootstrap' (cwd=/var/task, handler=)
and we won’t be able to type new command as the Docker container is running our lambda code in the current terminal.
Then, create a second terminal and run the following command
curl -XPOST “http://localhost:9000/2015-03-31/functions/function/invocations" -d ‘{“payload”:”Lambda”}’
This command provides our lambda function with the payload input string Lambda
and we will see the result Hello Lambda
. We can replace "Lambda"
with anything for testing.
We have run our lambda function using Docker and without uploading it to AWS console. Congrats!