Skip to content

anishkny/realworld-dynamodb-lambda

Repository files navigation

RealWorld Example App

RealWorld Backend Build Status Coverage Status Greenkeeper badge Known Vulnerabilities Gitter

AWS DynamoDB + Lambda codebase containing real world examples (CRUD, auth, advanced patterns, etc) that adheres to the RealWorld spec and API.

This codebase was created to demonstrate a fully fledged fullstack application built with AWS DynamoDB + Lambda including CRUD operations, authentication, routing, pagination, and more.

We've gone to great lengths to adhere to the AWS DynamoDB + Lambda community styleguides & best practices.

For more information on how to this works with other frontends/backends, head over to the RealWorld repo.

Getting started

Requires Node 8 or higher

Clone this repo, and cd into it:

git clone https://github.com/anishkny/realworld-dynamodb-lambda
cd realworld-dynamodb-lambda

Starting the local server

npm install
npm run start

This should start local DynamoDB emulator and Serverless offline. You can now make API calls against http://localhost:3000/api like this:

curl http://localhost:3000/api/articles

Serverless: GET /api/articles (λ: listArticles)
Serverless: The first request might take a few extra seconds
Serverless: [200] {"statusCode":200,"headers":{"Access-Control-Allow-Origin":"*","Access-Control-Allow-Credentials":true},"body":"{\"articles\":[]}"}

Running tests locally

npm test

See sample test run log and network traffic.

How it works

Overview

This repo uses Serverless Framework to describe, test and deploy the RealWorld REST API to AWS Lambda. AWS Lambda provides "serverless" cloud functions as a service. AWS API Gateway is used to expose the deployed Lambda functions as a HTTP REST API.

Architecture Diagram

API

The API is described in the serverless.yml file. For example the following snippet instructs AWS Lambda to execute the create method in src/User.js whenever a POST method is called on /api/users:

functions:

  ## Users API
  createUser:
    handler: src/User.create
    events:
      - http:
          method: POST
          path: /api/users
          cors: true

  ...

Storage

For storage, AWS DynamoDB a managed serverless NoSQL database is used. Tables are created to store users, articles and comments also described in serverless.yml file. For example:

resources:
  Resources:

    UsersDynamoDBTable:
      Type: 'AWS::DynamoDB::Table'
      DeletionPolicy: Retain
      Properties:
        AttributeDefinitions:
        ...

Deployment

To deploy the code to AWS, simply execute:

npm run deploy

This will use serverless to deploy the API as described in serverless.yml.

Once deployed, you can test the deployed API by executing:

npm run test:deployed