Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gfragoso committed Apr 3, 2018
1 parent b6c83a4 commit 8fae6c0
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
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
```
80 changes: 80 additions & 0 deletions index.js
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;
23 changes: 23 additions & 0 deletions package.json
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"
}
}

0 comments on commit 8fae6c0

Please sign in to comment.