A crash course on Serverless with AWS - Running Node.js 11 on Lambda
Tags: node, aws, lambda, serverless
Published: 2018-12-13 11:55:48

---
title: A crash course on Serverless with AWS - Running Node.js 11 on Lambda
published: true
description: With AWS Lambda Layers and custom runtimes you can now build your own custom artifacts, enabling you to share and manage common code between serverless functions. 
tags: node, aws, lambda, serverless
canonical_url: https://statsbot.co/blog/a-crash-course-on-serverless-with-aws-running-node11-on-lambda/
cover_image: https://github.com/adnanrahic/cdn/raw/master/node11-sls-sample/header.jpeg
series: Serverless Crash Courses
---


Quite an exciting title, isn't it? I was hyped when I heard AWS was adding support for [custom runtimes and layers](https://aws.amazon.com/about-aws/whats-new/2018/11/aws-lambda-now-supports-custom-runtimes-and-layers/) for [AWS Lambda](https://aws.amazon.com/lambda/). This means you can now build your own custom artifacts, enabling you to share and manage common code between functions. 

![Mind blown gif](https://github.com/adnanrahic/cdn/raw/master/node11-sls-sample/mind-blown.gif)

I won't say I fainted when I heard the announcement. But, I did. Don't tell anybody. 

## What'll we be doing?

This article will show you how to hook up a custom Node.js 11 runtime to AWS Lambda. We'll create a simple Serverless project with a sample function, and add a layer that will enable us to run the Node.js 11 runtime.

## How it works

To use a [custom runtime](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html), you have to specify that you're providing one when deploying your function. When the function is invoked, AWS Lambda will bootstrap your runtime code and communicate with it over Runtime API to execute the function code.

That's enough about custom runtimes. What are [AWS Lambda Layers](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html)? They're a new type of artifact that can contain arbitrary code and data. It can be referenced by multiple functions at the same time. That's so awesome! Your functions usually share common dependencies like SDKs, prebuilt modules, libraries, and frameworks. Here's the kicker, now you can share runtimes as well!

By using AWS Lambda Layers, you can manage components that are used across multiple functions. Allowing better code reuse and more DRY code.

Using them is simple, you put the common code in a zip and upload it to AWS Lambda as a layer. You can also upload it as a CloudFormation template, then configure your functions to use it. The layer content will be available to your function code. But that's a topic for another tutorial. 

Let's jump into using a custom Node.js v11 runtime!

## Configuring the project 

I’ll assume you already have a basic understanding of [the Serverless Framework](https://serverless.com/framework/). I would also hope you have an AWS account set up. If you don’t, [please check this out](https://hackernoon.com/a-crash-course-on-serverless-with-node-js-632b37d58b44).

*__Note__: Update the Serverless Framework to v1.34.0 or greater for layers support*

### 1. Creating a service

As always we need a fresh service to hold all our code.

```
$ sls create -t aws-nodejs -p node11 && cd node11
```

After running this command you'll find yourself in the `node11` directory alongside a nice boilerplate to start building your functions. Next step is to open up the `serverless.yml` and add our layer.

### 2. Adding the Node11 layer to `serverless.yml`

There are a lot of prebuilt layers to choose from. Luckily, the serverless community is awesome! We'll go ahead and grab the [custom Node.js runtimes](https://github.com/lambci/node-custom-lambda). 

![version arns](https://github.com/adnanrahic/cdn/raw/master/node11-sls-sample/version-arns.png)

You can pick either one, but I'll go with `v11`. Open up the `serverless.yml` now, delete all the contents and paste this in.

```yaml
service: node11

provider:
  name: aws
  runtime: provided # set to provided

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: /
          method: get
    layers: # add layer
      - arn:aws:lambda:us-east-1:553035198032:layer:nodejs11:3
```

It's enough to add the layer ARN and the function will pick up the runtime. Don't forget to add the `runtime: provided` field as well. 

### 3. Adding code to `handler.js`

Moving on from here you'll feel right at home. You can finally write bleeding edge Node.js code on AWS Lambda. We've been waiting for this for a long time.

Open up the `handler.js` and paste in the snippet below.

```javascript
exports.hello = async (event, context) => {
  console.log(`Hi from Node.js ${process.version} on Lambda!`)
  return {
    statusCode: 200,
    body: JSON.stringify({ message: `Hi from Node.js ${process.version} on Lambda!` })
  }
}
```

Pretty simple snippet of code, but it proves a point. Making sure we're running `Node.js v11.4.0`.

## Deploying the project

The Serverless framework makes deployments quick and painless. All you need to do is to run one command.

```
$ sls deploy
```

It'll create a CloudFormation template, provision resources and deploy the code. All in one command. 

![sls deploy](https://github.com/adnanrahic/cdn/raw/master/node11-sls-sample/sls-deploy.png)

The deployment went well. Hit the URL with a curl to make sure it works.
```bash
$ curl https://<id>.execute-api.us-east-1.amazonaws.com/dev/
```
You should see `{"message":"Hi from Node.js v11.4.0 on Lambda!"}` get echoed back. It works great!


## Wrapping up

With the latest improvements to AWS Lambda, new supported languages, new runtimes, and layers, it's becoming so much more than just a supporting service to the _main_ VM and container services. Serverless architecture is becoming a force to be reckoned with. I can't wait to see where it will take us from here!

[Here's the repo](https://github.com/adnanrahic/sls-node11) if you got stuck following the tutorial, give it a star if you want more people to see it on GitHub. If you want to read some of my previous serverless musings head over to [my profile](https://dev.to/adnanrahic) or [join my serverless newsletter!](https://upscri.be/b6f3d5/)

If you need a [serverless analytics framework](https://statsbot.co/cubejs/), check out [Cube.js](https://statsbot.co/sign-up?cubejs=true). It's [open source and on GitHub](https://github.com/statsbotco/cubejs-client). Or, if you want to read more about serverless architectures, feel free to read more serverless related articles on the [Statsbot blog](https://statsbot.co/blog/).

- [Building a Serverless Stripe Analytics Dashboard](https://statsbot.co/blog/building-serverless-stripe-analytics-dashboard/)
- [A crash course on serverless-side rendering with React.js, Next.js and AWS Lambda](https://statsbot.co/blog/a-crash-course-on-serverless-side-rendering-with-reactjs-nextjs-and-aws-lambda/)
- [A crash course on Serverless with AWS - Building APIs with Lambda and Aurora Serverless](https://statsbot.co/blog/a-crash-course-on-serverless-with-aws---building-apis-with-lambda-and-aurora-serverless/)

*Hope you guys and girls enjoyed reading it as much as I enjoyed writing it. If you liked it, don't hesitate to share. Don't forget to give the Statsbot blog some love.*

***

<a href="https://bit.ly/adnan-zeet" target="_blank"><img src="https://dev-to-uploads.s3.amazonaws.com/i/kz3c913l4uxb7j6cpca2.png" alt="This months sponsor is Zeet." style="height: auto !important;width: auto !important;"></a>

*Disclaimer: <a href="https://bit.ly/adnan-zeet" target="_blank">Zeet</a> is sponsoring this blogpost for the next month. I tried it out the other day. It's like serverless but for running entire back ends. You can host and scale apps automagically. Pretty neat.*

***