├── .babelrc ├── .gitignore ├── .nvmrc ├── LICENSE ├── README.md ├── handler.js ├── package.json ├── serverless.yml └── webpack.config.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": ["source-map-support", "transform-runtime"], 3 | "presets": ["env", "stage-3"] 4 | } 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | 39 | # vim 40 | .*.sw* 41 | Session.vim 42 | 43 | # Serverless 44 | .webpack 45 | .serverless 46 | -------------------------------------------------------------------------------- /.nvmrc: -------------------------------------------------------------------------------- 1 | 6.10.2 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 datree.io 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Serverless Web Template 2 | 3 | ## The template is based on 4 | 5 | * [Node.js](https://nodejs.org/) version 6.10.2 (AWS Lambda version) 6 | * [Babel](https://babeljs.io/) for ES7 async/await support (and more) 7 | * [Koa](http://koajs.com/) version 2 8 | * [Serverless](https://serverless.com/) framework 9 | * [AWS X-Ray](https://aws.amazon.com/xray/) 10 | * [AWS API Gateway](https://aws.amazon.com/api-gateway/) 11 | 12 | ## Installation 13 | 14 | * `$ nvm install` (will use the .nvmrc Node.js version 6.10.2) 15 | * `$ npm install` (install dependencies) 16 | * `$ npm install -g serverless` (global installation of serverless) 17 | 18 | ## Usage 19 | 20 | * `$ npm run dev` (run serverless-offline for local development) 21 | * `$ npm run deploy-dev` (deploys the service to dev) 22 | * `$ npm run deploy-prod` (deploys the service to prod) 23 | 24 | (using the AWS credentials in ~/.aws/credentials) 25 | 26 | ## Development 27 | 28 | There are 2 example endpoints under the `handler.js` file (run the local dev server `$ npm run dev`) 29 | 30 | ### GET / 31 | 32 | ```bash 33 | $ curl -X GET http://localhost:3000/ 34 | ``` 35 | 36 | ### POST /echo 37 | 38 | ```bash 39 | $ curl -X POST \ 40 | http://localhost:3000/echo \ 41 | -H 'Content-Type: application/json' \ 42 | -d '{"name": "testing echo service"}' 43 | ``` 44 | 45 | The template is based on [Serverless Node.js Starter](https://github.com/AnomalyInnovations/serverless-nodejs-starter) 46 | -------------------------------------------------------------------------------- /handler.js: -------------------------------------------------------------------------------- 1 | const serverless = require('serverless-http') 2 | 3 | const koa = require('koa') 4 | const bodyParser = require('koa-bodyparser-node6') 5 | const Router = require('koa-router') 6 | const app = new koa() 7 | const router = new Router() 8 | 9 | router.get('/', (ctx, next) => { 10 | ctx.body = 'Hello World!' 11 | }) 12 | 13 | router.post('/echo', (ctx, next) => { 14 | let body = ctx.request.body 15 | ctx.body = body 16 | }) 17 | 18 | app 19 | .use(bodyParser()) 20 | .use(router.routes()) 21 | .use(router.allowedMethods()) 22 | 23 | module.exports.handler = serverless(app) 24 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "serverless-web-template", 3 | "version": "0.0.1", 4 | "description": "Serverless service template", 5 | "main": "handler.js", 6 | "scripts": { 7 | "test": "exit 0", 8 | "dev": "serverless offline start", 9 | "deploy-dev": "serverless deploy -s dev -v", 10 | "deploy-prod": "serverless deploy -s prod -v" 11 | }, 12 | "author": "Shimon Tolts ", 13 | "license": "MIT", 14 | "repository": { 15 | "type": "git" 16 | }, 17 | "devDependencies": { 18 | "babel-core": "^6.26.0", 19 | "babel-loader": "^7.1.2", 20 | "babel-plugin-source-map-support": "^1.0.0", 21 | "babel-plugin-transform-runtime": "^6.23.0", 22 | "babel-preset-env": "^1.6.1", 23 | "babel-preset-stage-3": "^6.24.1", 24 | "serverless-offline": "^3.16.0", 25 | "serverless-webpack": "^4.2.0", 26 | "webpack": "^3.10.0", 27 | "webpack-node-externals": "^1.6.0" 28 | }, 29 | "dependencies": { 30 | "babel-runtime": "^6.26.0", 31 | "koa": "^2.4.1", 32 | "koa-bodyparser-node6": "^4.2.0", 33 | "koa-router": "^7.4.0", 34 | "serverless-http": "^1.5.3", 35 | "serverless-plugin-tracing": "^2.0.0", 36 | "source-map-support": "^0.4.18" 37 | }, 38 | "prettier": { 39 | "semi": false, 40 | "singleQuote": true, 41 | "printWidth": 120 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /serverless.yml: -------------------------------------------------------------------------------- 1 | service: serverless-web-template 2 | 3 | plugins: 4 | - serverless-webpack 5 | - serverless-offline 6 | - serverless-plugin-tracing 7 | 8 | # Enable auto-packing of external modules 9 | custom: 10 | webpackIncludeModules: true 11 | 12 | provider: 13 | name: aws 14 | runtime: nodejs6.10 15 | region: us-east-1 16 | memorySize: 128 17 | timeout: 30 18 | tracing: true 19 | iamRoleStatements: 20 | - Effect: "Allow" # xray permissions (required) 21 | Action: 22 | - "xray:PutTraceSegments" 23 | - "xray:PutTelemetryRecords" 24 | Resource: 25 | - "*" 26 | 27 | functions: 28 | api: 29 | handler: handler.handler 30 | name: serverless-web-template 31 | description: Servelress Web Template 32 | events: 33 | - http: 34 | path: /{any+} 35 | method: ANY 36 | cors: true 37 | - http: 38 | path: / 39 | method: ANY 40 | cors: true 41 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | var slsw = require('serverless-webpack'); 2 | var nodeExternals = require('webpack-node-externals'); 3 | 4 | module.exports = { 5 | entry: slsw.lib.entries, 6 | target: 'node', 7 | // Generate sourcemaps for proper error messages 8 | devtool: 'source-map', 9 | // Since 'aws-sdk' is not compatible with webpack, 10 | // we exclude all node dependencies 11 | externals: [nodeExternals()], 12 | // Run babel on all .js files and skip those in node_modules 13 | module: { 14 | rules: [{ 15 | test: /\.js$/, 16 | loader: 'babel-loader', 17 | include: __dirname, 18 | exclude: /node_modules/, 19 | }] 20 | } 21 | }; 22 | --------------------------------------------------------------------------------