Skip to content

Commit

Permalink
Improved configuration of the tool (#30)
Browse files Browse the repository at this point in the history
* Customization improvements

    1) Fill variables in .env instead deploy.env and Makefile
    2) Autodetecting CloudWatch events.
    3) Remove slackbot customization

* Use npm scripts instead of makefile
* Fix image urls in readme
* Changed env variables examples and fixed names in scripts
* Remove old node versions from TravisCI
  • Loading branch information
proAlexandr authored and creichert committed Oct 12, 2018
1 parent 8260622 commit aac6157
Show file tree
Hide file tree
Showing 12 changed files with 1,196 additions and 137 deletions.
10 changes: 10 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ENCRYPTED_HOOK_URL= you can use ENCRYPTED_HOOK_URL if you want
UNENCRYPTED_HOOK_URL=
AWS_FUNCTION_NAME=
AWS_REGION=eu-west-1
AWS_ROLE="arn:aws:iam::123456789123:role/lambda_exec_role"

# You can get AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY here: https://console.aws.amazon.com/iam/home#/users
# Click on user -> Security credentials -> Access keys -> Create access key
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
*~
.env
deploy.env
tmp/deploy.env
npm-debug.log

build/
Expand Down
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@
language: node_js

node_js:
- 0.10
- 0.12
- 4
- 5
- 6
53 changes: 0 additions & 53 deletions Makefile

This file was deleted.

50 changes: 11 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# lambda-cloudwatch-slack

