Deploying an Express Application to AWS Lambda, the easy way

yarn add serverless serverless-plugin-write-env-vars --dev
yarn add aws-serverless-express
# .env.ymlcredentials:
accessKeyId: YOUR_AWS_ACCESS_KEY
secretAccessKey: YOUR_AWS_SECRET_KEY
development:
NODE_ENV: development
DATABASE_URL: postgres://todo_user:todo_password@localhost/todo
staging:
NODE_ENV: production
DATABASE_URL: postgres://user:password@myhost.com:5432/dbname?ssl=true
production:
NODE_ENV: production
DATABASE_URL: postgres://user:password@myhost.com:5432/dbname?ssl=true
  • accessKeyId and secretAccessKey: you can follow this instructions if you don’t have AWS credentials yet.
  • DATABASE_URL: you can provision free Postgres databases on Heroku, and you can get the url value from there (just make sure to add ?ssl=true to the end of the url)
# lambda.js'use strict';const awsServerlessExpress = require('aws-serverless-express');
const app = require('./app');
const server = awsServerlessExpress.createServer(app);
exports.handler = (event, context) => awsServerlessExpress.proxy(server, event, context);
# serverless.ymlservice: todoapiprovider:
name: aws
runtime: nodejs4.3
credentials: ${file(./.env.yml):credentials}
custom:
writeEnvVars:
NODE_ENV: ${file(./.env.yml):${opt:stage}.NODE_ENV}
DATABASE_URL: ${file(./.env.yml):${opt:stage}.DATABASE_URL}
plugins:
- serverless-plugin-write-env-vars
functions:
api:
handler: lambda.handler
events:
- http: ANY {proxy+}
  • service: our app name
  • provider:
    - name: serverless only supports aws at the moment
    - runtime: when writing your app, make sure you’re using a node version supported by AWS Lambda (at the time of this writing 4.3 was recommended)
    - credentials: here we’re reading the credentials variable from the .env.yml we wrote earlier. You can read more on how serverless deals with variables here.
  • custom:
    - writeEnvVars: during deployment,serverless-plugin-write-env-vars will grab any value defined inside custom.writeEnvVars and create a .env file for us, which our app can then read.
    We’re grabbing values from our .env.yml file, like we did for credentials. However, notice we’re also using an opt:stage variable, which will be populated from the CLI --stage option when we run the serverless deploy command.
  • plugins: include any serverless plugin here.
  • functions: a regular serverless app would usually have several functions defined. However, we’ll only use a single function that will cede control to our express app.
    - api: I’m calling my function api, but you can use any name you prefer. This name will be used to identify your function when running serverless commands like serverless logs.
    - handler: here we specify we want to execute the handler function we previously wrote on our lambda.js file.
    - events: a lambda function can be triggered from different events (an S3 bucket upload, an SNS topic, HTTP requests, etc). For this example, we’ll only deal with HTTP requests.
    - http: here we’re saying we want to accept any http method to any path, and just hand it over to our express app.
serverless deploy --stage staging
https://v5mmjbkn7e.execute-api.us-east-1.amazonaws.com/staging/
serverless logs --stage staging --function api -t
serverless deploy --stage production

--

--

Full-Stack Web Developer @ X-Team / Digital Nomad. Se habla español.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alexis Hevia

Alexis Hevia

Full-Stack Web Developer @ X-Team / Digital Nomad. Se habla español.