Skip to content

devopsutils/serverless-boilerplate

Repository files navigation

Serverless BoilerPlate


About the boilerplate

This code can be used as a starting point for a new Serverless based project. It contains a working example of a very basic movie rental shop. The code was developed in node v10.16.3. This example is being used by the React Boilerplate. There is also an example of a CI/CD automated configuration Serverless CI/CD

Getting started

You can get started quickly with:

git clone https://github.com/mariancraciun1983/serverless-boilerplate.git
nvm use
# skip this to use npx serverless in the local folder
npm install -g serverless
yarn
yarn run dev
# Now the APIs (offline mode) will listen to 0.0.0.0:4000

curl http://localhost:4000/dev/api/genres | json_pp
#{
#   "genres" : [
#      {
#         "name" : "Action",
#         "slug" : "action"
#		 .....

Directory Structure

The most important paths are:

  • __mock__ - data used by APIs as there are no db connection
  • __test__ - unit tests
  • config - local|prod defaults
  • handlers - handlers configurations
  • src
    • endpoints - handlers implementation
    • utils - utilities

NPM Scripts

To support development, testing and building there are a bunch of scripts like:

  • dev - serverless in offline mode (useful for development/local use)
  • lint - alias of the lint:app lint:styles lint:backend
  • lint:src - linter for the frontend code
  • lint:tests - linter for the test code
  • test - test runner for unit tests
  • test:watch- test runner in watch mode useful while writing tests
  • package:prod - packager for production - useful in CI/CD
  • deploy:dev - deploy the dev version
  • deploy:prod - deploy the prod version

Configuration

The config folder contains files for local and prod environments. You can also override parameters at run-time. Also the serverless.yml points to eu-central-1 as default. That can be changed at runtime with the --region eu-central-1 extra parameter, or just edited in the config.

# deploy using yarn script alias with default settings from config/prod.yml
yarn run package:prod

# run deploy manually with custom params
serverless package --region us-east-1
# or
npx serverless package --region us-east-1

Deployment

In order to deploy your stack to AWS, first make sure that credentials are configured AWS - Credentials. Once done, check your config files (serverless.yml and config/.*.yml) and than:

yarn run deploy:prod # or serverless deploy --stage=prod
# Serverless: Packaging service...
# Serverless: Excluding development dependencies...
# Serverless: Creating Stack...
# Serverless: Checking Stack create progress...
# .....
# Serverless: Stack create finished...
# Serverless: Uploading CloudFormation file to S3...
# Serverless: Uploading artifacts...
# Serverless: Uploading service serverless-boilerplate.zip file to S3 (158.73 KB)...
# Serverless: Validating template...
# Serverless: Updating Stack...
# Serverless: Checking Stack update progress...
# ...............................................................................................................................................................
# Serverless: Stack update finished...
# Service Information
# service: serverless-boilerplate
# stage: prod
# region: us-east-1
# stack: serverless-boilerplate-prod
# resources: 53
# api keys:
#   None
# endpoints:
#   POST - https://ctgiklbj9k.execute-api.us-east-1.amazonaws.com/prod/api/auth/login
#   POST - https://ctgiklbj9k.execute-api.us-east-1.amazonaws.com/prod/api/auth/register
#   POST - https://ctgiklbj9k.execute-api.us-east-1.amazonaws.com/prod/api/auth/token
#   GET - https://ctgiklbj9k.execute-api.us-east-1.amazonaws.com/prod/api/genres
#   GET - https://ctgiklbj9k.execute-api.us-east-1.amazonaws.com/prod/api/movies
#   GET - https://ctgiklbj9k.execute-api.us-east-1.amazonaws.com/prod/api/cart
#   POST - https://ctgiklbj9k.execute-api.us-east-1.amazonaws.com/prod/api/cart


curl https://ctgiklbj9k.execute-api.us-east-1.amazonaws.com/prod/api/genres | json_pp
#{
#   "genres" : [
#      {
#         "name" : "Action",
#         "slug" : "action"
#		 .....

Destroying

When you're done with this project, you can easily remove all resources created with:

serverless remove --stage prod #or dev
# or
npx serverless remove --stage prod #or dev

#Others You could configure the region,app-name and the stage via command line. This is useful in a CI/CD:

npx serverless deploy --name serverless-boilerplate-ci --region eu-central-1 --stage dev

About

Serverless boilerplate with tests, linting and offline development

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors