How to run a Node.js Express app in Amazon’s AWS API Gateway

Amazon’s API Gateway lets you run a web application without having to create and scale your own web server cluster. Instead, you define endpoints in the API Gateway that point to an AWS Lambda function.

It’s a great pattern from a cost point of view. API Gateway costs $3.50 per million API calls received, plus the cost of data transfer out. AWS Lambda costs based on the number of requests per month, plus a cost for the duration of execution and the amount of memory consumed. If your app isn’t used very much, you cost is going to be very low. If it does get a lot of usage, you’ll be able to scale up automatically. No installing servers, no maintenance, and linear pricing.

Most Node.js developers use Express, or something that derives from it like Locomotive. Transitioning to the AWS API Gateway might seem daunting, because you have to start over from scratch writing Lambdas. There goes all that lovely Express middle-ware, rendering, etc.

To help with the transition, I’ve created Legerdemain. It lets you run your entire Express app as a Lambda with very minimal work. Legerdemain is brand new and I’m sure there are a lot of kinks to work out, but I’ve been able to run a full Express app on AWS API Gateway with it.

Setting up API Gateway to work is still a pain. You have to create your routes all over again, by hand. You also have to do some work to configure the mappings per method. I hope to release an automated way to do this in the future.

If you encounter any problems with Legerdemain, please open an issue.