5 minute Endpoints on Lambda

Simon Stern
5 min readOct 13, 2019

--

Developing serverless infrastructure is the way of the future. There are broad implications for the technology such as enhanced scalability, lower cost, and other benefits. There are tons of resources for determining a business case for serverless, so its definitely something worth exploring. For this example, we’re working with a common problem: You have an API endpoint that you would like to test on a remote server, maybe something with SSL, without having to go through all the work of deploying an express stack on a server somewhere. Docker, Digital Ocean AWS and other providers all provide some kind of hosting service for this purpose…but there is still usually a quite a bit of work involved. For quickly testing a single endpoint in a remote environment, they’re far from ideal. If you’re familiar with deploying instances or containers, you know what I mean: Configuration, provisioning, DNS Resolution, the list goes on.

Getting Started

That being said, let’s get started! From your projects directory, create a new node application with the following commands:

Awesome! You’ve now set up a simple express application. Lets open up index.js and write our mini endpoint. For the sake of developing a comprehensive example, we’ll have 2 endpoints, a simple GET request to return a fake user, and a simple POST request which will return whatever data we send to it. Follow along with the code below (And reach out to me if you have any questions!)

Run this server with node index.js. We can test these endpoints by using a tool like Postman to verify that we get a response.

Creating a Serverless-Ready App.js

Excellent! So we have our server, and now we can create a serverless stack with these endpoints. To do this, we will need to add another package. This package is called serverless-http and you can read more about it here. This package allows you to wrap your API for serverless use. No messing around with ports, sockets, or provisioning configuration are necessary! Pretty cool stuff 😎

Go ahead and install the package with npm install serverless-http.

Next, make sure you require the package at the top of your index.js file.

serverless = require(‘serverless-http’);

Then, instead of calling app.listen, you will instead just export the serverless function you just imported, and pass it your express app as an argument.

module.exports.handler = serverless(app);

Don’t worry, the full example is shown below!

If you look at the example above, there are only 2 changes to this file:

  1. We require the the new package, serverless-http
  2. We export app via the serverless function, with module.exports.handler

Time for some Serverless Devops, Y’all

Awesome. Our next step is to connect the serverless framework to AWS. The main serverless compute product from AWS is Lambda. I encourage you to check it out! If you haven’t already done so, create an account on AWS and find your access key and secret via the IAM dashboard on the AWS website. Once you’ve done that, type the following 3 commands into your terminal inside your project.

  1. Install serverless globally with npm or yarn.
  2. With this command, you are connecting serverless to your AWS account for your deployments. Your code will be hosted there when you provision your resources. Don’t forget to replace ACCESS_KEY and SECRET_KEY with the actual access_key and secret from your IAM dashboard.
  3. Create a new YAML file called serverless.yml

You can also create a new AWS user specifically for this role, but if you do make sure your permissions for that user allows you to use Lambda functions and API Gateway. If you get stuck with this, I would recommend starting that user with admin privileges to get things working, and narrowing them down as much as possible from there.

Inside your serverless.yml file, type out the following code.

Hopefully most of that code is self-documenting. The important parts are that we define a provider (AWS), a region (us-east-1), and generic http events. You may also notice the runtime is nodejs10x, which basically will default to whichever version the lambda’s are most up to date with.

Now, we’re all set to deploy this serverless stack to the cloud. We have one last command to run.

If all goes well, you should see your two endpoints in your terminal appear like so:

and Voila! You now have a remote SSL endpoint that you can quickly deploy and test. Crucially, there is very little refactoring that had to be done to change our regular express app into this serverless stack!

In part II of this series, we will investigate a deeper version of this project, which will include understanding the specific resources we just deployed, provisioning a database, requiring an authorization header, and some more fun things you can do with the serverless framework!

Simon Stern is a freelance software developer and code school instructor. If you’re looking to build apps, or interested in consulting services: https://simonstern.ca

--

--

Simon Stern
Simon Stern

Responses (1)