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.
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
.env file ✅Despite being set correctly everywhere, process.env.BODY_SIZE_LIMIT was undefined in the Lambda environment.
SvelteKit handles environment variables as follows:
$env/static/private - read from .env at build timeprocess.env - runtime environment variablesI found that $env/static/private could read the value, but process.env returned undefined.
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;
}
bodySizeLimit option.env file reliably in the Lambda environment proved trickyI decided to directly modify amplify-adapter’s env.js.
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 };
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)"
After the fix, uploading the 605 KB file succeeded! 🎉
.env file is not automatically loaded into process.env in the Lambda runtimepatches/env.js accordinglySummary: 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.