---
slug: "sveltekit-amplify-body-size-limit"
title: "Resolving the BODY_SIZE_LIMIT for File Uploads on AWS Amplify Hosting with SvelteKit SSR"
description: "I resolved an issue in an app deployed to AWS Amplify using SvelteKit + amplify-adapter that was causing the upload error: \"SvelteKitError: Content-length of 605227 exceeds limit of 524288 bytes.\""
url: "https://www.ytyng.com/en/blog/sveltekit-amplify-body-size-limit"
publish_date: "2025-07-16T12:03:07Z"
created: "2025-07-16T12:03:07.617Z"
updated: "2026-02-27T08:47:11.124Z"
categories: ["AWS", "Svelte"]
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20250716/e4c76d80ff4f45c79b25ebb5e0f2aa5d.png.webp?width=768"
has_video: true
has_music: true
video_urls: ["https://media.ytyng.net/ytyng-blog/324/featured-video-1.mp4", "https://media.ytyng.net/ytyng-blog/324/featured-video-2.mp4", "https://media.ytyng.net/ytyng-blog/324/featured-video-3.mp4"]
music_urls: ["https://media.ytyng.net/ytyng-blog/324/featured-music-324-1.mp3", "https://media.ytyng.net/ytyng-blog/324/featured-music-324-2.mp3"]
lang: "en"
---

# Resolving the BODY_SIZE_LIMIT for File Uploads on AWS Amplify Hosting with SvelteKit SSR

## Problem overview

In an application deployed to AWS Amplify Hosting using SvelteKit + amplify-adapter, the following error occurred when uploading an image file:

```
Upload error: SvelteKitError: Content-length of 605227 exceeds limit of 524288 bytes.
```

A 566 KB file failed because of a 512 KB (524,288 bytes) limit.

## Environment

- SvelteKit
- amplify-adapter 0.2.0
- AWS Amplify Hosting (SSR)
- Node.js Lambda environment

## Investigation process

### 1. Initial investigation

In a normal SvelteKit setup, the body size limit should be configurable via the `BODY_SIZE_LIMIT` environment variable.

```bash
# .env
BODY_SIZE_LIMIT=52428800  # 50MB
```

### 2. Checking environment variables

- AWS Amplify environment variable settings ✅  
- AWS Systems Manager Parameter Store ✅  
- `.env` file ✅

Despite being set correctly everywhere, `process.env.BODY_SIZE_LIMIT` was `undefined` in the Lambda environment.

### 3. Understanding SvelteKit’s environment variable system

SvelteKit handles environment variables as follows:

- `$env/static/private` - read from `.env` at build time  
- `process.env` - runtime environment variables

I found that `$env/static/private` could read the value, but `process.env` returned `undefined`.

### 4. Internal implementation of amplify-adapter

Inspecting the amplify-adapter source, `handler.js` contained the following:

```javascript
const body_size_limit = parseInt(env('BODY_SIZE_LIMIT', '524288'));
```

The `env` function is defined in `env.js` and reads environment variables from `process.env`:

```javascript
function env(name, fallback) {
    const prefixed = "" + name;
    return prefixed in process.env ? process.env[prefixed] : fallback;
}
```

## Solution

### Attempts and experiments

1. svelte.config.js option — amplify-adapter does not support a `bodySizeLimit` option  
2. Custom implementation in hooks.server.ts — the upload was blocked before SvelteKit hooks ran  
3. Using dotenv — reading a `.env` file reliably in the Lambda environment proved tricky

### Final solution

I decided to directly modify amplify-adapter’s `env.js`.

#### 1. Create a patch file

I created `patches/env.js` to force `BODY_SIZE_LIMIT` to 50 MB:

```javascript
/* global "" */

const expected = new Set([
    'SOCKET_PATH',
    'HOST',
    'PORT',
    'ORIGIN',
    'XFF_DEPTH',
    'ADDRESS_HEADER',
    'PROTOCOL_HEADER',
    'HOST_HEADER',
    'BODY_SIZE_LIMIT',
]);

if ("") {
    for (const name in process.env) {
        if (name.startsWith("")) {
            const unprefixed = name.slice("".length);
            if (!expected.has(unprefixed)) {
                throw new Error(
                    `You should change envPrefix (${""}) to avoid conflicts with existing environment variables — unexpectedly saw ${name}`
                );
            }
        }
    }
}

/**
 * @param {string} name
 * @param {any} fallback
 */
function env(name, fallback) {
    // BODY_SIZE_LIMIT is forced to 50MB (52428800 bytes)
    if (name === 'BODY_SIZE_LIMIT') {
        return '52428800';
    }
    
    const prefixed = "" + name;
    return prefixed in process.env ? process.env[prefixed] : fallback;
}

export { env };
```

#### 2. Update the build script

In `sh/build-for-amplify.sh`, copy the patch file after the build:

```bash
# Copy compute assets and prepare the compute environment.
cp .env build/compute/default/.env
cd build/compute/default/
npm i --production
cd -

# Copy patched env.js that fixes BODY_SIZE_LIMIT to 50MB
cp patches/env.js build/compute/default/env.js
echo "✅ env.js patched with BODY_SIZE_LIMIT=52428800 (50MB)"
```

## Result

After the fix, uploading the 605 KB file succeeded! 🎉

## What I learned

1. amplify-adapter constraints: it behaves differently from standard SvelteKit environment variable handling  
2. Environment variables in Lambda: a `.env` file is not automatically loaded into `process.env` in the Lambda runtime  
3. Adapter internals: understanding the adapter implementation was necessary to solve the problem  
4. Simple fixes can be more maintainable: a direct change proved simpler than complex workarounds

## Next steps

- If other size limits are needed, update `patches/env.js` accordingly  
- Re-apply the patch if amplify-adapter is updated  
- Consider contributing the fix upstream to amplify-adapter

## References

- [amplify-adapter GitHub](https://github.com/gzimbron/amplify-adapter)  
- [SvelteKit Environment Variables](https://kit.svelte.dev/docs/modules#$env-static-private)  
- [AWS Amplify Documentation](https://docs.amplify.aws/)

---

Summary: The file upload limit in a SvelteKit + amplify-adapter environment was resolved by understanding the adapter’s internals and directly patching its environment loader. This reinforced the importance of knowing the differences in environment variable handling.
