Skip to content

Instantly share code, notes, and snippets.

@PrashantBhatasana
Last active April 17, 2020 09:39
Show Gist options
  • Save PrashantBhatasana/338566ca800a9e2850fc005ed753025c to your computer and use it in GitHub Desktop.
Save PrashantBhatasana/338566ca800a9e2850fc005ed753025c to your computer and use it in GitHub Desktop.
This is the lambda function that send slack notification.
const AWS = require('aws-sdk');
var url = require('url');
var https = require('https');
var util = require('util');
const p = require('phin');
const reqURL = `https://hooks.slack.com/services/TBCSBJ5/50117HG5HFV/hnfnDN78KJNH56RFVds`;
async function notifySlack() {
const message = {
'channel': '<Notification Slack channel>',
'username': 'Build Server',
'text': 'Jenkins: Open Jenkins Build server here',
'icon_emoji': ':aws:',
'attachments': [{
'color': '#8697db',
'fields': [
{
'title': 'EC2 server',
'value': 'EC2 server Starting...',
'short': true
}
]
}]
};
return p({
url: reqURL,
method: 'POST',
data: message
});
}
exports.handler = async (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
const ec2 = new AWS.EC2({ region: event.instanceRegion });
ec2.startInstances({ InstanceIds: [event.instanceId] }).promise()
.then(() => callback(null, `Successfully started ${event.instanceId}`))
.catch(err => callback(err));
const req = await notifySlack();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment