--- title: 'Deploy SvelteKit on AWS' description: 'Deploy SvelteKit applications on AWS using static site generation with S3 and CloudFront, single-page apps, or full-stack SSR with ECS Fargate and Application Load Balancer.' --- import { Tabs, TabItem, LinkButton, Aside } from '@astrojs/starlight/components'; Deploy your [SvelteKit](https://svelte.dev/docs/kit) applications to AWS using Thunder patterns. This guide covers static deployments and server-side rendering options. SvelteKit offers flexible rendering modes that can be deployed using two Thunder libraries: 1. **Static Deployment** — Deploy Static site generation (SSG) or client-side rendered Single-page apps (SPAs) built with SvelteKit - using S3 and CloudFront with the [Single Page Application](/docs/patterns/s3-cloudfront) pattern 2. **Server-Side Rendering (SSR)** — Deploy server-rendered SvelteKit applications using ECS Fargate with the [Web Service](/docs/patterns/fargate-alb) pattern ## Static (SSG/SPA) Deployment --- Deploy static or client-side rendered SvelteKit applications to S3 and CloudFront using the [CDK-SPA](https://github.com/thunder-so/cdk-spa) library. Choose the configuration that best fits your needs: ### Create Project ```sh npm create svelte@latest my-sveltekit-app cd my-sveltekit-app npm install ``` ```sh pnpm create svelte my-sveltekit-app cd my-sveltekit-app pnpm install ``` ```sh bunx create-svelte my-sveltekit-app cd my-sveltekit-app bun install ``` ### Configure SvelteKit for SSG/SPA Use `adapter-static` to prerender your entire site at build time: ```ts title="svelte.config.js" import adapter from '@sveltejs/adapter-static'; export default { kit: { adapter: adapter(), }, }; ``` Enable prerendering for all pages: ```ts title="src/routes/+layout.ts" export const prerender = true; ``` Optionally, configure prerendering options: ```ts title="svelte.config.js" import adapter from '@sveltejs/adapter-static'; export default { kit: { adapter: adapter({ pages: 'build', assets: 'build', fallback: null, compress: true, strict: true }), }, }; ``` ### Install Dependencies and Setup Stack ```sh npm i tsx @thunderso/cdk-spa --save-dev ``` ```sh pnpm add -D tsx @thunderso/cdk-spa ``` ```sh bun add -d tsx @thunderso/cdk-spa ``` ```ts title="stack/index.ts" import { Cdk, SPAStack, type SPAProps } from "@thunderso/cdk-spa"; const myApp: SPAProps = { env: { account: 'your-account-id', region: 'us-east-1' }, application: 'your-application-id', service: 'your-service-id', environment: 'production', rootDir: '', // e.g. 'frontend/' for monorepos outputDir: 'build/', }; new SPAStack( new Cdk.App(), `${myApp.application}-${myApp.service}-${myApp.environment}-stack`, myApp ); ``` ### Environment Variables and Secrets for Build Configure build-time environment variables using the `buildProps` configuration: ```ts title="stack/index.ts" const myApp: SPAProps = { // ... other props buildProps: { environment: [ { VITE_API_URL: 'https://api.example.com' }, { VITE_ANALYTICS_ID: 'UA-XXXXXX' } ], secrets: [ { key: 'API_KEY', resource: '/my-app/API_KEY' }, ], }, }; ``` ### Deploy Build and deploy your static SvelteKit site: ```sh npm run build npx cdk deploy --all --app="npx tsx stack/index.ts" ``` ```sh pnpm run build pnpm exec cdk deploy --all --app="pnpm exec tsx stack/index.ts" ``` ```sh bun run build npx cdk deploy --all --app="bunx tsx stack/index.ts" ``` After deployment, you'll receive a **CloudFront URL** to access your static site. For complete documentation, see the [CDK-SPA README](https://github.com/thunder-so/cdk-spa). ## Full Stack Deployment --- Deploy server-side rendered SvelteKit applications using ECS Fargate and Application Load Balancer with the [CDK-WebService](https://github.com/thunder-so/cdk-webservice) library. This pattern is ideal for MPA and full-stack applications where you need server-side rendering, database integration, or API routes. ### Create Project ```sh npm create svelte@latest my-sveltekit-app cd my-sveltekit-app npm install ``` ```sh pnpm create svelte my-sveltekit-app cd my-sveltekit-app pnpm install ``` ```sh bunx create-svelte my-sveltekit-app cd my-sveltekit-app bun install ``` ### Configure SvelteKit for SSR Use `adapter-node` for Node.js server deployment: ```ts title="svelte.config.js" import adapter from '@sveltejs/adapter-node'; export default { kit: { adapter: adapter(), }, }; ``` By default, SvelteKit uses server-side rendering (SSR) for the initial page load, which improves SEO and perceived performance. You can also optionally prerender specific pages while keeping others server-rendered: ```ts title="src/routes/about/+page.ts" export const prerender = true; // Only for static pages ``` ### Install Dependencies and Setup Stack ```sh npm i tsx @thunderso/cdk-webservice --save-dev ``` ```sh pnpm add -D tsx @thunderso/cdk-webservice ``` ```sh bun add -d tsx @thunderso/cdk-webservice ``` ```ts title="stack/index.ts" import { Cdk, WebServiceStack, type WebServiceProps } from "@thunderso/cdk-webservice"; const svcProps: WebServiceProps = { env: { account: 'your-account-id', region: 'us-west-2' }, application: 'your-application-id', service: 'your-service-id', environment: 'production', rootDir: '', // e.g. 'app/' for monorepos }; new WebServiceStack( new Cdk.App(), `${svcProps.application}-${svcProps.service}-${svcProps.environment}-stack`, svcProps ); ``` ### Build Settings Using Nixpacks Configure automatic containerization with Nixpacks: ```ts title="stack/index.ts" const svcProps: WebServiceProps = { // ... other props buildProps: { buildSystem: 'Nixpacks', installcmd: 'bun install', buildcmd: 'bun run build', startcmd: 'bun start', }, }; ``` ### Build Settings Using Docker Container Alternatively, use a custom Dockerfile: ```dockerfile title="Dockerfile" FROM public.ecr.aws/docker/library/alpine # Install Bun RUN curl -fsSL https://bun.sh/install | bash # Update PATH for bun ENV PATH="/root/.bun/bin:$PATH" # Copy code, install and build WORKDIR /app COPY . . RUN bun install RUN bun run build EXPOSE 4321 CMD ["bun", "start"] ``` ```ts title="stack/index.ts" const svcProps: WebServiceProps = { // ... other props serviceProps: { dockerFile: 'Dockerfile', port: 4321, }, }; ``` ### Environment Variables and Secrets for SSR Configure runtime environment variables and secrets: ```ts title="stack/index.ts" const svcProps: WebServiceProps = { // ... other props serviceProps: { variables: [ { NODE_ENV: 'production' }, { PUBLIC_API_URL: 'https://api.example.com' } ], secrets: [ { key: 'DATABASE_URL', resource: 'arn:aws:secretsmanager:us-west-2:123456789012:secret:/my-app/DATABASE_URL-abc123' }, ], }, }; ``` ### Deploy Build and deploy your containerized application: ```sh npm run build npx cdk deploy --all --app="npx tsx stack/index.ts" ``` ```sh pnpm run build pnpm exec cdk deploy --all --app="pnpm exec tsx stack/index.ts" ``` ```sh bun run build npx cdk deploy --all --app="bunx tsx stack/index.ts" ``` After deployment, you'll receive an **Application Load Balancer URL** to access your application. For complete documentation, see the [CDK-WebService README](https://github.com/thunder-so/cdk-webservice).