Giter Site home page Giter Site logo

serverless-react-boilerplate's Introduction

serverless-react-boilerplate

serverless license Join the chat at https://gitter.im/99xt/serverless-react-boilerplate

Sample Todo App

screen shot 2017-08-29 at 4 49 58 am

Requirements

  • Java Runtime Engine (JRE) version 6.x or newer to run dynamodb locally.

Features

  • Support serverless v1.26.0
  • Support offline development with dynamodb, lambda and API Gateway
  • Support local dynamodb seeds/migrations
  • Build automation in client and server to ease local development
  • Deploy to multiple API Services
  • Use of Environment variables
  • Lambda CRUD operations for a Todo application with live reload
  • React web application to utilize the API

How to develop and test offline?

We have used serverless-offline plugin and serverless-dynamodb-local plugin in this boilerplate. You can declare your table templates and seeds in api/todo/offline/migrations/ folder just like the todo.json template. When you spin up the offline server, these tables will be used as the datasources for your lambda functions.

Production vs Offline

Thanks to the offline plugin's environment variable IS_OFFLINE we can select between local dynamodb and aws dynamodb.

var dynamodbOfflineOptions = {
        region: "localhost",
        endpoint: "http://localhost:8000"
    },
    isOffline = () => process.env.IS_OFFLINE;

var client = isOffline() ? new AWS.DynamoDB.DocumentClient(dynamodbOfflineOptions) :  new AWS.DynamoDB.DocumentClient();

Directory structure

|──api
|  |──todo
|  |  |──lib
|  |  |  |──todo.js
|  |  |  |──helper.js
|  |  |  |──response.js
|  |  |──handler.js
|  |  |──database
|  |  |──dynamodb.js
|  |  |──offline
|  |  |  |──migrations
|  |  |  |  |──todo.yml
|  |  |  |  |──todo-seed.json
|  |  |──config.yml
|  |  |──serverless.yml
|  |  |──package.json
|──web
|  |──src
|  |  |──components
|  |  |──index.js
|  |──public
|  |  |──index.html
|  |──package.json
|──gulpfile.js
|──package.json


Installation & Usage

  • Clone this repo.
  • Make sure AWS credentials are setup properly. Otherwise refer this document
  aws configure --profile peter
  • Add the aws cli profile name and region on to serverless.yml file located at /api/todo/serverless.yml
  ...
  provider:
  name: aws
  runtime: nodejs6.10
  profile: peter
  stage: dev
  region: eu-west-2
  ...
  • Install serverless globally
 npm i -g serverless
  • Install project dependencies. cd serverless-react-boilerplate and type,
  npm install 
  • Install dynamodb local. (Make sure you have Java runtime 6.x or newer)
  npm run db-setup
  • Run the app with the local server
  npm run app
  • Browser will open the todo app at http://localhost:3001

Deploying to AWS

When you are ready to deploy your database and api to AWS, you can create multiple APIGateways for different service level stages. For example you can create "dev" and "production" stages. When you deploy to a specific stage, it will create a separate database tables for that stage.

Following command will deploy your lambda functions and APIGateway onto 'prod' stage.

cd api
cd todo
serverless deploy --stage prod

If you want to test your React app with the online API and Database, you may have to change the, BASE_URL of the react app found in web/src/App.js. Change its value from http://localhost:3000 to your APIGateway uri.

Application Secrets Keys

You can define application secret keys in config.yml file. For example if you need to have a database connection string and use it in your lambda function, define it as follows.

prod:
  DB_STRING: <my-db-connection-string>

Then in the serverless.yml file,

custom:
  DB_STRING: ${file(./config.yml):${self:custom.stage}.DB_STRING}

This will corretly select the DB_STRING corresponding to the defined stage.

You should NOT commit config.yml to your version control system

Deploying react web app

Once you have setup a S3 bucket with static web hosting enabled you can simply build your react app and deploy to that bucket. Make sure to change BASE_URL to refer your production APIGateway endpoint.

On the root level package.json file add that bucket name and your AWS profile name on the deploy-s3 task. After that run the following command.

  npm run deploy-s3

See following vidoes for a step by step guide to create a s3 bucket and configure static web hosting.

Contribution

Your contributions are much appriciated.

Release Log

  • Release v4.1.0 - Updated boilerplate to support Lambda Proxy
  • Release v4.0.0 - Added support for [email protected]
  • Release v3.0.0 - Added environment variables for database table names & Feature to deploy in multiple APIGateway service level stages.
  • Release v2.2.0 - Added foundation css framework for the react client
  • Release v2.1.1 - Improvements in react web app
  • Release v2.0.1 - Dynamodb local table creation bug fix
  • Release v2.0.0 - Added support for serverless v1.0 and Issues #24 #25
  • Release v1.0.3 - Fixed local dynamobd get packaged for deployment
  • Release v1.0.2 - Added support for [email protected]

Links

License

MIT

serverless-react-boilerplate's People

Contributors

