├── README.md ├── package.json ├── .gitignore ├── LICENSE └── src └── index.js /README.md: -------------------------------------------------------------------------------- 1 | # serverless-enable-api-logs 2 | Enables Cloudwatch logging for API Gateway events 3 | 4 | # Resources 5 | - [Github](https://github.com/paulSambolin/serverless-enable-api-logs) 6 | - [NPM](https://www.npmjs.com/package/serverless-enable-api-logs) 7 | 8 | # Usage 9 | ```yaml 10 | ... 11 | 12 | plugins: 13 | - serverless-enable-api-logs 14 | 15 | ... 16 | 17 | functions: 18 | get: 19 | handler: index.handler 20 | events: 21 | - http: 22 | path: user/{id} 23 | method: get 24 | ``` 25 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "author": { 3 | "name": "Paul Sambolin" 4 | }, 5 | "description": "Plugin for the SLS 1.x that enables logging for all API Gateway events", 6 | "main": "src/index.js", 7 | "maintainers": [ 8 | { 9 | "email": "paulsambolin@gmail.com", 10 | "name": "Paul Sambolin" 11 | } 12 | ], 13 | "dependencies": { 14 | "class.extend": "0.9.2", 15 | "lodash.merge": "4.6.1" 16 | }, 17 | "license": "MIT", 18 | "repository": { 19 | "git": "https://github.com/paulSambolin/serverless-enable-api-logs" 20 | }, 21 | "name": "serverless-enable-api-logs", 22 | "version": "1.1.0" 23 | } 24 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 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 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const merge = require('lodash.merge'); 4 | const Class = require('class.extend'); 5 | 6 | module.exports = Class.extend({ 7 | 8 | init: function(serverless, opts) { 9 | this._serverless = serverless; 10 | this._opts = opts; 11 | 12 | this.hooks = { 13 | 'before:deploy:deploy': this.addStageVariables.bind(this), 14 | }; 15 | }, 16 | 17 | addStageVariables: function() { 18 | const template = this._serverless.service.provider.compiledCloudFormationTemplate; 19 | 20 | // setup variables, if any are defined 21 | /* 22 | var variables = {}; 23 | if (this._serverless.service.custom.stageVariables) { 24 | variables = this._serverless.service.custom.stageVariables; 25 | } 26 | */ 27 | 28 | // find the correct stage name 29 | const stage = this._serverless.variables.options.stage ? 30 | this._serverless.variables.options.stage : 31 | this._serverless.service.provider.stage; 32 | 33 | // override the deployment config, which can be ignored, see: 34 | // http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-deployment.html 35 | var deploymentConfig = { 36 | StageName: `${stage}na`, 37 | } 38 | 39 | // create a stage resource, which sets the stage and variables correctly 40 | var stageConfig = { 41 | Type: "AWS::ApiGateway::Stage", 42 | Properties: { 43 | StageName: stage, 44 | Description: stage, 45 | RestApiId: {"Ref": "ApiGatewayRestApi"}, 46 | DeploymentId: null, 47 | MethodSettings: [{ 48 | DataTraceEnabled: true, 49 | HttpMethod: "*", 50 | LoggingLevel: "INFO", 51 | ResourcePath: "/*", 52 | MetricsEnabled: true 53 | }] 54 | } 55 | } 56 | 57 | // find the deployment resource, and add the stage resource 58 | Object.keys(template.Resources).forEach(function(key){ 59 | if (template.Resources[key]['Type'] == 'AWS::ApiGateway::Deployment') { 60 | delete template.Resources[key].Properties.StageName; 61 | 62 | //template.Resources[key]['DependsOn'] = 'ApiGatewayStage'; 63 | // add stage config 64 | stageConfig.Properties.DeploymentId = {"Ref":key} 65 | if (template.Resources.ApiGatewayStage) { 66 | merge(template.Resources.ApiGatewayStage, stageConfig); 67 | } else { 68 | template.Resources.ApiGatewayStage = stageConfig; 69 | } 70 | } 71 | 72 | // we need to make all api keys dependend on the stage, not the deployment 73 | if (template.Resources[key]['Type'] == 'AWS::ApiGateway::ApiKey') { 74 | template.Resources[key]['DependsOn'] = 'ApiGatewayStage'; 75 | } 76 | }) 77 | 78 | 79 | this._serverless.cli.log('Enabled logging for ApiGateway Stage'); 80 | }, 81 | }); 82 | --------------------------------------------------------------------------------