├── .editorconfig ├── .eslintrc.json ├── .gitignore ├── LICENSE ├── README.md ├── handlers ├── testfct1 │ └── handler.js └── testfct2 │ └── handler.js ├── package-lock.json ├── package.json ├── serverless.yml ├── src ├── test1.js └── test2.js └── webpack.lambda.config.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | indent_size = 2 6 | end_of_line = lf 7 | charset = utf-8 8 | 9 | [*.{json,yml}] 10 | indent_style = space 11 | indent_size = 2 12 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "es6": true, 4 | "node": true 5 | }, 6 | "extends": [ 7 | "eslint:recommended", 8 | "plugin:lodash/recommended", 9 | "plugin:import/errors", 10 | "plugin:import/warnings" 11 | ], 12 | "installedESLint": true, 13 | "parser": "babel-eslint", 14 | "parserOptions": { 15 | "sourceType": "module", 16 | "ecmaFeatures": { 17 | "classes": true, 18 | "experimentalObjectRestSpread": true, 19 | "jsx": true 20 | } 21 | }, 22 | "plugins": [ 23 | "promise", 24 | "lodash", 25 | "import" 26 | ], 27 | "rules": { 28 | "indent": [ 29 | "error", 30 | "tab" 31 | ], 32 | "linebreak-style": [ 33 | "error", 34 | "unix" 35 | ], 36 | "semi": [ 37 | "error", 38 | "always" 39 | ], 40 | "promise/always-return": "error", 41 | "promise/no-return-wrap": "error", 42 | "promise/param-names": "error", 43 | "promise/catch-or-return": "error", 44 | "promise/no-native": "off", 45 | "promise/no-nesting": "off", 46 | "promise/no-promise-in-callback": "warn", 47 | "promise/no-callback-in-promise": "warn", 48 | "promise/avoid-new": "warn", 49 | "import/no-named-as-default": "off", 50 | "lodash/import-scope": "off", 51 | "lodash/preferred-alias": "off", 52 | "lodash/prop-shorthand": "off" 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | dist/ 3 | .webpack/ 4 | .serverless/ 5 | /out 6 | /testpkg 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | Copyright (c) 2016,2017 Frank Schmid 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | this software and associated documentation files (the "Software"), to deal in 6 | the Software without restriction, including without limitation the rights to 7 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 8 | the Software, and to permit persons to whom the Software is furnished to do so, 9 | subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 17 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 18 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Serverless test project 2 | 3 | This project uses some central functionality of Serverless and 4 | shows how the alias plugin works. 5 | 6 | You can deploy the available branches `master` and `warmup-apig` to different 7 | aliases. The branches have different configurations: 8 | 9 | ### master 10 | 11 | Simple Lambda function + DynamoDB table + DynamoDB event source 12 | 13 | ### warmup-apig 14 | 15 | The warmup branch simulates feature development in a project perspective as well 16 | as developer changes in the Serverless configuration. 17 | 18 | The DynamoDB is removed in this branch, APIG endpoints are added and the warmup 19 | plugin has been added to serverless yml. 20 | 21 | This branch can be deployed to a separate alias without destroying anything 22 | that is deployed on master. Branch development in master and warmup can even 23 | get more different over time. 24 | 25 | ### sns-topic 26 | 27 | This branch adds a SNS subscription event to one of the functions. It shows how 28 | the alias plugin will handle these when deployed to different aliases. 29 | 30 | ### authorizer 31 | 32 | This branch deploys a custom authorizer and attaches it to the func1 GET method. 33 | You can call GET https://XXXXXXXXX.execute-api.us-east-1.amazonaws.com/auth/func1 34 | with an Authorization header set to an arbitrary value. The function will echo 35 | the incoming event object, that contains the results of the authorizer under 36 | `requestContext.authorizer`. 37 | 38 | ### Deploying the system 39 | 40 | Checkout the master branch and deploy the stage without specifying an alias (i.e. 41 | the branch gets deployed into the master/stage alias `dev`). 42 | 43 | ``` 44 | git checkout master 45 | npm install 46 | node ./node_modules/serverless/bin/serverless deploy 47 | ``` 48 | 49 | Checkout the warmup branch and deploy it to a different alias 50 | 51 | ``` 52 | git checkout warmup-apig 53 | npm install 54 | node ./node_modules/serverless/bin/serverless deploy --alias=warmup 55 | ``` 56 | 57 | ``` 58 | git checkout sns-topic 59 | npm install 60 | node ./node_modules/serverless/bin/serverless deploy --alias=sns 61 | ``` 62 | 63 | Afterwards you will have everything deployed in your AWS account. The functions 64 | for the different aliases are labeled (aliased) with the alias name. 65 | See the documentation of the alias plugin for details. 66 | 67 | 68 | 69 | Attention: The project will create resources in your AWS account. These will 70 | generate costs. You should delete the created resources (CF stacks) if you do 71 | not need them anymore. 72 | -------------------------------------------------------------------------------- /handlers/testfct1/handler.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | import { Test1 } from '../../src/test1'; 4 | 5 | export function handle(event, context) { 6 | Test1.run(event).asCallback(context.done); 7 | return; // no Promise return as we invoke `context.done` directly! 8 | } 9 | -------------------------------------------------------------------------------- /handlers/testfct2/handler.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | import { Test2 } from '../../src/test2'; 4 | 5 | export function handle(event, context) { 6 | Test2.run(event).asCallback(context.done); 7 | return; // no Promise return as we invoke `context.done` directly! 8 | } 9 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sls-test-project", 3 | "version": "1.0.0", 4 | "description": "Test project for SLS", 5 | "main": "index.js", 6 | "private": true, 7 | "scripts": { 8 | "test": "echo \"Error: no test specified\" && exit 1", 9 | "lint": "eslint src --ext .jsx --ext .js || exit 0", 10 | "offline": "env LOCAL=1 WEBPACK_DEV_SERVER=localhost:3030 node node_modules/serverless/bin/serverless offline --location .webpack --noTimeout --dontPrintOutput" 11 | }, 12 | "author": "Frank Schmid", 13 | "license": "MIT", 14 | "dependencies": { 15 | "babel-runtime": "^6.22.0", 16 | "bluebird": "^3.4.7", 17 | "lodash": "^4.17.4" 18 | }, 19 | "devDependencies": { 20 | "aws-sdk": "^2.86.0", 21 | "babel-core": "^6.22.1", 22 | "babel-eslint": "^7.1.1", 23 | "babel-loader": "^7.1.1", 24 | "babel-preset-node6": "^11.0.0", 25 | "babel-preset-stage-0": "^6.24.1", 26 | "eslint": "^3.15.0", 27 | "eslint-plugin-import": "^2.2.0", 28 | "eslint-plugin-lodash": "^2.3.4", 29 | "eslint-plugin-promise": "^3.4.0", 30 | "file-loader": "^0.9.0", 31 | "json-loader": "^0.5.4", 32 | "serverless": "^1.10.2", 33 | "serverless-aws-alias": "^1.0.0", 34 | "serverless-offline": "^3.8.3", 35 | "serverless-webpack": "^2.0.0", 36 | "source-list-map": "^2.0.0", 37 | "url-loader": "^0.5.7", 38 | "webpack": "^3.3.0", 39 | "webpack-dev-server": "^2.5.1", 40 | "webpack-node-externals": "^1.5.4", 41 | "webpack-sources": "^1.0.1" 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /serverless.yml: -------------------------------------------------------------------------------- 1 | service: sls-test-project 2 | 3 | provider: 4 | name: aws 5 | runtime: nodejs6.10 6 | stage: dev 7 | region: us-east-1 8 | 9 | memorySize: 512 10 | timeout: 15 11 | 12 | iamRoleStatements: 13 | - Effect: Allow # access to DynamoDB 14 | Action: 15 | - dynamodb:* 16 | Resource: 17 | - 'Fn::Join': 18 | - '/' 19 | - 20 | - 'Fn::Join': 21 | - ':' 22 | - 23 | - arn:aws:dynamodb 24 | - Ref: 'AWS::Region' 25 | - Ref: 'AWS::AccountId' 26 | - table 27 | - Ref: TestDynamoDbTable 28 | 29 | stackTags: # Optional CF stack tags 30 | environment: ${opt:stage, self:provider.stage} 31 | application: ${self:service} 32 | product: something 33 | 34 | environment: 35 | SERVERLESS_PROJECT_NAME: ${self:service} # FIXME Required for Sessions 36 | SERVERLESS_PROJECT: ${self:service} 37 | SERVERLESS_STAGE: ${self:custom.stage} 38 | SERVERLESS_REGION: ${self:custom.region} 39 | # Auth 40 | TEST_TABLE_NAME: 41 | Ref: TestDynamoDbTable 42 | 43 | plugins: 44 | - serverless-offline 45 | - serverless-webpack 46 | - serverless-aws-alias 47 | 48 | custom: 49 | stage: ${opt:stage, self:provider.stage} 50 | region: ${opt:region, self:provider.region} 51 | 52 | # Stage dependent variables. 53 | # Usage: ${self:custom.${self:custom.stage}.} 54 | dev: 55 | testvar: test-${self:custom.stage} 56 | 57 | prod: 58 | testvar: test-${self:custom.stage} 59 | 60 | webpack: ./webpack.lambda.config.js 61 | webpackIncludeModules: true 62 | 63 | functions: 64 | testfct1: 65 | description: 'Echo function echoes alias' 66 | handler: handlers/testfct1/handler.handle 67 | events: 68 | - http: 69 | method: GET 70 | path: /func1 71 | - stream: 72 | type: dynamodb 73 | arn: 74 | Fn::GetAtt: 75 | - TestDynamoDbTable 76 | - StreamArn 77 | 78 | resources: 79 | Resources: 80 | 81 | TestDynamoDbTable: 82 | Type: AWS::DynamoDB::Table 83 | DeletionPolicy: Delete 84 | Properties: 85 | AttributeDefinitions: 86 | - AttributeName: myKey 87 | AttributeType: S 88 | KeySchema: 89 | - AttributeName: myKey 90 | KeyType: HASH 91 | ProvisionedThroughput: 92 | ReadCapacityUnits: 1 93 | WriteCapacityUnits: 1 94 | StreamSpecification: 95 | StreamViewType: NEW_AND_OLD_IMAGES 96 | 97 | Outputs: 98 | TestDynamoDbTableName: 99 | Description: Test DynamoDB Table Name 100 | Value: 101 | Ref: TestDynamoDbTable 102 | -------------------------------------------------------------------------------- /src/test1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Test function 1 3 | */ 4 | 5 | import BbPromise from 'bluebird'; 6 | 7 | export class Test1 { 8 | 9 | static run(event) { 10 | return BbPromise.resolve({ statusCode: 200, headers: { 11 | 'Content-Type': 'application/json' 12 | }, body: JSON.stringify(event) }); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/test2.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Test function 1 3 | */ 4 | 5 | import BbPromise from 'bluebird'; 6 | 7 | export class Test2 { 8 | 9 | static run(event) { 10 | return BbPromise.resolve({ statusCode: 200, headers: { 11 | 'Content-Type': 'application/json' 12 | }, body: JSON.stringify(event) }); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /webpack.lambda.config.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Webpack Configuration to package code for deployment to AWS Lambda. 3 | */ 4 | "use strict"; 5 | 6 | const _ = require('lodash'); 7 | const path = require('path'); 8 | 9 | const webpack = require('webpack'); 10 | const NodeExternals = require('webpack-node-externals'); 11 | 12 | module.exports = { 13 | bail: true, // exit on any error 14 | entry: { 15 | // Entry point is our Lambda handler function 16 | 'testfct1': './handlers/testfct1/handler.js', 17 | 'testfct2': './handlers/testfct2/handler.js' 18 | }, 19 | output: { 20 | libraryTarget: 'commonjs2', 21 | path: path.join(__dirname, '.webpack'), 22 | filename: 'handlers/[name]/handler.js', // this should match the first part of function handler in serverless.yml 23 | }, 24 | target: 'node', 25 | resolve: { 26 | extensions: ['.js', '.json'] 27 | }, 28 | devtool: process.env.LOCAL ? 'source-map' : undefined, 29 | plugins: _.compact([ 30 | new webpack.optimize.ModuleConcatenationPlugin(), 31 | ]), 32 | externals: [ 33 | // We exclude all (but selected) node modules here as the serverless-webpack 34 | // plugin will automatically inject them again later. This way we keep 35 | // development dependencies (i.e. serverless itself) out of our deployment. 36 | NodeExternals({}) 37 | ], 38 | module: { 39 | rules: [ 40 | { 41 | // Process ES6 with Babel. 42 | test: /\.(js|jsx)$/, 43 | use: [ 44 | { 45 | loader: "babel-loader", 46 | options: { 47 | presets: [ "node6", "stage-0" ], 48 | plugins: [] 49 | } 50 | } 51 | ], 52 | } 53 | ] 54 | } 55 | }; 56 | --------------------------------------------------------------------------------