Skip to content

Instantly share code, notes, and snippets.

@tranvansang
Created July 28, 2020 05:11
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 tranvansang/d155801d0635f5546182e9f659d8216a to your computer and use it in GitHub Desktop.
Save tranvansang/d155801d0635f5546182e9f659d8216a to your computer and use it in GitHub Desktop.
Send slack webhook from AWS Lambda on AWS SNS notification (single script without library)
// NodeJS v12
const https = require('https')
const send = data => {
const options = {
hostname: 'hooks.slack.com',
port: 443,
path: '/services/<webhook-id>',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
}
return new Promise((resolve, reject) => {
const req = https.request(options, res => {
console.log('code = ', res.statusCode)
console.log('headers = ', res.headers)
res.on('data', console.log.bind('[data]'))
res.on('end', resolve)
res.on('error', reject)
})
req.on('error', reject)
req.write(data)
req.end()
})
}
exports.handler = async event => {
const text = event.Records.map(record => record.Sns.Message).join('\n')
await send(JSON.stringify({text}))
return {
statusCode: 200,
body: JSON.stringify(`Sent message ${text}`)
}
}