Skip to content

Instantly share code, notes, and snippets.

@MrCoro
Created March 31, 2022 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrCoro/dcdc4b9e36da8a14c2c033e8ef1bb5e0 to your computer and use it in GitHub Desktop.
Save MrCoro/dcdc4b9e36da8a14c2c033e8ef1bb5e0 to your computer and use it in GitHub Desktop.
const p = require('phin');
// input your slack webhook
const reqURL = `https://hooks.slack.com/services/xxxxxxxxxxxxxxxxxxxxxxxxxxx`;
async function notifySlack(instanceInfo) {
const message = {
'channel': 'aws-alarm',
'username': 'AWS_State_Reporting_Bot',
'text': 'EC2 State is changing',
'attachments': [{
'color': '#8697db',
'fields': [
{
'title': 'EC2',
'value': `EC2 Instance ID : ${instanceInfo.instanceId}\nState : ${instanceInfo.state}\nTime : ${instanceInfo.time}\nRegion : ${instanceInfo.region}`,
'short': false
}
]
}]
};
return p({
url: reqURL,
method: 'POST',
data: message
});
}
exports.handler = async (event, context, callback) => {
console.log(event);
const instanceInfo = {
instanceId: event.detail["instance-id"],
state: event.detail.state,
time: event.time,
region: event.region,
detailType: event["detail-type"]
}
console.log(instanceInfo);
const req = await notifySlack(instanceInfo);
const response = {
statusCode: 200,
body: JSON.stringify(instanceInfo),
};
return response;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment