Getting Started with AWS Lambda (Node JS)

Tom Bray
4 min readJan 10, 2016

In this article I’ll walk you through creating a Lambda the easy way, using the AWS Console. At the end I provide links to gulp projects that will help you manage Lambdas for real-world development. In a subsequent article I’ll walk you through how I deploy my Lambda microservices.

For a brief overview of AWS Lambda and how it fits into the serverless microservices picture, checkout https://medium.com/@tombray/from-monolith-to-microservices-part-1-aws-lambda-and-api-gateway-8ce5cf3f0d99#.yv0eswnis

Create a Lambda

Assuming you already have an AWS account, creating a Lambda is quite simple. Just login to the console and navigate to Services > Lambda. If you’ve never created a Lambda before, you should see something like this:

Click the ‘Get Started Now’ button

Click on the blue ‘Get Started Now’ button and you should arrive at the ‘Select blueprint’ screen:

From here you can select a starter template that closely approximates your needs. Type ‘hello’ into the filter input and click on the ‘hello-world’ blueprint for Node JS:

Next, set the name of the function to ‘hello-world’ and replace the inline code with the following:

exports.handler = function(event, context) {
context.succeed(“Hello, World!”);
};

You screen should look like this:

Below the code editor, click into the Role input and choose ‘Basic execution role’:

The following page will appear in a new tab:

The default settings are fine for now, just click ‘Allow’. The tab will close and you should be back to the ‘Configure function’ screen shown earlier:

Just click the ‘Next’ button and on the next page click ‘Create Function’. You should see something like this:

Click the ‘Test’ button and you’ll be presented with the following modal, which you can ignore for this example by clicking ‘Save and test’. The modal will disappear and you may not notice that some interesting stuff appeared below the inline code editor:

In the gray box you can see that your function successfully returned ‘Hello, World!’.

Now, add a console.log to the code like so:

exports.handler = function(event, context) {
console.log(‘Hello, Cloudwatch!’);
context.succeed(“Hello, World!”);
};

Click the ‘Save and test’ button again and scroll down and check out the ‘Log output’ section to see the the console.log appeared there:

You view all of your logs for this lambda by clicking on the ‘logs’ link in the ‘Execution result’ section:

That will take you to CloudWatch and you’ll see something like this:

Click the top link in the ‘Log Streams’ column and you’ll see something like this:

Next Steps

Editing code inline is great for getting your feet wet with Lambda but you’ll quickly run into limitations. For real-world projects you’ll need an automated way for zipping up your lambda with the node modules you require and pushing them to AWS. I use gulp , similar to this: https://github.com/ThoughtWorksStudios/node-aws-lambda. My version allows you to manage multiple lambdas from the same repo: https://github.com/tombray/aws-lambda-gulp-boilerplate. Both the ThoughtWorks version and my version were inspired by this article: https://medium.com/@AdamRNeary/a-gulp-workflow-for-amazon-lambda-61c2afd723b6.

Have fun playing with Lambda and please don’t hesitate to get in touch if you have questions!

Follow me on twitter @tombray

--

--