You want to send automated messages from an AWS Lambda function to a Discord channel. Discord webhooks let you post messages without a bot. This guide covers the complete setup using Node.js. You will learn how to create a webhook in Discord, write a Lambda function that sends a POST request, and handle common errors.
Key Takeaways: Discord Webhook Setup with AWS Lambda
- Discord Server Settings > Integrations > Webhooks: Create a webhook URL that your Lambda function will call.
- Node.js HTTPS module: Use the built-in https module to send POST requests — no extra npm packages required.
- AWS Lambda environment variables: Store the webhook URL in an environment variable to keep it secure and easy to update.
How Discord Webhooks Work with AWS Lambda
A Discord webhook is a simple HTTP endpoint. When your Lambda function sends a POST request to this endpoint, Discord posts the message payload to the channel you configured. No authentication headers or OAuth tokens are needed — the webhook URL itself contains the authorization token.
AWS Lambda runs Node.js code in response to triggers such as an API Gateway call, an S3 bucket event, or a scheduled CloudWatch Event. The Lambda function constructs a JSON payload and sends it via HTTPS to the webhook URL. The entire flow is serverless, meaning you pay only for execution time and no servers to maintain.
Prerequisites
You need the following before you start:
- An AWS account with permissions to create Lambda functions and IAM roles
- A Discord server where you have the Manage Webhooks permission
- Node.js 18 or later runtime selected in your Lambda function
Steps to Create a Discord Webhook and Connect It to AWS Lambda
Step 1: Create the Discord Webhook
- Open Server Settings
In Discord, click your server name at the top left. Select Server Settings from the dropdown. - Go to Integrations
In the left menu, click Integrations. Then click Webhooks. - Create a New Webhook
Click the blue Create Webhook button. Give it a name such as AWS Notifications. Select the channel where messages will appear. - Copy the Webhook URL
Click Copy Webhook URL. Save this URL securely — it is your only credential. Anyone with this URL can post to your channel.
Step 2: Write the AWS Lambda Function
- Open the AWS Lambda Console
Sign in to AWS and go to Lambda. Click Create function. Choose Author from scratch. Set Runtime to Node.js 18.x or later. Click Create function. - Set the Webhook URL as an Environment Variable
Scroll to Environment variables. Click Edit. Add a variable with keyDISCORD_WEBHOOK_URLand paste your webhook URL as the value. Save. - Write the Function Code
In the Code source editor, replace the default code with the following. This example sends a simple text message when the function is invoked.const https = require('https'); exports.handler = async (event) => { const webhookUrl = process.env.DISCORD_WEBHOOK_URL; if (!webhookUrl) { throw new Error('DISCORD_WEBHOOK_URL environment variable not set'); } const message = { content: 'Hello from AWS Lambda!' }; const data = JSON.stringify(message); const url = new URL(webhookUrl); const options = { hostname: url.hostname, path: url.pathname + url.search, method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': data.length } }; return new Promise((resolve, reject) => { const req = https.request(options, (res) => { let body = ''; res.on('data', chunk => body += chunk); res.on('end', () => { if (res.statusCode === 204) { resolve({ statusCode: 200, body: 'Message sent' }); } else { reject(new Error(`Discord returned ${res.statusCode}: ${body}`)); } }); }); req.on('error', (e) => reject(e)); req.write(data); req.end(); }); }; - Deploy and Test
Click Deploy. Then click Test. Create a test event with default JSON. Click Test again. If successful, your Discord channel displays the message.
Step 3: Configure a Trigger (Optional)
- Add a Trigger
In the Lambda function overview, click Add trigger. Choose a source such as API Gateway, S3, or EventBridge (CloudWatch Events). - Set Permissions
For API Gateway, select Create a new API or use an existing one. For EventBridge, define a schedule expression such asrate(1 hour)to send a message every hour. - Test the Full Flow
Invoke the trigger and check your Discord channel for the message.
Common Issues When Setting Up Discord Webhooks with AWS Lambda
Lambda Returns Timeout Error
The default Lambda timeout is 3 seconds. Discord webhook responses are usually fast, but network issues can cause delays. Increase the timeout in the Lambda configuration to 10 seconds. Go to Configuration > General configuration > Edit and set Timeout to 10 seconds.
Webhook URL Is Invalid or Returns 404
Make sure you copied the full webhook URL from Discord. The URL format is https://discord.com/api/webhooks/ID/TOKEN. If you accidentally truncated it, Discord returns a 404 error. Also verify the webhook is not deleted from the Discord server.
Message Payload Exceeds Discord Limits
Discord webhook messages have a 2000 character limit for the content field. If your Lambda sends a longer message, Discord returns a 400 Bad Request. Split long messages into multiple webhook calls or use embeds, which have a higher combined limit of 6000 characters.
Environment Variable Not Read
If the function throws “DISCORD_WEBHOOK_URL environment variable not set”, check that the variable name matches exactly. AWS environment variable names are case-sensitive. Also verify the variable is set in the same Lambda function version you deployed.
Discord Webhook Payload Types: Simple Message vs Embed
| Item | Simple Message | Embed Message |
|---|---|---|
| Payload structure | { "content": "text" } |
{ "embeds": [{ "title": "...", "description": "..." }] } |
| Character limit | 2000 characters | 6000 characters total across all embeds |
| Supports formatting | Markdown only | Rich formatting: author, fields, footer, color, timestamp |
| Use case | Simple alerts, short status messages | Detailed reports, structured data, logs |
You now have a working Discord webhook integrated with AWS Lambda. The function sends messages to your Discord channel without additional infrastructure. To extend the setup, add error handling for network failures or use environment variables to change the message content without editing code. For advanced use, explore Discord embed objects to send rich notifications with colored sidebars and multiple fields.