AWS Lambda Function URLs are one of those features that feel almost suspiciously easy. Click a checkbox, get a public HTTPS endpoint, ship it.
Then the browser starts yelling about CORS.
I’ve seen this catch people because Function URLs blur two layers together:
- AWS can add CORS headers for you at the Function URL level.
- Your Lambda code can also return CORS headers.
Mix those carelessly and you get flaky behavior, broken preflights, and confusing browser errors that look unrelated to the actual bug.
Here are the mistakes I see most often with Lambda Function URLs, plus the fixes that actually work.
Mistake #1: Turning on CORS in AWS and also setting headers in code
This is probably the most common mess.
You configure CORS in the Lambda Function URL settings like this:
- Allowed origins:
https://app.example.com - Allowed methods:
GET, POST, OPTIONS
Then your handler also does this:
export const handler = async (event) => {
return {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
},
body: JSON.stringify({ ok: true }),
};
};
Now you’ve got two sources of truth. Depending on setup and response path, you can end up with inconsistent headers or values that don’t match your intent.
Fix
Pick one place to manage CORS.
For Lambda Function URLs, I usually prefer letting AWS handle the basic CORS policy if the API is simple. It keeps the handler clean. But if you need dynamic origin logic, manage CORS in code and make the AWS-side config minimal or disabled.
A clean app-level approach looks like this:
const allowedOrigins = new Set([
"https://app.example.com",
"https://admin.example.com",
]);
export const handler = async (event) => {
const origin = event.headers?.origin || event.headers?.Origin;
const allowOrigin = allowedOrigins.has(origin) ? origin : null;
if (event.requestContext?.http?.method === "OPTIONS") {
return {
statusCode: 204,
headers: {
...(allowOrigin && { "Access-Control-Allow-Origin": allowOrigin }),
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
"Access-Control-Max-Age": "86400",
"Vary": "Origin",
},
};
}
return {
statusCode: 200,
headers: {
...(allowOrigin && { "Access-Control-Allow-Origin": allowOrigin }),
"Content-Type": "application/json",
"Vary": "Origin",
},
body: JSON.stringify({ ok: true }),
};
};
If you go dynamic, add Vary: Origin. Otherwise caches can serve the wrong origin’s response.
Mistake #2: Forgetting preflight requests exist
A lot of developers test with curl, see a 200 OK, and assume the browser will be happy.
Then the frontend sends:
fetch("https://abcde.lambda-url.us-east-1.on.aws/data", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer token"
},
body: JSON.stringify({ hello: "world" })
});
That request triggers a preflight because of:
Content-Type: application/jsonAuthorizationPOST
The browser sends an OPTIONS request first. If your function or Function URL config doesn’t answer it correctly, the real request never happens.
Fix
Make sure OPTIONS is allowed and returns the right headers:
Access-Control-Allow-OriginAccess-Control-Allow-MethodsAccess-Control-Allow-Headers
Example:
if (event.requestContext?.http?.method === "OPTIONS") {
return {
statusCode: 204,
headers: {
"Access-Control-Allow-Origin": "https://app.example.com",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
"Access-Control-Max-Age": "86400",
},
};
}
If you want to sanity-check what your endpoint is actually returning, headertest.com is handy for quickly inspecting response headers without relying on browser error messages.
Mistake #3: Using * with credentials
This one never dies.
People set:
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
That combination is invalid for credentialed browser requests. If you’re sending cookies or using credentials: "include", the browser will reject it.
Fix
If credentials are involved, return the exact origin, not *.
const origin = event.headers?.origin;
const allowedOrigins = new Set(["https://app.example.com"]);
return {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": allowedOrigins.has(origin) ? origin : "",
"Access-Control-Allow-Credentials": "true",
"Vary": "Origin",
"Content-Type": "application/json",
},
body: JSON.stringify({ ok: true }),
};
Also make sure your frontend actually asks for credentials:
fetch("https://abcde.lambda-url.us-east-1.on.aws/me", {
credentials: "include"
});
No exact origin match, no cookies.
Mistake #4: Forgetting Access-Control-Expose-Headers
This one is subtle because the request succeeds, but frontend code can’t read a header it expects.
Browsers only expose a small safelist of response headers to JavaScript unless you explicitly allow more.
A real-world example: GitHub’s API sends:
access-control-allow-origin: *
access-control-expose-headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset, Warning
That’s not random. They expose the headers client apps actually need.
If your Lambda returns pagination headers, rate-limit headers, or trace IDs, your frontend won’t be able to read them unless you expose them.
Fix
Add Access-Control-Expose-Headers for anything your JavaScript needs:
return {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "https://app.example.com",
"Access-Control-Expose-Headers": "ETag, Link, X-Request-Id, X-RateLimit-Remaining",
"ETag": '"abc123"',
"Link": '</items?page=2>; rel="next"',
"X-Request-Id": "req-123",
"X-RateLimit-Remaining": "42",
"Content-Type": "application/json",
},
body: JSON.stringify({ items: [] }),
};
If the frontend reads it with response.headers.get("X-Request-Id"), this is the difference between req-123 and null.
Mistake #5: Allowing every header “just to make it work”
I get the temptation:
Access-Control-Allow-Headers: *
Sometimes that works in modern contexts, sometimes it interacts badly with older assumptions, credentials, proxies, or just your own debugging process. It also makes it harder to understand what your clients actually need.
Fix
Be explicit, especially for preflighted requests.
"Access-Control-Allow-Headers": "Content-Type, Authorization, X-Requested-With"
If you control the frontend, this is easy. If you support third-party clients, document the allowed headers clearly.
Same advice for methods. Don’t return a giant list if your endpoint only supports GET and POST.
Mistake #6: Not handling errors with the same CORS policy
Happy-path responses often get the right CORS headers. Error responses don’t.
Then the frontend sees a generic CORS failure instead of the actual 401, 403, or 500. That wastes a ton of debugging time because you start chasing CORS when the real problem is auth or a thrown exception.
Fix
Return CORS headers on every response path.
function corsHeaders(origin) {
const allowedOrigins = new Set(["https://app.example.com"]);
return {
...(allowedOrigins.has(origin) && { "Access-Control-Allow-Origin": origin }),
"Vary": "Origin",
};
}
export const handler = async (event) => {
const origin = event.headers?.origin || "";
try {
if (!event.headers?.authorization) {
return {
statusCode: 401,
headers: {
...corsHeaders(origin),
"Content-Type": "application/json",
},
body: JSON.stringify({ error: "missing auth" }),
};
}
return {
statusCode: 200,
headers: {
...corsHeaders(origin),
"Content-Type": "application/json",
},
body: JSON.stringify({ ok: true }),
};
} catch (err) {
return {
statusCode: 500,
headers: {
...corsHeaders(origin),
"Content-Type": "application/json",
},
body: JSON.stringify({ error: "internal error" }),
};
}
};
Consistency matters more than cleverness here.
Mistake #7: Confusing CORS with actual security
CORS is a browser access control policy. It is not API auth. It does not protect your Lambda Function URL from non-browser clients.
If your Function URL is public, anyone with the URL can hit it using curl, Postman, a script, or a different backend, regardless of your CORS policy.
Fix
Treat CORS as a browser-sharing policy, not a security boundary.
Use actual controls for protection:
- AWS IAM auth for Function URLs when appropriate
- application auth like JWT or session validation
- rate limiting in front of the function if needed
- proper response headers beyond CORS
If you’re tightening browser-facing security generally, that’s where headers like CSP start to matter, and csp-guide.com is a solid reference for that side of the stack.
Mistake #8: Ignoring caching behavior
Browsers cache preflight responses. CDNs and proxies may cache actual responses. If your origin allowlist is dynamic and you forget cache-related details, users can get weird, inconsistent failures.
This gets especially ugly when one user from https://app.example.com primes a cache and another user from https://admin.example.com gets the wrong Access-Control-Allow-Origin.
Fix
When origin-specific responses are possible, send:
Vary: Origin
And be deliberate with Access-Control-Max-Age. High values reduce preflight chatter, but they also make mistakes stick around longer during rollout.
I usually start modestly while testing and increase later.
A practical baseline
For many Lambda Function URL setups, this baseline is enough:
- exact allowed origins
- explicit methods
- explicit headers
OPTIONSsupportVary: OriginAccess-Control-Expose-Headerswhen needed- same CORS behavior on success and error
Example:
const allowedOrigins = new Set(["https://app.example.com"]);
function getCorsHeaders(origin) {
if (!allowedOrigins.has(origin)) return {};
return {
"Access-Control-Allow-Origin": origin,
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
"Access-Control-Expose-Headers": "ETag, X-Request-Id",
"Access-Control-Max-Age": "3600",
"Vary": "Origin",
};
}
export const handler = async (event) => {
const origin = event.headers?.origin || event.headers?.Origin || "";
const cors = getCorsHeaders(origin);
const method = event.requestContext?.http?.method;
if (method === "OPTIONS") {
return {
statusCode: 204,
headers: cors,
};
}
return {
statusCode: 200,
headers: {
...cors,
"Content-Type": "application/json",
"ETag": '"v1"',
"X-Request-Id": crypto.randomUUID(),
},
body: JSON.stringify({ ok: true }),
};
};
That won’t solve every edge case, but it avoids the mistakes that break most Lambda Function URL integrations.
My rule of thumb: if CORS behavior feels unpredictable, you probably have too many places setting headers or you forgot that the browser is negotiating with OPTIONS before your real request ever lands. That’s usually where the bug is.