Skip to content

Instantly share code, notes, and snippets.

@Xanaxiel
Forked from m2wasabi/README.md
Created October 20, 2021 06:18
Show Gist options
  • Save Xanaxiel/38e23794223ef9cddf772562f5a24a70 to your computer and use it in GitHub Desktop.
Save Xanaxiel/38e23794223ef9cddf772562f5a24a70 to your computer and use it in GitHub Desktop.

Revisions

  1. @m2wasabi m2wasabi revised this gist Aug 11, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ Post SNS Message to Discord Webhook
    | Platform | Node.js 14 |
    | handler | index.handler |

    ## Environment Variavles
    ## Environment Variables

    | Key | Description |
    | --- | --- |
  2. @m2wasabi m2wasabi created this gist Aug 10, 2021.
    16 changes: 16 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    # AWS SNS to Discord Webhook

    Post SNS Message to Discord Webhook

    ## Lambda Preferences

    | Key | Value |
    | --- | --- |
    | Platform | Node.js 14 |
    | handler | index.handler |

    ## Environment Variavles

    | Key | Description |
    | --- | --- |
    | WEBHOOK_DISCORD | Discord Webhook URL |
    45 changes: 45 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    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;
    }