notreniev / serverless-dynamodb-api-boilerplate

Production-ready Serverless API with DynamoDB, Typescript and best practices, sponsored by https://dynobase.dev

Home Page:https://dynobase.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Serverless DynamoDB API Boilerplate

Kickstart your Cloud-native and Serverless project in minutes. This is work in progress


serverless

About

This repository is a result of many lessons from launching variety of Serverless powered applications to the production. It was created according to many best practices and aims to provide developers a rapid start while also being production-ready.

Features

  • Written in Typescript, compiled by Webpack using Serverless Webpack plugin. Optimized for huge projects where Out of Memory errors are frequent
  • Works fully offline thanks to Serverless Offline and DynamoDB Local ran using Docker Compose
  • Easibly Testable using Jest
  • Interacts with DynamoDB using DocumentClient and DynamoDB Toolbox
  • Works with both REST and GraphQL
  • Managable - To avoid spaghetti code, serverless.yml is divided into logical parts. Functions' code is placed next to their definitions
  • CORS by default - allows preflight requests to the resource using the OPTIONS method
  • HTTP API - uses new version of API Gateway which provides costs savings up to 70%

Quick start

Prerequisites:

Start by pulling this repo using git:

git clone https://github.com/Dynobase/serverless-dynamodb-api-boilerplate

Or using Serverless Framework:

serverless create --template-url https://github.com/Dynobase/serverless-dynamodb-api-boilerplate --path myService

Once ready, install dependencies:

yarn

After that, start the project locally:

docker-compose up -d # To start DynamoDB local

yarn provision-local-table # To provision DynamoDB table locally

yarn dev # Run project locally

# Add item to the table
curl --location --request POST 'localhost:3000/dev' --data-raw '{ "name":"John Doe" }'

Deploying

sls deploy

Adding new function

  1. Create new folder in functions directory. It should contain:
  • function.yml - function definition in accordance to Serverless Framework contract
  • function.ts - actual implementation of Lambda function

Optionally:

  • role.yml - in order to get better control over IAM Role tied to the function, referenced inside function's role property
  • controller.ts - the best practice is to keep functions as thin as possible. Controllers with injectable dependencies allow that
  • request_schema.json - request object definition in JSON Schema
  1. Include newly created function in serverless.yml like so:
- ${file(./functions/<new_function_directory>/function.yml)}
  1. Run yarn generate-request-types to generate Typescript types from request JSON Schema

  2. Complete implementation of your function

Viewing DynamoDB Local contents

Apart from using DynamoDB Shell or CLI, you can use Dynobase to view contents of your local tables:

Dynobase Interface

Contributing

Contributions are more than welcome.

Licensing

This project is licensed under the MIT License.

About

Production-ready Serverless API with DynamoDB, Typescript and best practices, sponsored by https://dynobase.dev

https://dynobase.dev

License:MIT License


Languages

Language:TypeScript 70.8%Language:JavaScript 29.2%