Myrmex Home Reference Source Repository
Manual » Example

Deploying an Express application in Lambda

This tutorial uses express-generator to quickly create a sample application. This sample application is not an API and serves static content but we do not recommend to do that in a real case scenario. You should use Express with API Gateway and Lambda only for APIs.

Starting with an Express application

We will start by creating a basic Express application following the documentation.

# Installation of express-generator
npm install express-generator -g
# Creation of a basic application
express --view=pug express-app
# Installation of dependencies
cd express-app && npm install

We start the application locally to check that everything works correctly.

set DEBUG=express-app:* & npm start

Visit http://127.0.0.1:3000/ to check that the application runs correctly locally.

Adding Myrmex to the project

Be sure that the Myrmex cli is installed.

npm install -g myrmex

Install Myrmex in the Express project

# For the prompt "What is your project name?", leave it empty, so Myrmex will be installed in the same directory that Express
# We will need the plugins @myrmex/iam @myrmex/api-gateway and @myrmex/lambda
myrmex new

Myrmex and its plugins will be installed in the section devDependencies of the file package.json.

Creation of IAM roles

We create two IAM roles: one to allow Lambda to write in cloudwatch and the other to allow API Gateway to invoke Lambda.

myrmex create-role MyExpressAppLambdaExecution --model LambdaBasicExecutionRole
myrmex create-role MyExpressAppLambdaInvocation --model APIGatewayLambdaInvocation

Creation of the Lambda

We create a new Lambda managed by Myrmex.

myrmex create-lambda serverless-express --runtime nodejs6.10 --timeout 30 --memory 256 --role MyExpressAppLambdaExecution

We have to do a small alteration to the package.json of the Express application to tell node that app.js is the file that has to be loaded when require() is used.

// in /package.json
{
  "name": "express-app",
  "version": "0.0.0",
  "main": "app.js",
  ...
}

In the package.json of the Lambda, we define the Express application as a dependency

# in /lambda/lambdas/serverless-express
npm install ../../.. --save

aws-serverless-express is a npm package that wrap an Express application to process requests from API Gateway with Lambda integration. We add it as a dependency of our Lambda.

# in /lambda/lambdas/serverless-express
npm install aws-serverless-express --save

Then we can alter the code of index.js from the Lambda to correctly expose the handler.

// in /lambda/lambdas/serverless-express/index.js
'use strict';
const awsServerlessExpress = require('aws-serverless-express');
const app = require('express-app');
const server = awsServerlessExpress.createServer(app);

exports.handler = (event, context) => awsServerlessExpress.proxy(server, event, context);

Creation of the API

We create an API with two endpoints using ANY method and Lambda proxy integration: one to handle the root url, and another for the rest.

myrmex create-api serverless-express --title "Serverless Express" --desc "An Express application served by API Gateway and Lambda"
myrmex create-endpoint / ANY --apis serverless-express --summary "Proxy to Lambda" --auth none --integration lambda-proxy --lambda serverless-express --role MyExpressAppLambdaInvocation
myrmex create-endpoint /{proxy+} ANY --apis serverless-express --summary "Proxy to Lambda" --auth none --integration lambda-proxy --lambda serverless-express --role MyExpressAppLambdaInvocation

Deployment

myrmex deploy-apis serverless-express --region us-east-1 --environment DEV --stage v0 --deploy-lambdas all --alias ""

The deployment messages will give you the url of the application. It will have the form https://{your-api-id}.execute-api.us-east-1.amazonaws.com/v0. You can test the root url and the url https://{your-api-id}.execute-api.us-east-1.amazonaws.com/v0/users that is also declared in the sample application generated by express-generator. You can also test some bad urls and observe that you receive an error 404.