DEV Community

loading...
Cover image for Building REST API using AWS with Serverless, Node.js, Express
AWS Community Builders

Building REST API using AWS with Serverless, Node.js, Express

Salah Elhossiny
ML engineer || AWS Certified MLS || AWS Community Builders member || Fullstack developer
Updated on ・1 min read

Getting started

To get started, you'll need the Serverless Framework installed. You'll also need your environment configured with AWS credentials.

Then, let's install a few dependencies. We'll install the express framework, as well as the serverless-http:

$ npm install --save express serverless-http 
Enter fullscreen mode Exit fullscreen mode

Then create a file index.js and initialize it like so:

const serverless = require('serverless-http');
const express = require('express')
const app = express()

app.get('/', function (req, res) {
  res.send('Hello World!')
})

module.exports.handler = serverless(app);
Enter fullscreen mode Exit fullscreen mode

The serverless-http package is a handy piece of middleware that handles the interface between your Node.js application and the specifics of API Gateway.

Then create a file serverless.yml:

  service: my-express-application

provider:
  name: aws
  runtime: nodejs6.10
  stage: dev
  region: us-east-1

functions:
  app:
    handler: index.handler
    events:
      - http: ANY /
      - http: 'ANY {proxy+}'
Enter fullscreen mode Exit fullscreen mode

Now deploy

$ sls deploy 
Enter fullscreen mode Exit fullscreen mode

Hooray! congratulations :D

Coming part for how to use Dyanmodb as a data resource for enriching API.

resources :
Book: Full Stack Serverless: Modern Application Development for Nader Dabit
https://www.serverless.com/blog/serverless-express-rest-api

Discussion (0)