-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,25 @@ | ||
# serverless-tag-cloud-watch-logs | ||
Serverless plugin to tag CloudWatchLogs | ||
|
||
## Installation | ||
|
||
Install the plugin via <a href="https://docs.npmjs.com/cli/install">NPM</a> | ||
|
||
``` | ||
npm install --save-dev serverless-tag-cloud-watch-logs | ||
``` | ||
|
||
## Usage | ||
|
||
In Serverless template: | ||
|
||
``` | ||
custom: | ||
cloudWatchLogsTags: | ||
TagName1: TagValue1 | ||
TagName2: TagValue2 | ||
plugins: | ||
- serverless-tag-cloud-watch-logs | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
|
||
let _cloudWatchLogsService = null; | ||
let _cloudFormationService = null; | ||
|
||
class ServerlessCloudWatchLogsTagPlugin { | ||
|
||
get stackName() { | ||
return `${this.serverless.service.service}-${this.options.stage}`; | ||
} | ||
|
||
get logGroupService() { | ||
|
||
if (!_cloudWatchLogsService) | ||
_cloudWatchLogsService = new this.awsService.sdk.CloudWatchLogs({ region: this.options.region }); | ||
|
||
return _cloudWatchLogsService; | ||
} | ||
|
||
get cloudWatchLogsService() { | ||
|
||
if (!_cloudFormationService) | ||
_cloudFormationService = new this.awsService.sdk.CloudFormation({ region: this.options.region }); | ||
|
||
return _cloudFormationService; | ||
} | ||
|
||
constructor(serverless, options) { | ||
|
||
this.options = options; | ||
this.serverless = serverless; | ||
this.awsService = this.serverless.getProvider('aws'); | ||
|
||
this.hooks = { | ||
'after:deploy:deploy': this.execute.bind(this), | ||
}; | ||
} | ||
|
||
execute() { | ||
return this.getStackResources() | ||
.then(data => this.tagCloudWatchLogs(data)) | ||
.then(data => this.serverless.cli.log(JSON.stringify(data))) | ||
.catch(err => this.serverless.cli.log(JSON.stringify(err))); | ||
} | ||
|
||
getStackResources() { | ||
return new Promise((resolve, reject) => { | ||
this.cloudWatchLogsService.describeStackResources({ StackName: this.stackName }, (err, data) => { | ||
if (err) return reject(err); | ||
resolve(data); | ||
}); | ||
}); | ||
} | ||
|
||
tagCloudWatchLogs(data) { | ||
|
||
const cloudWatchResources = _.filter(data.StackResources, { ResourceType: 'AWS::Logs::LogGroup' }); | ||
|
||
const promises = _.map(cloudWatchResources, item => { | ||
return new Promise((resolve, reject) => { | ||
|
||
const params = { | ||
logGroupName: item.PhysicalResourceId, | ||
tags: this.serverless.service.custom.cloudWatchLogsTags | ||
}; | ||
|
||
this.logGroupService.tagLogGroup(params, (err, apiData) => { | ||
if (err) return reject(err); | ||
resolve(`Tagged LogGroup ${item.LogicalResourceId}`); | ||
}); | ||
}); | ||
}); | ||
|
||
return Promise.all(promises); | ||
} | ||
} | ||
|
||
module.exports = ServerlessCloudWatchLogsTagPlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "serverless-tag-cloud-watch-logs", | ||
"version": "1.0.0", | ||
"description": "Serverless plugin to tag CloudWatchLogs", | ||
"main": "index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/gfragoso/serverless-tag-cloud-watch-logs" | ||
}, | ||
"keywords": [ | ||
"serverless", | ||
"cloudWatchLogs", | ||
"cloudWatch", | ||
"plugin", | ||
"tag", | ||
"aws" | ||
], | ||
"author": "Gustavo Fragoso", | ||
"license": "MIT", | ||
"dependencies": { | ||
"lodash": "^4.17.5" | ||
} | ||
} |