AWS Lambda
Run an HTTP bot on AWS Lambda with a function URL.
This page assumes you've read Serverless, and have a createBot() function like the one there.
Lambda has no waitUntil: it freezes the function as soon as the handler returns. The handler below waits for your
handlers to finish before returning, so Discord gets its HTTP response only after your command is done.
Its reply still reaches Discord in time, since commands reply through the API, but keep commands short, and move slow work (anything after a defer that takes more than a few seconds) to a queue processed by another function.
Writing the handler
Lambda passes the request as an event instead of a Request, so call the client's handle() yourself. The body can
arrive base64-encoded, and the signature is checked against its exact bytes, so decode it as a buffer:
import type { , } from 'aws-lambda';
const = (. as , false).(() => .());
export async function (: ): <> {
const = . === '/webhook-events'
? (await ).()
: await ;
const = await .({
: .(. ?? '', . ? 'base64' : 'utf8'),
: .['x-signature-ed25519'],
: .['x-signature-timestamp'],
: ,
});
// Lambda freezes once this returns, so wait for the handlers to finish first
await .;
return { : ., : ., : . };
}Deploying
Deploy the function the way you prefer. Some ways to do it:
- By hand: bundle with
npx esbuild src/index.ts --bundle --platform=node --target=node22 --format=esm --outfile=dist/index.mjs, zipdist/index.mjs, and upload it on the AWS console. The handler is thenindex.handler. - AWS SAM: a function with
BuildMethod: esbuildand aFunctionUrlConfig, deployed withsam build && sam deploy. - AWS CDK: a
NodejsFunction, which bundles your TypeScript by itself, withaddFunctionUrl(). - Continuous deployment: run either of the above from your CI, like GitHub Actions with
aws-actions/configure-aws-credentials, on every push. It's a good place to deploy your commands too.
Remember to deploy your commands whichever way you choose.
Setting the endpoint
On the Developer Portal, set your app's Interactions Endpoint URL to your function URL, and its Webhook Events URL
(if you use them) to the same URL ending in /webhook-events.
Last updated on
