Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ The final step is to deploy the integration to AWS Lambda:
npm install
npm run deploy

## Programmatic usage

If you want to programmatically pass in a configuration, rather than relying on
environment variables, you can create your own handler:

```js
const { createHandler, config } = require('lambda-cloudwatch-slack');

exports.handler = createHandler({
...config,
unencryptedHookUrl: 'https://hooks.slack.com/services/...'
})
```

This can for example be useful if you need to get the webhook url from
AWS Parameter Store or Secrets Manager at runtime.

## Tests

With the variables filled in, you can test the function:
Expand Down
54 changes: 30 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,27 +410,33 @@ var processEvent = function(event, context) {
});
};

exports.handler = function(event, context) {
if (hookUrl) {
processEvent(event, context);
} else if (config.unencryptedHookUrl) {
hookUrl = config.unencryptedHookUrl;
processEvent(event, context);
} else if (config.kmsEncryptedHookUrl && config.kmsEncryptedHookUrl !== '<kmsEncryptedHookUrl>') {
var encryptedBuf = new Buffer(config.kmsEncryptedHookUrl, 'base64');
var cipherText = { CiphertextBlob: encryptedBuf };
var kms = new AWS.KMS();

kms.decrypt(cipherText, function(err, data) {
if (err) {
console.log("decrypt error: " + err);
processEvent(event, context);
} else {
hookUrl = "https://" + data.Plaintext.toString('ascii');
processEvent(event, context);
}
});
} else {
context.fail('hook url has not been set.');
}
};
exports.config = config

exports.createHandler = function (config) {
return function (event, context) {
if (hookUrl) {
processEvent(event, context);
} else if (config.unencryptedHookUrl) {
hookUrl = config.unencryptedHookUrl;
processEvent(event, context);
} else if (config.kmsEncryptedHookUrl && config.kmsEncryptedHookUrl !== '<kmsEncryptedHookUrl>') {
var encryptedBuf = new Buffer(config.kmsEncryptedHookUrl, 'base64');
var cipherText = { CiphertextBlob: encryptedBuf };
var kms = new AWS.KMS();

kms.decrypt(cipherText, function (err, data) {
if (err) {
console.log("decrypt error: " + err);
processEvent(event, context);
} else {
hookUrl = "https://" + data.Plaintext.toString('ascii');
processEvent(event, context);
}
});
} else {
context.fail('hook url has not been set.');
}
};
}

exports.handler = exports.createHandler(config)