Photo by Christopher Robin Ebbinghaus on Unsplash

Developing on AWS Lambda (Part 1): NodeJS and the AWS SDK

George Mao
Feb 2 · 3 min read

Developers who are new to Lambda, NodeJS, and the AWS SDK tend to run into issues with the async nature of NodeJS. This post will provide a quick intro to async development on Lambda.

The first thing to remember is that all calls using the AWS SDK for NodeJS are asynchronous. This means requests are placed on the call stack and when the call is completed the response will either invoke a callback (that you provide) or you must handle a promise.

Lets take a look at a few examples. Open the official AWS API documentation for the Node SDK and pick any service you are interested in. Lets try S3 or DynamoDB. Open the S3 or DynamoDB docs and scroll to the ‘Method Summary’ section. Notice every single Method has a parent class of ‘AWS.Request’. Click the ‘Request’ link to see how this class is defined and you’ll see the first statement:

All requests made through the SDK are asynchronous and use a callback interface

Now lets say you want to call one of the operations such as ‘listBuckets’. The definition says:

listBuckets(params = {}, callback)

The first parameter is input you provide to the listBuckets call. This will include details that pertain to the operation being invoked. The second parameter is a function that the Node runtime will invoke when the listBuckets operation completes. You will have access to the results of the API call in the ‘data’ field.

Callback example

listBuckets({}, function(err, data){
//This is the function that is invoked when listBuckets completes

if(err)
console.log(err)
else{
// Do something with the list of buckets returned
console.log(data)
}
});

This approach uses callbacks — when the listBucket operation completes, the anonymous function is invoked and passes the result into the ‘data’ parameter. As you can see, it’s pretty verbose. I recommend using the promise approach. Go back to the ‘AWS.Request’ documentation and under the ‘Sending a Request’ section, there is a method called ‘promise()’.

Promise example

You can ask any AWS SDK call to return you a promise. A promise is simply an object that holds the results of the async API call until it completes, or in Node terminology “resolves”. Completion can be success or failure which is called “resolve” or “reject”. Instead of passing our call the anonymous function, we will simply ask for the promise:

let s3 = AWS.S3()let buckets = s3.listBuckets().promise()

Now, we simply have to tell our code what to do when when the promise resolves. This is accomplished with the .then() syntax:

let s3 = AWS.S3()let buckets = s3.listBuckets().promise()
buckets.then(results => {
// do something with the results
console.log(results)
})

You can also use the async/await syntax. This tells the Node engine to block and wait for the results of the promise to complete.

let s3 = AWS.S3()let buckets = await s3.listBuckets().promise()
console.log(buckets)

Note how the use of Promises makes your code simpler and easier to read. You can use this approach with nearly any of the AWS NodeJS SDK operations that inherit from the AWS.Request class.

Thanks for reading and in the next article we’ll go deeper into some challenges async Lambda’s introduce.

George Mao

Written by

AWS Serverless Specialist. I’ll post short, high value tips & tricks for all things Serverless. Ping me if you want to talk about anything :)

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade