Last active
April 17, 2020 09:39
-
-
Save PrashantBhatasana/338566ca800a9e2850fc005ed753025c to your computer and use it in GitHub Desktop.
This is the lambda function that send slack notification.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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