Serverless Framework: Deploy an HTTP endpoint using NodeJS, Lambda on AWS
Goals
Today you are going to learn how to create an AWS Lambda HTTP endpoint implemented in NodeJS with Express using the Serverless framework
What’s the Serverless Framework?
“ Serverless is your toolkit for deploying and operating serverless architectures. Focus on your application, not your infrastructure. — Serverless.com ”
Main benefits of the Serverless Framework:
- Using the Serverless Framework you can define your entire Serverless application defining a simple yam configuration file
- The Serverless Framework is open source and provides agnostic
- The Serverless Framework supports multiple languages as NodeJS, Python, Java, Ruby, Go and more
Prerequisites
This tutorial will cover some of the steps needed in order to create and deploy a serverless application into AWS. However, before starting, you need to:
- Have an AWS account: AWS gives you 12 months free tier that you can use in order to make your tests!
- Install NodeJS and Yarn
Serverless Configuration
Just before starting we need to do some few boring steps to install and configure Serverless
Install Serverless
Open your console and type (put sudo before if you are using Linux)
$ npm install -g serverless
Configure AWS IAM keys
$ serverless config credentials --provider aws --key xxxxxxxxxxxxxx --secret xxxxxxxxxxxxxx
Let’s start!
We are finally ready to get started! We will commence creating a single HTTP endpoint that will be exposed through the AWS API Gateway
Create a boilerplate
Serverless can prepare for us a simple project once defined the technology stack (NodeJS) and the Cloud Provider (AWS) we want to use. So open a console and type
$ serverless create --template aws-nodejs --path serverless-aws-nodejs
As you can see, Serverless created a new folder with inside a basic NodeJS service. The principal files are:
- serverless.yml: it’s the main config file of Serverless
- handlers.js: it does contain the NodeJS application. We can rename it in app.js
Next step is to install the required dependencies. Enter in the created folder and type
$ npm init -y && npm install
Deploy!
We are finally ready to deploy! Just type on the console:
$ serverless deploy
After a few seconds you will have the following output
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
.....
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (387 B)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
...............
Serverless: Stack update finished...
Service Information
service: serverless-aws-nodejs
stage: dev
region: us-east-1
stack: serverless-aws-nodejs
api keys:
None
endpoints:
None
functions:
hello: serverless-aws-nodejs-dev-hello
layers:
None
We have deployed our first service on AWS in less than 5 minutes! Although it looks cool, the service we deployed doesn’t expose any endpoints so it can’t be used (Doh!) Let’s change it a bit.
Expose an HTTP endpoint
In order to expose an HTTP endpoint, we need to configure the AWS API Gateway inside the serverless.yml and handles it with Express.
Let’s clean up the serverless.yml file and inside we paste this content
service: serverless-aws-nodejsprovider:
name: aws #Define the cloud provider to use
runtime: nodejs8.10
stage: dev
region: eu-central-1functions:
app:
handler: app.server
events: #Define the events that trigger the lambda
- http: #Define an HTTP endpoint that respond to the / route
path: /
method: ANY #Define the HTTP methods to be used (GET/POST)
cors: true
- http: #Define an HTTP endpoin that respond to any routes
path: /{proxy+}
method: ANY
cors: true
Clean the app.js and paste inside this content
// app.js
const express = require('express')
const app = express()
const sls = require('serverless-http')//Handle the GET endpoint on the root route /
app.get('/', async (req, res, next) => {
res.status(200).send('Hello Serverless!')
})module.exports.server = sls(app)
Install the dependencies
npm install express serverless-http
Let’s deploy it again!
After few minutes you will have a similar output
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (283 B)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
.......................................
Serverless: Stack update finished...
Service Information
service: serverless-aws-nodejs
stage: dev
region: eu-central-1
stack: serverless-aws-nodejs-dev
api keys:
None
endpoints:
ANY - https://xxxxxxxxxx.execute-api.eu-central-1.amazonaws.com/dev/
ANY - https://xxxxxxxxxx.execute-api.eu-central-1.amazonaws.com/dev/{proxy+}
functions:
app: serverless-aws-nodejs-dev-app
layers:
None
As you can notice from the output, this time it did expose two different endpoints. Let’s test the first one
$ curl https://xxxxxxxxxx.execute-api.eu-central-1.amazonaws.com/dev/Hello Serverless!% ###The output!
Conclusion
In this tutorial, we’ve learnt how to deploy a very simple HTTP endpoint using the Serverless Framework.
Next tutorials:
- “Serverless Framework: Deploy a REST API using AWS Lambda and DynamoDB”, we will see how to create a REST-like API that is integrated with DynamoDB!
- Serverless Framework: Warming up AWS Lambda to avoid “cold start”, we will see what is the “cold start” issue for AWS Lambda functions and how to tackle it using the Serverless Framework
Here the link to the bitbucket repo
Please feel free to comment :)