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

AWS
2025-07-16 12:03 (15 months ago)
Bureaucrats of the Cloud
Play a song themed on this article

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.

# .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:

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:

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:

/* 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:

# 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


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.

Categories

Archive