An [AWS Lambda](http://aws.amazon.com/lambda/) function for better
Slack
notifications. [Check out the blog post](https://assertible.com/blog/npm-package-lambda-cloudwatch-slack).
An [AWS Lambda](http://aws.amazon.com/lambda/) function for better Slack notifications.
[Check out the blog post](https://assertible.com/blog/npm-package-lambda-cloudwatch-slack).

[![BuildStatus](https://travis-ci.org/assertible/lambda-cloudwatch-slack.png?branch=master)](https://travis-ci.org/assertible/lambda-cloudwatch-slack)
[![NPM version](https://badge.fury.io/js/lambda-cloudwatch-slack.png)](http://badge.fury.io/js/lambda-cloudwatch-slack)
Expand Down Expand Up @@ -36,23 +35,17 @@ ways:

## Configuration

Clone this repository and open the Makefile in your editor, then follow
the steps beow:
### 1. Clone this repository


### 1. Configure AWS environment

Fill in the variables at the top of the `Makefile`. For example, your
variables may look like this:
### 2. Configure environment variables

```
LAMBDA_FUNCTION_NAME=cloudwatch-to-slack
AWS_REGION=us-west-2
AWS_ROLE=arn:aws:iam::123456789123:role/lambda_exec_role
AWS_PROFILE=default
cp .env.example .env
```

### 2. Setup Slack hook
Fill in the variables in the `.env`.

### 3. Setup Slack hook

Follow these steps to configure the webhook in Slack:

Expand All @@ -69,18 +62,6 @@ Follow these steps to configure the webhook in Slack:
5. Click 'Save Settings' at the bottom of the Slack integration
page.

### 3. Configure AWS Lambda script

Next, open `deploy.env.example`, there are several configuration
options here. At a minimum, you must fill out `UNENCRYPTED_HOOK_URL`
(or `KMS_ENCRYPTED_HOOK_URL`) and `SLACK_CHANNEL` (the name of the Slack room to send messages).

When you're done, copy the file to `deploy.env`:

```
$ cp deploy.env.example deploy.env
```

#### Encrypted the Slack webhook URL

If you don't want or need to encrypt your hook URL, you can use the
Expand Down Expand Up @@ -128,27 +109,18 @@ encrypt your Slack hook URL for use in this function:

The final step is to deploy the integration to AWS Lambda:

make deploy
npm install
npm run deploy

## Tests

With the variables filled in, you can test the function:

```
npm install
make test
npm test
```

## Caveats

- Environment variables specified in `deploy.env` may not show up on
AWS Lambda but are still in use.

- `node-lambda` appends `-development` to Lambda function names. To
fix this, check out the `.env` file created by `node-lambda` and set
the `AWS_ENVIRONMENT` var to an empty string, like
`AWS_ENVIRONMENT=`

## License

MIT License
10 changes: 1 addition & 9 deletions config.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
module.exports = {

kmsEncryptedHookUrl: process.env.KMS_ENCRYPTED_HOOK_URL, // encrypted slack webhook url
unencryptedHookUrl: process.env.UNENCRYPTED_HOOK_URL, // unencrypted slack webhook url
slackChannel: process.env.SLACK_CHANNEL, // slack channel to send a message to
slackUsername: process.env.SLACK_USERNAME, // "AWS SNS via Lamda", // slack username to user for messages
icon_emoji: process.env.ICON_EMOJI, // slack emoji icon to use for messages
orgIcon: process.env.ORG_ICON, // url to icon for your organization for display in the footer of messages
orgName: process.env.ORG_NAME, // name of your organization for display in the footer of messages

services: {
elasticbeanstalk: {
// text in the sns message or topicname to match on to process this service type
match_text: "ElasticBeanstalkNotifications"
},
cloudwatch: {
// text in the sns message or topicname to match on to process this service type
match_text: "CloudWatchNotifications"
cloudwatch: {
},
codepipeline: {
// text in the sns message or topicname to match on to process this service type
Expand Down
7 changes: 0 additions & 7 deletions deploy.env.example

This file was deleted.

33 changes: 15 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,7 @@ var config = require('./config');
var _ = require('lodash');
var hookUrl;

var baseSlackMessage = {
channel: config.slackChannel,
username: config.slackUsername,
icon_emoji: config.icon_emoji,
attachments: [
{
"footer": config.orgName,
"footer_icon": config.orgIcon
}
]
}
var baseSlackMessage = {}

var postMessage = function(message, callback) {
var body = JSON.stringify(message);
Expand Down Expand Up @@ -368,29 +358,36 @@ var processEvent = function(event, context) {
var slackMessage = null;
var eventSubscriptionArn = event.Records[0].EventSubscriptionArn;
var eventSnsSubject = event.Records[0].Sns.Subject || 'no subject';
var eventSnsMessage = event.Records[0].Sns.Message;
var eventSnsMessageRaw = event.Records[0].Sns.Message;
var eventSnsMessage = null;

try {
eventSnsMessage = JSON.parse(eventSnsMessageRaw);
}
catch (e) {
}

if(eventSubscriptionArn.indexOf(config.services.codepipeline.match_text) > -1 || eventSnsSubject.indexOf(config.services.codepipeline.match_text) > -1 || eventSnsMessage.indexOf(config.services.codepipeline.match_text) > -1){
if(eventSubscriptionArn.indexOf(config.services.codepipeline.match_text) > -1 || eventSnsSubject.indexOf(config.services.codepipeline.match_text) > -1 || eventSnsMessageRaw.indexOf(config.services.codepipeline.match_text) > -1){
console.log("processing codepipeline notification");
slackMessage = handleCodePipeline(event,context)
}
else if(eventSubscriptionArn.indexOf(config.services.elasticbeanstalk.match_text) > -1 || eventSnsSubject.indexOf(config.services.elasticbeanstalk.match_text) > -1 || eventSnsMessage.indexOf(config.services.elasticbeanstalk.match_text) > -1){
else if(eventSubscriptionArn.indexOf(config.services.elasticbeanstalk.match_text) > -1 || eventSnsSubject.indexOf(config.services.elasticbeanstalk.match_text) > -1 || eventSnsMessageRaw.indexOf(config.services.elasticbeanstalk.match_text) > -1){
console.log("processing elasticbeanstalk notification");
slackMessage = handleElasticBeanstalk(event,context)
}
else if(eventSubscriptionArn.indexOf(config.services.cloudwatch.match_text) > -1 || eventSnsSubject.indexOf(config.services.cloudwatch.match_text) > -1 || eventSnsMessage.indexOf(config.services.cloudwatch.match_text) > -1){
else if(eventSnsMessage && 'AlarmName' in eventSnsMessage && 'AlarmDescription' in eventSnsMessage){
console.log("processing cloudwatch notification");
slackMessage = handleCloudWatch(event,context);
}
else if(eventSubscriptionArn.indexOf(config.services.codedeploy.match_text) > -1 || eventSnsSubject.indexOf(config.services.codedeploy.match_text) > -1 || eventSnsMessage.indexOf(config.services.codedeploy.match_text) > -1){
else if(eventSubscriptionArn.indexOf(config.services.codedeploy.match_text) > -1 || eventSnsSubject.indexOf(config.services.codedeploy.match_text) > -1 || eventSnsMessageRaw.indexOf(config.services.codedeploy.match_text) > -1){
console.log("processing codedeploy notification");
slackMessage = handleCodeDeploy(event,context);
}
else if(eventSubscriptionArn.indexOf(config.services.elasticache.match_text) > -1 || eventSnsSubject.indexOf(config.services.elasticache.match_text) > -1 || eventSnsMessage.indexOf(config.services.elasticache.match_text) > -1){
else if(eventSubscriptionArn.indexOf(config.services.elasticache.match_text) > -1 || eventSnsSubject.indexOf(config.services.elasticache.match_text) > -1 || eventSnsMessageRaw.indexOf(config.services.elasticache.match_text) > -1){
console.log("processing elasticache notification");
slackMessage = handleElasticache(event,context);
}
else if(eventSubscriptionArn.indexOf(config.services.autoscaling.match_text) > -1 || eventSnsSubject.indexOf(config.services.autoscaling.match_text) > -1 || eventSnsMessage.indexOf(config.services.autoscaling.match_text) > -1){
else if(eventSubscriptionArn.indexOf(config.services.autoscaling.match_text) > -1 || eventSnsSubject.indexOf(config.services.autoscaling.match_text) > -1 || eventSnsMessageRaw.indexOf(config.services.autoscaling.match_text) > -1){
console.log("processing autoscaling notification");
slackMessage = handleAutoScaling(event, context);
}
Expand Down
Loading

0 comments on commit aac6157

Please sign in to comment.