Fix Vercel FUNCTION_INVOCATION_FAILED for a Django App
Fix Vercel 500 FUNCTION_INVOCATION_FAILED and serverless function crashed errors for Django: read the function logs, expose the WSGI app correctly, fix ALLOWED_HOSTS, static files, database connections, and bundle size limits.
Problem statement
The deploy succeeds, but visiting the site shows:
500: INTERNAL_SERVER_ERROR
Code: FUNCTION_INVOCATION_FAILED
This Serverless Function has crashed.
Vercel runs a Django app as a Python serverless function. FUNCTION_INVOCATION_FAILED means the function raised an exception or exited before returning a response. Vercel shows only the generic page; the real traceback is in the function's runtime logs. Most crashes come from the entry point not exposing a WSGI app, a settings error at import, a missing environment variable, or a database the function cannot reach.
- Open the deployment's Logs tab (or
vercel logs <url>) and read the Python traceback. - Make sure the entry file exposes the WSGI app as
apporapplication. - Add the Vercel domains to
ALLOWED_HOSTS:.vercel.appand your custom domain. - Set every required environment variable in the Vercel project settings, for the right environment.
- Use a hosted database with a pooled connection string; SQLite files do not persist between invocations.
Step-by-step solution
1. Read the runtime logs
In the dashboard, open the deployment, then Logs, filtered to errors. From the CLI:
vercel logs https://myapp-abc123.vercel.app
Look for the last exception. Common ones:
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
DisallowedHost: Invalid HTTP_HOST header: 'myapp-abc123.vercel.app'.
django.db.utils.OperationalError: connection to server ... failed
ModuleNotFoundError: No module named 'config'
2. Expose the WSGI application
Vercel's Python runtime looks for a WSGI or ASGI callable named app (or application, handler) in the entry file. With the conventional layout:
# api/index.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.production")
app = get_wsgi_application()
{
"rewrites": [{ "source": "/(.*)", "destination": "/api/index" }]
}
A ModuleNotFoundError for your project package means the entry file cannot import it; keep the project at the repository root or adjust sys.path in the entry file.
3. Allow Vercel host names
Every deployment gets a unique *.vercel.app URL plus your aliases:
ALLOWED_HOSTS = [".vercel.app", "www.example.com", "example.com"]
CSRF_TRUSTED_ORIGINS = ["https://*.vercel.app", "https://www.example.com"]
4. Set environment variables for each environment
Vercel separates Production, Preview, and Development variables. A variable set only for Production is missing in preview deployments, so previews crash while production works. Add required settings to all environments that need them, then redeploy; variables are read at build and invocation time of a new deployment.
5. Use a database that suits serverless
Each invocation may run in a new instance, so local files, including SQLite databases and uploaded media, do not persist. Use a hosted PostgreSQL such as Neon, Supabase, or a managed instance, through its pooled endpoint, and keep CONN_MAX_AGE = 0 so idle functions do not hold connections:
DATABASES = {"default": dj_database_url.config(conn_max_age=0, ssl_require=True)}
Store media in object storage. Static files can be served by WhiteNoise or by Vercel's static output if you run collectstatic in the build.
Explanation
On Vercel, there is no long-running Gunicorn. A request triggers the function, which imports your entry module, which imports Django and your settings, and then calls the WSGI app once. If anything raises during import or the call, the platform cannot return your app's error page, so it returns its own. Cold starts make import-time errors especially visible, because every new instance re-runs the imports.
Edge cases / notes
- Bundle size. Python functions have a size limit; large dependencies such as pandas or heavy ML libraries can exceed it and fail at deploy or at load.
- DEBUG. Turning on
DEBUGbriefly shows Django's error page in previews, but never leave it on; logs are the safer source. - Migrations. Run them from CI or a one-off script against the database, not from the function.
Next steps
- Review the full setup in Deploy Django on Vercel.
- Manage database connections with Django Serverless Postgres Connections.
- For other 500 errors, read Fix Django 500 Internal Server Error in Production.
FAQ
Where do I see the real error?
In the deployment's runtime logs in the Vercel dashboard, or with vercel logs.
Why do preview deployments crash but production works?
Environment variables are set per environment. Previews often lack variables that exist only for Production.
Can I use SQLite on Vercel?
Not for real data. Function instances do not share or keep local files.
Can I run Celery on Vercel?
No. Use an external queue service or move background work to a platform that runs workers.