Interactions via Lambda
By setting AWS::Lambda as a trigger for SQS receiveMessage, you can send the contents of the message
to where ever you want. In my case, it was two places
Pagerduty
CAVEAT: Sending lots of messages as incidents to PagerDuty costs money!
The way shown sends an incident to the PagerDuty service that you have to previously setup
to receive events in the form of PagerDuty Events API v2. And the structure of the message to be sent
also needs to be exactly that, with blanks to be filled in for the ID of the service created, which is
usually a six character uppercase string.
The Authorisation token is available in the settings section of the service in PagerDuty.
So requiring the https module to send messagess
var https = require('https');
Inside the main function, where the actual call happens and is pre-defined for us
we first parse the contents of the message which triggered the function.
exports.handler = (event, context, callback) => {
let messageBody = JSON.parse(event.Records[0].body);
Then we define the payload that we want to send.
var incident = {
"incident": {
"type": "incident",
"title": "SQS incident",
"service": {
"id": ID_OF_PAGERDUTY_SERVICE,
"type": "service_reference"
},
"body": {
"type": "incident_body",
"details": messageBody,
}
}
}
The options to hit the PagerDuty v2 incident API
const options = {
host: "api.pagerduty.com",
family: 4,
path: "/incidents",
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Token token=TOKEN_OF_SERVICE",
"Accept": "application/vnd.pagerduty+json;version=2",
"From": EMAIL_ASSOCIATED_WITH_ACCOUNT,
}
};
Making the request
const req = https.request(options, (res) => {
console.log(res.statusCode);
console.log("Success");
});
req.on('error', (e) => {
console.log(e.message);
});
req.write(JSON.stringify(incident));
req.end();
callback(null, 'Lambda execution completed');
Slack
We still parse the message and make the request the same way, we just have to redefine the payload and the
options to make the request. However, a pre-requisite is a incoming webhook pre-defined in Slack
let slackMessage = {
text: "Something important" // This field is requisite
};
const slackOptions = {
host: "hooks.slack.com",
family: 4,
path: PATH_OF_WEBHOOK,
method: "POST",
headers: {
"Content-Type": "application/json"
}
};
The rest is roughly the same.