Serverless DynamoDB API Boilerplate
Kickstart your Cloud-native and Serverless project in minutes. This is work in progress
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:
- Serverless Framework
- Node.js
- Git
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
- Create new folder in
functions
directory. It should contain:
function.yml
- function definition in accordance to Serverless Framework contractfunction.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'srole
propertycontroller.ts
- the best practice is to keep functions as thin as possible. Controllers with injectable dependencies allow thatrequest_schema.json
- request object definition in JSON Schema
- Include newly created function in
serverless.yml
like so:
- ${file(./functions/<new_function_directory>/function.yml)}
-
Run
yarn generate-request-types
to generate Typescript types from request JSON Schema -
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:
Contributing
Contributions are more than welcome.
Licensing
This project is licensed under the MIT License.