/lambda_rest_api

A serverless RESTful API built with AWS Lambda, Node.js, DynamoDB and Serverless Framework

Primary LanguageJavaScript

  1. Install the serverless framework

    npm install -g serverless
  2. Configure AWS IAM keys

    Replace [Access key ID] and [Secret access key] with your IAM credentials. If you don't have it, create one in the AWS console, My Security Credentials.

    serverless config credentials --provider aws --key [Access key ID] --secret [Secret access key]
  3. Generate a boilerplate

    serverless create --template aws-nodejs --path [folder]

    Then it will create a boilerplate using the template for Node.js. Go into the root folder, and install the required packages.

    cd [folder]
    npm init -y && npm install

    And also install express and serverless-http with the command below:

    npm install express serverless-http
  4. Expose an HTTP endpoint

    In the serverless.yml file, comment out and change the following code:

    functions:
       app:
          handler: app.server
            events:
                - http:
                    path: /
                    method: get
                    cors: true
  5. Set up Express

    Delete handler.js file, and create one named app.js:

    const express = require('express');
    const app = express();
    const sls = require('serverless-http');
    
    app.get('/', async (req, res, next) => {
        res.status(200).send('It\'s working')
    });
    
    module.exports.server = sls(app);

    Noticed that, AWS Lambda does not support the ES6 import specifier (at the time of 10/10/2019). import express from 'express' will cause an internal server error. And in the log we can see that it says "Runtime.UserCodeSyntaxError: SyntaxError: Unexpected identifier"

  6. Deploy

serverless deploy
  1. Test the API

    In the output, we can see that it exposes an endpoint probably like this.

    endpoints:
    	GET - https://xxxx.execute-api.xx-xxxx-x.amazonaws.com/xxx/

    Let's try it with the command below:

    curl https://xxxx.execute-api.xx-xxxx-x.amazonaws.com/xxx/

    And we will get our message back, "It's working".