Hello, I am using serverless + API gateway and facing cors error if I pass custom headers.
I am adding cors configuration on the function level, and then it’s working fine, but at the provider level it’s not doing anything. So, if I pass the cors configuration to provider level, then it’s not working fine.
Can anyone help with this? How to do it at the provider level?
/* eslint-disable import/no-import-module-exports */
/* eslint-disable no-template-curly-in-string */
import type { AWS } from '@serverless/typescript';
const serverlessConfig: AWS = {
service: 'epicpass',
frameworkVersion: '3',
plugins: ['serverless-esbuild'],
provider: {
name: 'aws',
runtime: 'nodejs20.x',
versionFunctions: false,
stage: '${opt:stage, "sandbox"}',
region: 'ap-south-1',
iam: { role: { statements: [{ Effect: 'Allow', Action: '*', Resource: '*' }] } },
environment: {
STAGE: '${self:provider.stage}',
REGION: '${self:provider.region}',
},
httpApi: {
cors: {
allowedOrigins: ['*'],
allowedHeaders: ['*'],
allowedMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowCredentials: false,
},
},
},
custom: {
esbuild: {
bundle: true,
minify: false,
tsconfig: 'tsconfig.json',
exclude: ['@aws-sdk/*', 'aws-sdk'],
target: 'node20',
define: { 'require.resolve': undefined },
platform: 'node',
concurrency: 10,
},
},
package: {
patterns: ['!node_modules/**', '!package.json', '!package-lock.json'],
},
functions: {
AdminDashboard: {
handler: 'src/functions/v1/admin-api/dashboard/handler.api',
events: [
{ http: { path: '/v1/admins/testimonials', method: 'POST', cors: { headers: ['*'] } } },
],
},
},
};
module.exports = serverlessConfig;