A simple API powered by Serverless, Node.js and DynamoDB, intended as a starting point for Serverless APIs. This project uses serverless-webpack with Babel to compile handler functions written with the next level JavaScript, serverless-offline to run locally and serverless-dynamodb-local to run a local DynamoDB server. I've also created a simple React/Redux-powered UI to front this API.
Auth0 handles authentication. You must signup/login to generate an auth token and gain access to the secured area. All endpoints in the API check validity of the auth token and return unauthorised if invalid, the UI then prompts you to log in again. The API also determines the identity of the user via the auth token.
See Apiary for API structure - defined in apiary.apib.
- Serverless
- Node.js
- DynamoDB
- Webpack
- Serverless-offline
- Serverless-webpack
- Serverless-dynamodb-local
- Jsonwebtoken
- Babel
- Bitbucket Pipelines
Please note that Serverless must be installed globally before running any commands below: yarn global add serverless.
To run locally you must run two servers - DB and API.
Serverless-webpack, serverless-dynamodb-local and serverless-offline offer great tooling for local Serverless development. To start a local server that will mimic AWS API Gateway, run the commands below. Both servers will fire up and code will be reloaded upon change so that every request to your local server will serve the latest code.
Serverless-dynamodb-local requires Java Runtime Engine (JRE) version 6.x or newer.
There is currently an issue that requires serverless dynamodb start to be run explicitly, previously serverless offline --location .webpack would also start the local DynamoDB server. Hopefully this will be resolved soon!
DYNAMODB_PORT environment variable must be set before serverless dynamodb start command below.
E.g. DYNAMODB_PORT=8001 serverless dynamodb start
DYNAMODB_PORT and AUTH0_CLIENT_SECRET environment variables must be set before serverless offline --location .webpack command below.
E.g. DYNAMODB_PORT=8001 AUTH0_CLIENT_SECRET=YOUR_SECRET serverless offline --location .webpack
yarn install
serverless dynamodb install
serverless dynamodb start
serverless offline --location .webpack
TBC
To deploy/manage this service you will need to create an IAM user with the required permissions and set credentials for this user - see here for further info. After you have done this, run the commands below to deploy the service:
NODE_ENV and AUTH0_CLIENT_SECRET environment variables must be set to production before serverless deploy command below.
E.g. NODE_ENV=production AUTH0_CLIENT_SECRET=YOUR_SECRET serverless deploy
yarn install
serverless deploy
Manual steps suck so this project uses Bitbucket Pipelines to automate the build and deployment to AWS - see bitbucket-pipelines.yml. AWS credentials are set using Bitbucket Pipelines environment variables.
I've created a Docker-powered build/deployment environment for Serverless projects to use with Bitbucket Pipelines which is used by this project.