ashanfernando avatar dinukadesilva avatar gayashanbc avatar gitter-badger avatar harshanas avatar janakshi28 avatar jaydenrasmussen avatar lakindu95 avatar mjzone avatar obie avatar pelhage avatar rehrumesh avatar tromesh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

serverless-react-boilerplate's Issues

3 package.json files

Will it affect the structure of the boilerplate, if package.json files gets merged somehow ?

Developed Authentication Per User

I've forked the latest version of the aws-react branch as of today (11/02/2016) and integrated with the serverless-authentication-boilerplate. Features are:

  • Writes user data to user table.
  • Authenticates per user so that one user cannot modify another's data.
  • Authenticates user before responding with auth token to prevent hacking.
  • Added linting ("npm run lint" from root folder)

Working demo here: http://sls-react-auth.s3-website-us-east-1.amazonaws.com/ (facebook login only).

See: https://github.com/jcummins54/serverless-react-boilerplate
and https://github.com/jcummins54/serverless-authentication-boilerplate

Would be happy to collaborate to make this a branch here.

can i use it in 2021?

I want to use a fullstack template, so far I can't find a complete one, this looks a lot like it, but it's from 2019, I wanted to know if it can be used or due to time, are there new things to update manually?

Environmental variables are not properly loading in Lambda

Environmental variables are not properly loading in Lambda with serverless-plugin-write-env-vars

serverless-plugin-write-env-vars users dotenv npm internally and creates .env file in serverless deploy stage so that it bundles inside the zip for deployment. However dotenv requires to run the following code as early as possible in the application at execution.

require('dotenv').config();

reference: https://www.npmjs.com/package/dotenv

offline doesn't create table.

I follow exactly same, However it doesn't work.
'''
Serverless: Failure: Error: ResourceNotFoundException: Cannot do operations on a non-existent table
'''
this is why failed.

I searched a little,

'''
custom:
stage: ${opt:stage, self:provider.stage}
dynamodb:
start:
migration: true
migration:
dir: offline/migrations
table_prefix: ${self:custom.stage}
writeEnvVars:
REMOTE_STAGE: ${self:custom.stage}
'''

that part, migration: true, migration: dir: offline/...
is this really works??
Examples what I found are make table's information in serverless.yml using 'resource'

Error when following installation instructions

I get the following error when running either npm install or yarn on a fresh clone of the repository.

$ cd serverless && npm install
npm ERR! git rev-list -n1 serverless_v1: fatal: ambiguous argument 'serverless_v1': unknown revision or path not in the working tree.
npm ERR! git rev-list -n1 serverless_v1: Use '--' to separate paths from revisions, like this:
npm ERR! git rev-list -n1 serverless_v1: 'git <command> [<revision>...] -- [<file>...]'
npm ERR! git rev-list -n1 serverless_v1: 
npm ERR! Darwin 14.0.0
npm ERR! argv "/usr/local/Cellar/node/0.10.32/bin/node" "/usr/local/bin/npm" "install"
npm ERR! node v6.4.0
npm ERR! npm  v3.10.3
npm ERR! code 128

npm ERR! Command failed: git rev-list -n1 serverless_v1
npm ERR! fatal: ambiguous argument 'serverless_v1': unknown revision or path not in the working tree.
npm ERR! Use '--' to separate paths from revisions, like this:
npm ERR! 'git <command> [<revision>...] -- [<file>...]'
npm ERR! 
npm ERR! 
npm ERR! If you need help, you may report this error at:
npm ERR!     <https://github.com/npm/npm/issues>

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/benhirsch/Github/1000minutes/serverless/npm-debug.log
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

And then these are the offending lines in the log:

