Build an Infinitely Scalable API with Lambda + API Gateway

For our new Intel product, we had the following requirements when looking to build a new API:

  • Something that could easily scale
  • It needed to have authentication
  • We need to track usage (how many calls / month a user makes)
  • It would be nice if it was quick to build / setup

Trying out Lambda / API Gateway

I had played around a bit with Lambda and API Gateway while working at Intuit as the concept is pretty cool. Basically you can set up an API via API Gateway and have a Lambda function handle the request. Then you pay for each request that's made with the costs being next to nothing (the first million requests to Lambda each month are free).

This means you don't have to worry about managing a server infrastructure (load balancing, auto scaling, etc.) or paying for idle servers.

Although this sounds awesome, I had my reservations as the technology was so new, and my team initially had a number of problems during dev including:

  • Setting up a build / deploy process
  • Versioning the API (dev, staging, and prod)
  • Testing it both locally and when deployed

Plus there wasn't a lot of documentation available at the time, which made it difficult to figure out best practices and get around the bugs we ran into.

Header picture

Enter Serverless

So when I heard about this new project called Serverless that was supposed to help out on managing a Lambda / API Gateway project, I was curious to try it out.

And it is awesome...

Serverless is an CLI application that helps you easily structure, version, and deploy your projects. I literally had a functioning API that would infinitely scale in less than five minutes.

Here's how it works:

Setup and create a function

First install serverless globally
npm install serverless -g

Next, you'll need to configure it work with AWS as Serverless requires admin privileges to do its thing. This is probably the most challenging part if you're not familar with AWS.

After you configure AWS, create an AWS Lamdba function in Node.js
serverless create --template aws-nodejs

Deploy it to your AWS account
serverless deploy

It should take a minute to deploy, then afterwards you can test it by running:
serverless invoke --function hello (hello is the default name of the function)

If it's working correctly it should log:

    "statusCode": 200,
    "body": "{\"message\":\"Go Serverless v1.0! Your function executed successfully!\",\"input\":{}}"

Create an event

Serverless is all based on events. Basically everything which can trigger a function is an event.

So events could be HTTP requests, events fired from a cloud storage (like a S3 bucket), scheduled events, etc.

We want an API, so we need to create a HTTP event.

If you look in the directory where you created your function you'll see a serverless.yml file. This is the configuration file to manage all the details for your project. You should see something like:

    handler: handler.hello


Basically we have a function named hello that maps to a JavaScript function in the handler.js file also called hello.

module.exports.hello = (event, context, callback) => {  
  const response = {
    statusCode: 200,
    body: JSON.stringify({
      message: 'Go Serverless v1.0! Your function executed successfully!',
      input: event,

  callback(null, response);


So to create an API endpoint that can trigger this hello function, let's setup a simple HTTP GET event. Your serverless.yml should now look like this:

    handler: handler.hello
      - http: GET handler/hello


Now let's deploy our new endpoint
serverless deploy

When it is done deploying and it was successful, you should see the new endpoint in the output. It should look something like this:

  GET -

Trying it out

Now if you copy and paste that into your browser, if it's working correctly you should see something like:

Serverless browser test

How cool is that? You now have an infinite scalable API returning JSON.

Next steps

Although this is really basic, this provides the foundation to quickly build and deploy an API.

We can now add logic to our Lambda function such as calling a database, sending an email, or in the case of our Intel product, analyze the IP address from the incoming request.

Here's a bunch of other features we can add as well:

API Gateway has come a long way in the past year and with projects like Serverless, it's easier than ever to begin using. We're now considering migrating all of our APIs to it.

Michael Frye

Read more posts by this author.

Subscribe to The Blog

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!