How I Solved the BODY_SIZE_LIMIT for File Uploads with AWS Amplify Gen2 + SvelteKit SSR

AWS Svelte
2025-07-16 21:03 (32 hours ago) ytyng

Problem Overview

When deploying an application using SvelteKit with amplify-adapter to AWS Amplify, an error occurred during image file upload:

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

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

Environment

Investigation Process

1. Initial Investigation

Normally in SvelteKit, you can set the body size limit via the BODY_SIZE_LIMIT environment variable:

# .env
BODY_SIZE_LIMIT=52428800  # 50MB

2. Checking Environment Variables

Despite correct settings, process.env.BODY_SIZE_LIMIT was undefined in the Lambda environment.

3. Understanding SvelteKit’s Env Variable System

SvelteKit handles environment variables as follows:

It turned out that $env/static/private had the value, but process.env was undefined.

4. Internal Implementation of amplify-adapter

Inspecting amplify-adapter’s source code (handler.js):

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

The env function, defined in env.js, reads from process.env:

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

Solution

Attempts

  1. Option settings in svelte.config.js — amplify-adapter does not support bodySizeLimit option.
  2. Custom implementation in hooks.server.ts — stopped before internal SvelteKit limits.
  3. Using dotenv — complicated to load .env files correctly in Lambda.

Final Solution

Directly patch env.js in amplify-adapter to force BODY_SIZE_LIMIT to 50MB.

1. Creating a Patch File

Create patches/env.js that forces BODY_SIZE_LIMIT to 50MB (52428800 bytes):

/* 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) {
    // Force BODY_SIZE_LIMIT to 50MB
    if (name === 'BODY_SIZE_LIMIT') {
        return '52428800';
    }

    const prefixed = "" + name;
    return prefixed in process.env ? process.env[prefixed] : fallback;
}

export { env };

2. Modify Build Script

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

# Copy necessary assets for compute environment
cp .env build/compute/default/.env
cd build/compute/default/
npm i --production
cd -

# Overwrite env.js with patched version forcing BODY_SIZE_LIMIT=50MB
cp patches/env.js build/compute/default/env.js
echo "✅ env.js patched with BODY_SIZE_LIMIT=52428800 (50MB)"

Results

After the patch, uploading a 605KB file succeeded! 🎉

Lessons Learned

  1. amplify-adapter constraints: behaves differently from standard SvelteKit environment variable handling.
  2. Lambda env vars: .env files do not automatically populate process.env.
  3. Adapter internal knowledge: understanding internal implementation was essential.
  4. Simple fixes: direct patching was more maintainable than complex workarounds.

Future Actions

References


Summary: The file upload size limit issue in SvelteKit + amplify-adapter was resolved by understanding the adapter’s internals and directly patching its environment variable handling. This highlighted the importance of grasping differences in environment variable systems.

Currently unrated
The author runs the application development company Cyberneura.
We look forward to discussing your development needs.

Archive

2025
2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011