Skip to content

Commit aac6157

Browse files
proAlexandrcreichert
authored andcommitted
Improved configuration of the tool (#30)
* 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
1 parent 8260622 commit aac6157

File tree

12 files changed

+1196
-137
lines changed

12 files changed

+1196
-137
lines changed

.env.example

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ENCRYPTED_HOOK_URL= you can use ENCRYPTED_HOOK_URL if you want
2+
UNENCRYPTED_HOOK_URL=
3+
AWS_FUNCTION_NAME=
4+
AWS_REGION=eu-west-1
5+
AWS_ROLE="arn:aws:iam::123456789123:role/lambda_exec_role"
6+
7+
# You can get AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY here: https://console.aws.amazon.com/iam/home#/users
8+
# Click on user -> Security credentials -> Access keys -> Create access key
9+
AWS_ACCESS_KEY_ID=
10+
AWS_SECRET_ACCESS_KEY=

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
*~
22
.env
3-
deploy.env
3+
tmp/deploy.env
44
npm-debug.log
55

66
build/

.travis.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,4 @@
22
language: node_js
33

44
node_js:
5-
- 0.10
6-
- 0.12
7-
- 4
8-
- 5
95
- 6

Makefile

Lines changed: 0 additions & 53 deletions
This file was deleted.

README.md

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# lambda-cloudwatch-slack
22

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

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

3736
## Configuration
3837

39-
Clone this repository and open the Makefile in your editor, then follow
40-
the steps beow:
38+
### 1. Clone this repository
4139

42-
43-
### 1. Configure AWS environment
44-
45-
Fill in the variables at the top of the `Makefile`. For example, your
46-
variables may look like this:
40+
### 2. Configure environment variables
4741

4842
```
49-
LAMBDA_FUNCTION_NAME=cloudwatch-to-slack
50-
AWS_REGION=us-west-2
51-
AWS_ROLE=arn:aws:iam::123456789123:role/lambda_exec_role
52-
AWS_PROFILE=default
43+
cp .env.example .env
5344
```
5445

55-
### 2. Setup Slack hook
46+
Fill in the variables in the `.env`.
47+
48+
### 3. Setup Slack hook
5649

5750
Follow these steps to configure the webhook in Slack:
5851

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

72-
### 3. Configure AWS Lambda script
73-
74-
Next, open `deploy.env.example`, there are several configuration
75-
options here. At a minimum, you must fill out `UNENCRYPTED_HOOK_URL`
76-
(or `KMS_ENCRYPTED_HOOK_URL`) and `SLACK_CHANNEL` (the name of the Slack room to send messages).
77-
78-
When you're done, copy the file to `deploy.env`:
79-
80-
```
81-
$ cp deploy.env.example deploy.env
82-
```
83-
8465
#### Encrypted the Slack webhook URL
8566

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

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

131-
make deploy
112+
npm install
113+
npm run deploy
132114

133115
## Tests
134116

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

137119
```
138120
npm install
139-
make test
121+
npm test
140122
```
141123

142-
## Caveats
143-
144-
- Environment variables specified in `deploy.env` may not show up on
145-
AWS Lambda but are still in use.
146-
147-
- `node-lambda` appends `-development` to Lambda function names. To
148-
fix this, check out the `.env` file created by `node-lambda` and set
149-
the `AWS_ENVIRONMENT` var to an empty string, like
150-
`AWS_ENVIRONMENT=`
151-
152124
## License
153125

154126
MIT License

config.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
module.exports = {
2-
32
kmsEncryptedHookUrl: process.env.KMS_ENCRYPTED_HOOK_URL, // encrypted slack webhook url
43
unencryptedHookUrl: process.env.UNENCRYPTED_HOOK_URL, // unencrypted slack webhook url
5-
slackChannel: process.env.SLACK_CHANNEL, // slack channel to send a message to
6-
slackUsername: process.env.SLACK_USERNAME, // "AWS SNS via Lamda", // slack username to user for messages
7-
icon_emoji: process.env.ICON_EMOJI, // slack emoji icon to use for messages
8-
orgIcon: process.env.ORG_ICON, // url to icon for your organization for display in the footer of messages
9-
orgName: process.env.ORG_NAME, // name of your organization for display in the footer of messages
104

115
services: {
126
elasticbeanstalk: {
137
// text in the sns message or topicname to match on to process this service type
148
match_text: "ElasticBeanstalkNotifications"
159
},
16-
cloudwatch: {
17-
// text in the sns message or topicname to match on to process this service type
18-
match_text: "CloudWatchNotifications"
10+
cloudwatch: {
1911
},
2012
codepipeline: {
2113
// text in the sns message or topicname to match on to process this service type

deploy.env.example

Lines changed: 0 additions & 7 deletions
This file was deleted.

index.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,7 @@ var config = require('./config');
55
var _ = require('lodash');
66
var hookUrl;
77

8-
var baseSlackMessage = {
9-
channel: config.slackChannel,
10-
username: config.slackUsername,
11-
icon_emoji: config.icon_emoji,
12-
attachments: [
13-
{
14-
"footer": config.orgName,
15-
"footer_icon": config.orgIcon
16-
}
17-
]
18-
}
8+
var baseSlackMessage = {}
199

2010
var postMessage = function(message, callback) {
2111
var body = JSON.stringify(message);
@@ -368,29 +358,36 @@ var processEvent = function(event, context) {
368358
var slackMessage = null;
369359
var eventSubscriptionArn = event.Records[0].EventSubscriptionArn;
370360
var eventSnsSubject = event.Records[0].Sns.Subject || 'no subject';
371-
var eventSnsMessage = event.Records[0].Sns.Message;
361+
var eventSnsMessageRaw = event.Records[0].Sns.Message;
362+
var eventSnsMessage = null;
363+
364+
try {
365+
eventSnsMessage = JSON.parse(eventSnsMessageRaw);
366+
}
367+
catch (e) {
368+
}
372369

373-
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){
370+
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){
374371
console.log("processing codepipeline notification");
375372
slackMessage = handleCodePipeline(event,context)
376373
}
377-
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){
374+
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){
378375
console.log("processing elasticbeanstalk notification");
379376
slackMessage = handleElasticBeanstalk(event,context)
380377
}
381-
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){
378+
else if(eventSnsMessage && 'AlarmName' in eventSnsMessage && 'AlarmDescription' in eventSnsMessage){
382379
console.log("processing cloudwatch notification");
383380
slackMessage = handleCloudWatch(event,context);
384381
}
385-
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){
382+
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){
386383
console.log("processing codedeploy notification");
387384
slackMessage = handleCodeDeploy(event,context);
388385
}
389-
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){
386+
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){
390387
console.log("processing elasticache notification");
391388
slackMessage = handleElasticache(event,context);
392389
}
393-
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){
390+
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){
394391
console.log("processing autoscaling notification");
395392
slackMessage = handleAutoScaling(event, context);
396393
}

0 commit comments

Comments
 (0)