Sitemap
Tripop Torcheep

Tripop Torcheep

Hi, I am a Software Engineer who love in design thinking and problem solving.

Write AWS Lambda with Javascript

3 min readMar 11, 2023

This is tutorial for beginner that work with AWS Lambda by writing code with Javascript language.

If you need to know some detial of AWS Lambda, please checkout to this article before we start.

Prerequisite

  • Node
  • Docker
  • Vscode
  • Postman

Project Structure

clone repository: https://github.com/mrtripop/lambda-function-demo.git

node_modules
Dockerfile
handler.js
package-lock.json
package.json
test.js

Setup Project

  1. Create sample directory
mkdir lambda-function-demo && cd lambda-function-demo

2. Initial package.json in order to install any dependency. If you want to use any service of AWS, install aws-sdk dependency and implement it in handler function.

npm init -y && npm install aws-sdk

3. Create function handler file and poaste this code to handler.js file

touch handler.js
exports.handler = async function (event) {
return event.something;
};

Now you will have these file in directory, these three file is already to handle request. Next step is about is testing without

node_modules
handler.js
package-lock.json
package.json

4. Create file for testing handler and paste this code to test.js file

touch test.js
const lambda = require('./handler');

const simpleEvent = {
something: 'hello world',
};

const result = lambda.handler(simpleEvent);
console.log('result : ', result);

5. Testing using node

node test.js

Result will look like this

result :  Promise { 'hello world' }

This is simple application that you can handle any request that come to Lambda. Next step we will use docker to simulation when we deploy on AWS Lambda function.

  1. Create docker file
touch Dockerfile

2. Pasts this code in docker file. For any base image version, please checkout official document

FROM public.ecr.aws/lambda/nodejs:18
# Copy function code
COPY package.json ${LAMBDA_TASK_ROOT}/package.json
COPY handler.js ${LAMBDA_TASK_ROOT}/handler.js
WORKDIR ${LAMBDA_TASK_ROOT}
RUN npm install
# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "handler.handler" ]

3. Build docker image

docker build -t lambda-demo-test .

4. Run docker container with lambda-demo-testimage

docker run --rm -p 9000:8080 --name lambda lambda-demo-test

After run container, you will see log message like this

Press enter or click to view image in full size

This mean handler function can run without any issue. Then, make request to our handler function. You can use curl or Postman to make request with POST method.

Get Tripop Torcheep’s stories in your inbox

Join Medium for free to get updates from this writer.

Request body

{
"something": "Hello, something"
}

Make request using curl

curl -X POST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{ "something" : "Hello, something" }'

response will return like this

"Hello, something"%

Make request using Postman , you can import this json to your Postman

{
"info": {
"_postman_id": "220d7a65-2e31-464c-bd0d-06e2df055595",
"name": "aws-lambda-function",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "21144856"
},
"item": [
{
"name": "lambda-demo-test",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"something\": \"Hello, something\"\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:9000/2015-03-31/functions/function/invocations",
"protocol": "http",
"host": [
"localhost"
],
"port": "9000",
"path": [
"2015-03-31",
"functions",
"function",
"invocations"
]
}
},
"response": []
}
]
}

After import this json, you will see aws-lambda-function collection in your workspace. In this collection will have request name lambda-demo-test . you can make request.

Press enter or click to view image in full size

This is result from our handler function that run on docker container. You can push this docker image to AWS ECR and pull it to AWS Lambda Function for test you handler function on cloud.

In the end of this tutorial, I just show how to implement and setting up AWS Lambda project on local machine instead of implement on Lambda Console. You can read more detail in official document.

Hope you like, thanks!

--

--

Tripop Torcheep
Tripop Torcheep

Written by Tripop Torcheep

Hi, I am a Software Engineer who love in design thinking and problem solving.