AWS Lambda adapter for SvelteKit applications
npm install @foladayo/sveltekit-adapter-lambda
// svelte.config.js
import adapter from "@foladayo/sveltekit-adapter-lambda";
export default {
kit: {
adapter: adapter(),
},
};
npm run build
The adapter generates a build/
directory with your Lambda function:
import { handler } from "./build/index.js";
Use index.handler
as your Lambda function handler.
// svelte.config.js
import adapter from "@foladayo/sveltekit-adapter-lambda";
export default {
kit: {
adapter: adapter({
out: "build", // Output directory (default: 'build')
precompress: false, // Gzip assets (default: false)
binaryMediaTypes: ["image/*"], // Binary content types for base64 encoding
bodySizeLimit: 6291456, // Body size limit in bytes (6MB default)
envPrefix: "MY_APP_", // Environment variable prefix
external: ["@aws-sdk/client-s3"], // Additional external dependencies
serveStatic: false, // Serve static assets from Lambda (default: false)
}),
},
};
By default, this adapter is optimized for SSR (Server-Side Rendering) only and does not serve static assets from Lambda.
adapter({
serveStatic: false, // Default - Lambda handles SSR only
});
Use CloudFront + S3 to serve static assets for optimal performance and cost efficiency.
adapter({
serveStatic: true, // Serves static assets from Lambda with optimized caching
});
When enabled, the adapter uses @foladayo/web-file-server for high-performance static asset serving with:
⚠️ Note: While optimized, serving static assets from Lambda still increases costs compared to CloudFront + S3. Use serveStatic: true
for simple deployments or when you need centralized asset serving.
The generated handler function is located at build/index.handler
and supports:
// src/routes/+page.server.js
export async function load({ platform }) {
const requestId = platform?.context?.awsRequestId;
const timeRemaining = platform?.context?.getRemainingTimeInMillis();
const event = platform?.event; // Original Lambda event
return { requestId, timeRemaining };
}
Environment variables are available through process.env
. If you configure an envPrefix
, the adapter will look for prefixed environment variables:
// With envPrefix: 'MY_APP_'
adapter({
envPrefix: "MY_APP_",
});
// Environment: MY_APP_DATABASE_URL=postgres://...
// Access as: process.env.MY_APP_DATABASE_URL