Being a developer is amazing. Writing code, solving problems and thinking of ingenious solutions for complicated algorithms is what we live for. But, the grass is not always so green on this side of the fence. Sooner or later you need to get your hands dirty and deploy the app you worked so hard on. Deployments are not always easy. To be blunt, they can be incredibly hard and time-consuming. That’s what we’ll solve in this tutorial.

TL;DR

You can follow along from the beginning or jump to the section which interests you most, and severely hurt my feelings. 😅

Goals

Today you’ll learn how to deploy a Node.js application to AWS Lambda with the help of the Serverless Framework.

The walkthrough will also cover a real-life scenario of deploying a production version of your application, with environment variablesproper monitoring, and, of course, easy debugging. Oh, how I love seeing nice stack traces!

Prerequisites

This tutorial will need you to have a few simple things set up already. Don’t worry, nothing special, just the basics, and I’ll link it all below for you to see, and make sure you have, before moving on.

  • AWS account - AWS has 12 months of free tiers, and plenty of lifetime free tiers, so don’t worry about breaking the bank.
  • Node.js and npm installed - Here’s a quick reference for making sure you have them installed.
  • Serverless Framework installed - Check this out for a nice reference, and follow along steps 1-3.
  • Dashbird account - This will give us the required overview to see what’s happening to our app.

Ready? Let’s go!

What’s AWS Lambda?

AWS Lambda is a pay-as-you-go serverless compute service. Meaning what now? Well, you just deploy your source code to AWS and they handle the rest. Amazing! No need to fiddle with servers, ssh connections, Linux or Vim. But, want to know what’s even better? It scales automatically and has absolutely no downtime. I’ll let that sink in…

The technical definition for AWS Lambda would be a Function as a Service. You deploy some code, it gets invoked, processes some input, and returns a value. Simple!

Hold up a sec. We need to mention a crucial fact. All lambda functions are stateless, meaning they cannot store persistent data. And, if I just said Function as a Service, how do we deploy a whole Node.js application to AWS Lambda?

But, how does it work?

When you think about it, it’s not that complicated. A single lambda function is essentially just a tiny Node.js runtime environment. You can run whatever you want in there. That’s what we’ll do, package up a Node.js app and send it off to AWS Lambda. Sneaky. 😉

What will we deploy?

To make this example as simple as possible, the code we’ll be deploying is just 7 lines long. However, it could be as large as any Node.js application you have in production, and it would all work like a charm. Intriguing…

How do we deploy it?

In comes the Serverless Framework like a horse riding a white knight! Hold up, I may have that backward. 🤔

Anyhow, this awesome framework enables us to both develop our apps locally, just like we’re used to, but also deploy it with one simple command. Hmm… tell me more.

Let’s get coding!

First thing’s first. Open up your terminal, we need to install some packages. Yay, installing stuff, love it! 😫

#serverless #aws #node.js

How to deploy a Node.js application to AWS Lambda using Serverless
1.25 GEEK