Skip to content

Instantly share code, notes, and snippets.

@Xanaxiel
Forked from m2wasabi/README.md
Created October 20, 2021 06:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xanaxiel/38e23794223ef9cddf772562f5a24a70 to your computer and use it in GitHub Desktop.
Save Xanaxiel/38e23794223ef9cddf772562f5a24a70 to your computer and use it in GitHub Desktop.
AWS SNS to Discord Webhook

AWS SNS to Discord Webhook

Post SNS Message to Discord Webhook

Lambda Preferences

Key Value
Platform Node.js 14
handler index.handler

Environment Variables

Key Description
WEBHOOK_DISCORD Discord Webhook URL
const webhookUrl = process.env.WEBHOOK_DISCORD;
const https = require('https');
const url = new URL(webhookUrl);
exports.handler = function(event, context, callback) {
send_message(event);
};
function send_message(events){
const body = create_message(events);
const options = {
host: url.host,
path: url.pathname,
port: url.port,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(JSON.stringify(body))
},
};
let req = https.request(options, (res) => {
res.on('error', (e) => {
console.log('problem with request: ' + e.message);
});
});
req.write(JSON.stringify(body));
req.end();
}
function create_message(events){
const sns = events.Records[0].Sns;
const data = {
'embeds': [
{
'title': 'AWS Notification: ' + sns.Subject,
'description': sns.Message,
'color': 0xFF0000,
'timestamp': sns.Timestamp,
}
]
};
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment