Skip to content

Instantly share code, notes, and snippets.

@harlow
Last active April 27, 2018 20:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harlow/d82a4baf943153998601 to your computer and use it in GitHub Desktop.
Save harlow/d82a4baf943153998601 to your computer and use it in GitHub Desktop.
Lambda Functions
console.log('Loading function');
exports.handler = function(event, context) {
//console.log('Received event:', JSON.stringify(event, null, 2));
event.Records.forEach(function(record) {
// Kinesis data is base64 encoded so decode here
var payload = new Buffer(record.kinesis.data, 'base64').toString('ascii');
console.log('Decoded payload:', payload);
});
context.succeed("Successfully processed " + event.Records.length + " records.");
};
/*
This function handles a Slack slash command and echoes the details back to the user.
Follow these steps to configure the slash command in Slack:
1. Navigate to https://<your-team-domain>.slack.com/services/new
2. Search for and select "Slash Commands".
3. Enter a name for your command and click "Add Slash Command Integration".
4. Copy the token string from the integration settings and use it in the next section.
5. After you complete this blueprint, enter the provided API endpoint URL in the URL field.
Follow these steps to encrypt your Slack token for use in this function:
1. Create a KMS key - http://docs.aws.amazon.com/kms/latest/developerguide/create-keys.html.
2. Encrypt the token using the AWS CLI.
$ aws kms encrypt --key-id alias/<KMS key name> --plaintext "<COMMAND_TOKEN>"
3. Copy the base-64 encoded, encrypted key (CiphertextBlob) to the kmsEncyptedToken variable.
4. Give your function's role permission for the kms:Decrypt action.
Example:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"kms:Decrypt"
],
"Resource": [
"<your KMS key ARN>"
]
}
]
}
Follow these steps to complete the configuration of your command API endpoint
1. When completing the blueprint configuration select "POST" for method and
"Open" for security on the Endpoint Configuration page.
2. After completing the function creation, open the newly created API in the
API Gateway console.
3. Add a mapping template for the x-www-form-urlencoded content type with the
following body: { "body": $input.json("$") }
4. Deploy the API to the prod stage.
5. Update the URL for your Slack slash command with the invocation URL for the
created API resource in the prod stage.
*/
var AWS = require('aws-sdk');
var qs = require('querystring');
var token, kmsEncyptedToken;
kmsEncyptedToken = "<kmsEncryptedToken>";
exports.handler = function (event, context) {
if (token) {
// Container reuse, simply process the event with the key in memory
processEvent(event, context);
} else if (kmsEncyptedToken && kmsEncyptedToken !== "<kmsEncryptedToken>") {
var encryptedBuf = new Buffer(kmsEncyptedToken, 'base64');
var cipherText = {CiphertextBlob: encryptedBuf};
var kms = new AWS.KMS();
kms.decrypt(cipherText, function (err, data) {
if (err) {
console.log("Decrypt error: " + err);
context.fail(err);
} else {
token = data.Plaintext.toString('ascii');
processEvent(event, context);
}
});
} else {
context.fail("Token has not been set.");
}
};
var processEvent = function(event, context) {
var body = event.body;
var params = qs.parse(body);
var requestToken = params.token;
if (requestToken !== token) {
console.error("Request token (" + requestToken + ") does not match exptected");
context.fail("Invalid request token");
}
var user = params.user_name;
var command = params.command;
var channel = params.channel_name;
var commandText = params.text;
context.succeed(user + " invoked " + command + " in " + channel + " with the following text: " + commandText);
};
var request = require('request');
var util = require('util');
var slackWebhookUrl = '<YOUR_SLACK_WEBHOOK_API_URL>';
exports.handler = function(event, context) {
var data = convertModulusWebhook(event);
var options = {
method: 'post',
body: data,
json: true,
url: slackWebhookUrl
};
request(options, context.done);
};
/* convert a Modulus webhook into a slack webhook
*/
var convertModulusWebhook = function(webhook) {
var data = webhook;
var text = util.format('%s - %s', data.type, data.project.name);
var resp = {};
resp.attachments = [];
var info = {
'fallback': text,
'pretext': text,
'color': getStatusColor(data.type),
'fields': []
};
info.fields.push(buildCrashAttachment(data));
resp.attachments.push(info);
return resp;
};
var buildCrashAttachment = function(data) {
if (data.type === 'crash' && data.servo) {
return {
'title': 'Modulus Alert',
'text': data.servo.log,
'short': false
};
} else {
return {
'title': 'Modulus Alert',
'text': data.project.name,
'short': false
};
}
};
/* returns a Slack color depending on the modulus event type. Crash should be red, deploy should be
* green, and everything else will be orange.
*/
var getStatusColor = function(eventType) {
if (eventType === 'crash') {
return 'danger';
} else if (eventType === 'deploy') {
return 'good';
} else {
return 'warning';
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment