├── env.yml ├── .gitignore ├── echo └── index.js ├── .vscode ├── settings.json ├── tasks.json └── launch.json ├── test └── echo_spec.js ├── README.md ├── package.json ├── serverless.yml └── auth └── index.js /env.yml: -------------------------------------------------------------------------------- 1 | dev: 2 | stage: "dev" 3 | 4 | test: 5 | stage: "test" 6 | 7 | prod: 8 | stage: "prod" -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | .idea 4 | .serverless 5 | typings 6 | npm-debug.log 7 | bitbucket-pipelines.yml -------------------------------------------------------------------------------- /echo/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const echo = function (event, context, callback) { 4 | console.log(event, context); 5 | return callback(null, { 6 | statusCode: 200, 7 | body: JSON.stringify(event.body), 8 | headers: { 9 | 'Access-Control-Allow-Origin': '*' 10 | } 11 | }); 12 | }; 13 | 14 | module.exports.echo = echo; -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | // Place your settings in this file to overwrite default and user settings. 2 | { 3 | // Controls the rendering size of tabs in characters. 4 | // If set to auto, the value will be guessed based on the opened file. 5 | "editor.tabSize": 2, 6 | // Controls if the editor will insert spaces for tabs. 7 | // If set to auto, the value will be guessed based on the opened file. 8 | "editor.insertSpaces": false, 9 | "editor.detectIndentation": false 10 | } -------------------------------------------------------------------------------- /test/echo_spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('chai').assert, 4 | echoModule = require('../echo/index'); 5 | 6 | describe('echo handler', () => { 7 | it('should echo stuff ', (done) => { 8 | const input = { "bold": "statement" }; 9 | 10 | echoModule.echo({ body: input }, {}, (error, data) => { 11 | assert.isNull(error, `An error was returned ${error}`); 12 | assert.isDefined(data, 'Data is missing'); 13 | assert.equal(data.body, JSON.stringify(input), 'Did not return what was sent in.'); 14 | done(); 15 | }) 16 | 17 | }); 18 | }); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Local lambda debugging 2 | **Visual Studio Code (OSX, Linux, Windows) is recommended as IDE** 3 | 4 | ## Prerequisties 5 | * Check if nodejs is installed using **node -v**. Version should be **6.10.3 LTS**. 6 | 7 | ## Project Setup 8 | * npm install 9 | 10 | ## Tests 11 | * npm test 12 | 13 | ## Run/Debug (vscode) 14 | * Click on Debug, chose Debug from the drop down and click Play. After this setup you can simply hit F5 to run this. 15 | 16 | ### More info: 17 | * https://github.com/serverless/serverless 18 | * https://github.com/dherault/serverless-offline 19 | -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | // See https://go.microsoft.com/fwlink/?LinkId=733558 3 | // for the documentation about the tasks.json format 4 | "version": "0.1.0", 5 | "command": "npm", 6 | "isShellCommand": true, 7 | "showOutput": "always", 8 | "suppressTaskName": true, 9 | "tasks": [ 10 | { 11 | "taskName": "install", 12 | "args": [ 13 | "install" 14 | ] 15 | }, 16 | { 17 | "taskName": "update", 18 | "args": [ 19 | "update" 20 | ] 21 | }, 22 | { 23 | "taskName": "test", 24 | "args": [ 25 | "run", 26 | "test" 27 | ] 28 | } 29 | ] 30 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "onemuppet-local-lambda-debug", 3 | "version": "0.0.1", 4 | "description": "Example setup for vscode for debugging AWS lambda functions locally.", 5 | "author": "me", 6 | "license": "MIT", 7 | "private": false, 8 | "repository": { 9 | "type": "git", 10 | "url": "git://github.com/" 11 | }, 12 | "scripts": { 13 | "start": "./node_modules/.bin/serverless offline -s dev", 14 | "debug": "export SLS_DEBUG=* && node --debug ./node_modules/.bin/serverless offline -s dev", 15 | "test": "mocha" 16 | }, 17 | "dependencies": {}, 18 | "devDependencies": { 19 | "chai": "3.5.0", 20 | "mocha": "3.1.2", 21 | "serverless": "1.12.1", 22 | "serverless-offline": "^3.13.5", 23 | "serverless-plugin-browserify": "1.1.2" 24 | } 25 | } -------------------------------------------------------------------------------- /serverless.yml: -------------------------------------------------------------------------------- 1 | service: onemuppet-local-lambda-debug 2 | 3 | package: 4 | individually: true 5 | 6 | plugins: 7 | - serverless-plugin-browserify 8 | - serverless-offline 9 | 10 | provider: 11 | name: aws 12 | runtime: nodejs6.10 13 | profile: onemuppet-local-lambda-debug 14 | environment: ${file(env.yml):${self:custom.stage}} 15 | region: eu-west-1 16 | cfLogs: true 17 | stage: dev 18 | apiKeys: 19 | - onemuppet-local-lambda-debug-${opt:stage} 20 | 21 | custom: 22 | stage: "${opt:stage, self:provider.stage}" 23 | 24 | functions: 25 | auth: 26 | handler: auth/index.authorize 27 | echo: 28 | handler: echo/index.echo 29 | memorySize: 1024 30 | timeout: 5 31 | events: 32 | - http: 33 | path: echo 34 | method: post 35 | cors: true 36 | authorizer: auth -------------------------------------------------------------------------------- /auth/index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | var generatePolicy = function (principalId, effect) { 4 | return { 5 | principalId: principalId, 6 | policyDocument: { 7 | Version: '2012-10-17', 8 | Statement: [{ 9 | Action: '*', 10 | Effect: effect, 11 | Resource: '*' 12 | }] 13 | } 14 | }; 15 | }; 16 | 17 | const mockUser = { 18 | userId: "onemuppet", 19 | name: "David Borgenvik" 20 | } 21 | 22 | const authorize = function (event, context, callback) { 23 | console.log(event, context); 24 | 25 | if (event.authorizationToken === 'magic-token') { 26 | const allowPolicy = generatePolicy(JSON.stringify(mockUser), 'Allow'); 27 | callback(null, allowPolicy); 28 | } else { 29 | const denyPolicy = generatePolicy(JSON.stringify(mockUser), 'Deny'); 30 | callback(null, denyPolicy) 31 | } 32 | }; 33 | 34 | module.exports.generatePolicy = generatePolicy; 35 | module.exports.authorize = authorize; -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible Node.js debug attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": "Debug", 9 | "type": "node", 10 | "request": "launch", 11 | "cwd": "${workspaceRoot}", 12 | "runtimeExecutable": "npm", 13 | "runtimeArgs": [ 14 | "run-script", 15 | "debug" 16 | ], 17 | "port": 5858 18 | }, 19 | { 20 | "type": "node", 21 | "request": "launch", 22 | "name": "Mocha Tests", 23 | "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha", 24 | "args": [ 25 | "-u", 26 | "tdd", 27 | "--timeout", 28 | "999999", 29 | "--colors", 30 | "${workspaceRoot}/test" 31 | ], 32 | "internalConsoleOptions": "openOnSessionStart" 33 | } 34 | ] 35 | } --------------------------------------------------------------------------------