Skip to content

Instantly share code, notes, and snippets.

@sammarks
Created May 7, 2020 00:04
Show Gist options
  • Save sammarks/95a8da4ce7f7c7d21b1d3e1d648b319d to your computer and use it in GitHub Desktop.
Save sammarks/95a8da4ce7f7c7d21b1d3e1d648b319d 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