# Serverless Cors Issue Help

**URL:** https://forum.serverless.com/t/serverless-cors-issue-help/20687
**Category:** Serverless Framework
**Tags:** lambda, api-gateway
**Created:** [October 24, 2024, 2:07pm UTC](https://forum.serverless.com/t/serverless-cors-issue-help/20687 "2024-10-24T14:07:55Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![lucas1068](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/lucas1068/32/8063_2.png) [@lucas1068](https://forum.serverless.com/u/lucas1068)
#### Post date: [October 24, 2024, 2:07pm UTC](https://forum.serverless.com/t/serverless-cors-issue-help/20687/1 "2024-10-24T14:07:55Z")

</div>

I’m facing a persistent **CORS** issue when trying to make a **POST request** from my frontend application to an API deployed with the **Serverless Framework**. My frontend is running on **localhost:5173** (dev server) and the backend API endpoint is at **[https://zgrciu0uce.execute-api.us-east-1.amazonaws.com/dev/auth/createUserFromAdmin](https://zgrciu0uce.execute-api.us-east-1.amazonaws.com/dev/auth/createUserFromAdmin)**.

Error from the browser’s console

```auto
Access to XMLHttpRequest at 'https://zgrciu0uce.execute-api.us-east-1.amazonaws.com/dev/auth/createUserFromAdmin' from origin 'http://localhost:5173' has been blocked by CORS policy: Request header field access-control-allow-origin is not allowed by Access-Control-Allow-Headers in preflight response.
POST https://zgrciu0uce.execute-api.us-east-1.amazonaws.com/dev/auth/createUserFromAdmin net::ERR_FAILED

```

In **serverless.yml** , I added CORS configuration like this ([I’ve just followed official docs](https://www.serverless.com/framework/docs/providers/aws/events/apigateway#enabling-cors)):

```yaml
functions:
  authCreateUserFromAdminHandler:
    handler: src/modules/Auth/handlers/createUserFromAdminHandler.handler
    events:
      - http:
          path: auth/createUserFromAdmin
          method: post
          cors: true

```

In lambda function **authCreateUserFromAdminHandler** ’s code:

```ts
export const handler = async (event: APIGatewayEvent, context: Context, callback: Function) => {
/* ... process information */
  const headers = {
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Credentials': true
    }
  const response = {
            statusCode: 201,
            headers,
            body: JSON.stringify({ OK: true })
        }
}

```

And in my frontend I run requests with Axios. **Axios is configured this way** :

```ts
import axios from 'axios'
import { useAuthStore } from '@/stores/authStore';
const authStore = useAuthStore();

const api = axios.create({
  baseURL: 'https://zgrciu0uce.execute-api.us-east-1.amazonaws.com/dev',
  headers: {
    'Content-Type': 'application/json',
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Headers': 'Content-Type',
   'Authorization': `${authStore.tokenType} ${authStore.accessToken}`
  },
});
export { api }

```

Then in my .vue components, I just use the axios exported before:

```ts
import { api } from '@/libs/axios'

await api.post('/auth/createUserFromAdmin', newUser) // this line throws the error (see beginning of this post).

```

I wonder how could I fix this?

---

<div class="post-metadata">

### Author: ![tochiOz](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/tochioz/32/7944_2.png) [@tochiOz](https://forum.serverless.com/u/tochiOz)
#### Post date: [October 29, 2024, 7:29pm UTC](https://forum.serverless.com/t/serverless-cors-issue-help/20687/2 "2024-10-29T19:29:39Z")

</div>

There could be a number of reasons why CORS error occur. Try the following solutions

### Update the Lambda Handler Response Headers

Ensure the handler in your Lambda function returns the correct CORS headers, especially `Access-Control-Allow-Origin`, `Access-Control-Allow-Headers`, and `Access-Control-Allow-Credentials`.

javascript

Copy code

```auto
export const handler = async (event, context, callback) => {
  const headers = {
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Credentials': true,
    'Access-Control-Allow-Headers': 'Content-Type, Authorization'
  };
  
  const response = {
    statusCode: 201,
    headers,
    body: JSON.stringify({ OK: true })
  };
  return response;
};

```

### Remove `Access-Control-Allow-Origin` Header from Axios Configuration

Including `Access-Control-Allow-Origin` in your Axios headers can cause conflicts, as browsers don’t allow clients to set `Access-Control-*` headers manually. Remove it from the Axios configuration:

javascript

Copy code

```auto
const api = axios.create({
  baseURL: 'https://zgrciu0uce.execute-api.us-east-1.amazonaws.com/dev',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': `${authStore.tokenType} ${authStore.accessToken}`
  },
});

```

### Clear Cache and Retry

After deploying these changes, you might want to clear your browser’s cache and try again

Let me know if this helps, if not, it might be the api gateway configuration

---

<div class="post-metadata">

### Author: ![beenmeckel](https://avatars.discourse-cdn.com/v4/letter/b/8dc957/32.png) [@beenmeckel](https://forum.serverless.com/u/beenmeckel)
#### Post date: [April 21, 2026, 10:53pm UTC](https://forum.serverless.com/t/serverless-cors-issue-help/20687/3 "2026-04-21T22:53:33Z")

</div>

To resolve CORS issues in a serverless environment (specifically with AWS Lambda and API Gateway), you must ensure that both the infrastructure (API Gateway) and the application code (Lambda) are correctly configured to handle preflight and actual requests.

[snaptube](https://snaptube.cam/)[vidmate](https://vidmate.bid/)
