refer bellow template
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >-
aws-sam
Globals:
Function:
Runtime: nodejs16.x
CodeUri: .
Architectures:
- arm64
Timeout: 120
MemorySize: 1024
Resources:
GatewayResponseDefault5XX:
Type: 'AWS::ApiGateway::GatewayResponse'
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
ResponseType: DEFAULT_5XX
RestApiId:
Ref: 'ApiGatewayApi'
ApiGatewayApi:
Type: AWS::Serverless::Api
Properties:
StageName: staging
Cors:
AllowOrigin: "'*'"
AllowMethods: "'POST, OPTIONS'"
AllowHeaders: "'*'"
lambdaFunction:
Type: 'AWS::Serverless::Function'
Properties:
FunctionName: lambdaFn
Handler: src/handlers/lambdaFn.handler
Events:
PublicFunctionCreate:
Type: Api
Properties:
Path: /public/create
Method: post
RestApiId:
Ref: ApiGatewayApi
the above SAM template is used and works perfectly and from lambda function I return
return {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'OPTIONS,POST',
'Access-Control-Allow-Headers': '*',
}
}
which works in normal case but since my lambda function runs for more than 30sec, the API gateway timeout after 30sec which is expected and returns 504 error since the 'Access-Control-Allow-Origin' header is missing in the response header due to which the browser gives cors error.
Though in template I have mentioned DEFAULT_5XX response header it doesn't work.
Am I missing something or it is an issue with AWS SAM