769 silly mapToRegistry uri https://registry.yarnpkg.com/serverless-dynamodb-local
770 verbose addNameVersion registry:https://registry.yarnpkg.com/serverless-dynamodb-local not in flight; fetching
771 verbose get https://registry.yarnpkg.com/serverless-dynamodb-local not expired, no request
772 silly cache afterAdd [email protected]
773 verbose afterAdd /Users/benhirsch/.npm/serverless-dynamodb-local/0.2.17/package/package.json not in flight; writing
774 verbose correctMkdir /Users/benhirsch/.npm correctMkdir not in flight; initializing
775 verbose afterAdd /Users/benhirsch/.npm/serverless-dynamodb-local/0.2.17/package/package.json written
776 verbose updateRemote git fetch -a origin (https://github.com/dherault/serverless-offline.git/)
777 verbose correctMkdir /Users/benhirsch/.npm/_git-remotes correctMkdir not in flight; initializing
778 verbose setPermissions git+https://github.com/dherault/serverless-offline.git/#serverless_v1 set permissions on /Users/benhirsch/.npm/_git-remotes/git-https-github-com-dherault-serverless-offline-git-serverless-v1-6b02a695
779 verbose resolveHead git+https://github.com/dherault/serverless-offline.git/#serverless_v1 original treeish: serverless_v1
780 info git [ 'rev-list', '-n1', 'serverless_v1' ]
781 error git rev-list -n1 serverless_v1: fatal: ambiguous argument 'serverless_v1': unknown revision or path not in the working tree.
781 error git rev-list -n1 serverless_v1: Use '--' to separate paths from revisions, like this:
781 error git rev-list -n1 serverless_v1: 'git <command> [<revision>...] -- [<file>...]'
782 silly fetchPackageMetaData Error: Command failed: git rev-list -n1 serverless_v1
782 silly fetchPackageMetaData fatal: ambiguous argument 'serverless_v1': unknown revision or path not in the working tree.
782 silly fetchPackageMetaData Use '--' to separate paths from revisions, like this:
782 silly fetchPackageMetaData 'git <command> [<revision>...] -- [<file>...]'
782 silly fetchPackageMetaData
782 silly fetchPackageMetaData     at ChildProcess.exithandler (child_process.js:206:12)
782 silly fetchPackageMetaData     at emitTwo (events.js:106:13)
782 silly fetchPackageMetaData     at ChildProcess.emit (events.js:191:7)
782 silly fetchPackageMetaData     at maybeClose (internal/child_process.js:852:16)
782 silly fetchPackageMetaData     at Socket.<anonymous> (internal/child_process.js:323:11)
782 silly fetchPackageMetaData     at emitOne (events.js:96:13)
782 silly fetchPackageMetaData     at Socket.emit (events.js:188:7)
782 silly fetchPackageMetaData     at Pipe._handle.close [as _onclose] (net.js:492:12)
782 silly fetchPackageMetaData  error for serverless-offline@git+https://github.com/dherault/serverless-offline.git/#serverless_v1 { Error: Command failed: git rev-list -n1 serverless_v1
782 silly fetchPackageMetaData fatal: ambiguous argument 'serverless_v1': unknown revision or path not in the working tree.
782 silly fetchPackageMetaData Use '--' to separate paths from revisions, like this:
782 silly fetchPackageMetaData 'git <command> [<revision>...] -- [<file>...]'
782 silly fetchPackageMetaData
782 silly fetchPackageMetaData     at ChildProcess.exithandler (child_process.js:206:12)
782 silly fetchPackageMetaData     at emitTwo (events.js:106:13)
782 silly fetchPackageMetaData     at ChildProcess.emit (events.js:191:7)
782 silly fetchPackageMetaData     at maybeClose (internal/child_process.js:852:16)
782 silly fetchPackageMetaData     at Socket.<anonymous> (internal/child_process.js:323:11)
782 silly fetchPackageMetaData     at emitOne (events.js:96:13)
782 silly fetchPackageMetaData     at Socket.emit (events.js:188:7)
782 silly fetchPackageMetaData     at Pipe._handle.close [as _onclose] (net.js:492:12)
782 silly fetchPackageMetaData   killed: false,
782 silly fetchPackageMetaData   code: 128,
782 silly fetchPackageMetaData   signal: null,
782 silly fetchPackageMetaData   cmd: 'git rev-list -n1 serverless_v1' }
783 silly rollbackFailedOptional Starting
784 silly rollbackFailedOptional Finishing
785 silly runTopLevelLifecycles Starting
786 silly runTopLevelLifecycles Finishing
787 silly install printInstalled
788 verbose stack Error: Command failed: git rev-list -n1 serverless_v1
788 verbose stack fatal: ambiguous argument 'serverless_v1': unknown revision or path not in the working tree.
788 verbose stack Use '--' to separate paths from revisions, like this:
788 verbose stack 'git <command> [<revision>...] -- [<file>...]'
788 verbose stack
788 verbose stack     at ChildProcess.exithandler (child_process.js:206:12)
788 verbose stack     at emitTwo (events.js:106:13)
788 verbose stack     at ChildProcess.emit (events.js:191:7)
788 verbose stack     at maybeClose (internal/child_process.js:852:16)
788 verbose stack     at Socket.<anonymous> (internal/child_process.js:323:11)
788 verbose stack     at emitOne (events.js:96:13)
788 verbose stack     at Socket.emit (events.js:188:7)
788 verbose stack     at Pipe._handle.close [as _onclose] (net.js:492:12)
789 verbose cwd /Users/benhirsch/Github/1000minutes/serverless
790 error Darwin 14.0.0
791 error argv "/usr/local/Cellar/node/0.10.32/bin/node" "/usr/local/bin/npm" "install"
792 error node v6.4.0
793 error npm  v3.10.3
794 error code 128
795 error Command failed: git rev-list -n1 serverless_v1
795 error fatal: ambiguous argument 'serverless_v1': unknown revision or path not in the working tree.
795 error Use '--' to separate paths from revisions, like this:
795 error 'git <command> [<revision>...] -- [<file>...]'
796 error If you need help, you may report this error at:
796 error     <https://github.com/npm/npm/issues>
797 verbose exit [ 1, true ]

Can anybody tell me what exactly is going wrong?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.