Created
March 31, 2022 09:47
-
-
Save MrCoro/dcdc4b9e36da8a14c2c033e8ef1bb5e0 to your computer and use it in GitHub Desktop.
This file contains 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 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