Skip to content

Instantly share code, notes, and snippets.

@harishletsgo
Forked from sammarks/lambda.js
Created August 18, 2020 02:53
Show Gist options
  • Save harishletsgo/08c0ccb9d4f3ec18d66de12d9c0afbb8 to your computer and use it in GitHub Desktop.
Save harishletsgo/08c0ccb9d4f3ec18d66de12d9c0afbb8 to your computer and use it in GitHub Desktop.
Slack notifications from AWS Amplify
const https = require('https')
exports.handler = async (event) => {
const sns = event.Records[0].Sns.Message
let color = ''
let message = ''
if (sns.includes('build status is FAILED')) {
color = '#E52E59'
message = 'Release to My Company **production** failed.'
} else if (sns.includes('build status is SUCCEED')) {
color = '#21E27C'
message = 'Release to My Company **production** succeeded.'
} else if (sns.includes(`build status is STARTED`)) {
color = '#3788DD'
message = 'Release to My Company **production** has started...'
}
const data = JSON.stringify({
attachments: [
{
'mrkdwn_in': ['text'],
fallback: message,
color,
text: message
}
]
})
return new Promise((resolve, reject) => {
const request = https.request(process.env.WEBHOOK_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length,
}
}, (res) => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', (d) => process.stdout.write(d))
res.on('end', () => resolve())
})
request.write(data)
request.end()
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment