Skip to content

Instantly share code, notes, and snippets.

@justmiles
Created January 26, 2021 15:00
Show Gist options
  • Save justmiles/b8a28f06ba2c4a70332f2bef378eb45c to your computer and use it in GitHub Desktop.
Save justmiles/b8a28f06ba2c4a70332f2bef378eb45c to your computer and use it in GitHub Desktop.
var http = require('https');
let routing_key = process.env.ROUTING_KEY
exports.handler = async (event, context) => {
const response = await new Promise((resolve, reject) => {
const message = event.Records[0].Sns.Message;
console.log('SNS Message:', message);
var payload = JSON.stringify({
"payload": {
"summary": message["detail-type"] || "sns-to-pagerduty",
"severity": "error",
"source": message["source"] || "SNS",
"custom_details": JSON.parse(message)
},
"routing_key": routing_key,
"event_action": "trigger",
"dedup_key": ""
});
// request options
var options = {
host: 'events.pagerduty.com',
port: '443',
path: '/v2/enqueue',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(payload)
}
};
// Set up the request
var req = http.request(options, function(res) {
res.on('data', function(chunk) {
console.log('Response: ' + chunk);
context.succeed();
resolve({
statusCode: 200,
body: JSON.stringify({}, null, 4)
});
});
res.on('error', function(e) {
console.log("Got error: " + e.message);
context.done(null, 'FAILURE');
reject({
statusCode: 500,
body: 'Something went wrong!'
});
});
});
// post the data
req.write(payload);
req.end();
});
return response
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment