AWS::Serverless::HttpApi CorsConfiguration not working
See original GitHub issueDescription:
I have an AWS::Serverless::HttpApi
resource with the CorsConfiguration
property. The template deploys ok and I can get a successful response from the endpoint sending a GET
request using Postman but, an OPTION
request returns error 404.
Steps to reproduce:
Here is my template
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Parameters:
EnvironmentName:
Description: An environment name
Type: String
Default: production
AllowedValues:
- sandbox
- production
- staging
StageName:
Type: String
Description: Api Stage Name
Default: 'v0'
UiUserPoolId:
Type: String
Description: Ui User Pool Id.
UiUserPoolClientId:
Type: String
Description: Ui User Pool Client Id.
Globals:
Function:
Runtime: nodejs12.x
MemorySize: 128
Timeout: 60
Handler: index.handler
Environment:
Variables:
AWS_NODEJS_CONNECTION_REUSE_ENABLED: 1
Resources:
MyApi:
Type: 'AWS::Serverless::HttpApi'
Properties:
StageName: !Ref 'StageName'
CorsConfiguration:
AllowCredentials: true
AllowMethods: "'GET,POST,OPTIONS'"
AllowHeaders: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
AllowOrigins: "'http://localhost:9000'"
Auth:
Authorizers:
OAuth2Authorizer:
JwtConfiguration:
issuer: !Sub 'https://cognito-idp.${AWS::Region}.amazonaws.com/${UiUserPoolId}'
audience:
- !Ref UiUserPoolClientId
IdentitySource: '$request.header.Authorization'
Tags:
Environment: !Sub '${EnvironmentName}'
FantazGlobalNotificationFunction:
Type: AWS::Serverless::Function
Properties:
Description: global notification for FantaZ UI
FunctionName: !Sub '${EnvironmentName}-client-global-notification-lambda'
CodeUri: ./global-notification/
MemorySize: 128
Environment:
Variables:
GLOBAL_NOTIFICATION_TABLE: !Sub '${EnvironmentName}-cf-GlobalNotification'
ORIGIN: 'http://localhost:9000'
Policies:
- DynamoDBReadPolicy:
TableName: !Sub '${EnvironmentName}-cf-GlobalNotification'
Events:
Get:
Type: HttpApi
Properties:
Path: /globalNotification
ApiId: !Ref MyApi
Method: GET
Tags:
Environment: !Sub '${EnvironmentName}'
Outputs:
MyApiUrl:
Value: !Sub 'https://${MyApi}.execute-api.${AWS::Region}.amazonaws.com/${StageName}'
Description: Client API URL
Observed result:
After deploying the template, if I go to the API Gateway console and open the CORS page this is what I see
As you can see, there is no value set. If I send a GET
to /v0/globalNotification
it works ok, but sending an OPTIONS
to the same endpoint fails with a 404 error.
Am I missing something? Is my template wrong?
Expected result:
Send an OPTIONS
request works as expected.
Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
- OS: Linux
sam --version
: 1.18.1- AWS region: us-east-1
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:6 (1 by maintainers)
Top Results From Across the Web
HTTPApi + Serverless Framework + API Gateway CORS not ...
I have an HTTPApi API Gateway created with the Serverless Framework. But for some routes, the CORS is not working. provider: name: aws...
Read more >Troubleshoot CORS errors from API Gateway - AWS
I get the error "No 'Access-Control-Allow-Origin' header is present on the requested resource" when I try to invoke my Amazon API Gateway ...
Read more >CORS not working - Serverless Forums
Cors should work by default. You likely do not need all those headers for both request and response. I suggest you try one...
Read more >API Gateway HTTP + Lambda integration not enabling CORS
I have an API Gateway with an HTTP API + route that utilizes a Lambda function integration. From the AWS documentation, I see...
Read more >Configuring CORS for an HTTP API - Amazon API Gateway
Learn how to configure CORS for an HTTP API. ... a response to preflight OPTIONS requests, even if there isn't an OPTIONS route...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Is the new HttpApi ready for production ? Is not only this that is not working, following all proper docs available online, I can’t get anything working
Internal Server Error
and no further detailsX-Api-Key
header, which ends up with theInternal Server Error
aboveDon’t know what else I can do
If of any help, the authorizer lambda code for my test is also as documented here https://aws.amazon.com/blogs/compute/introducing-iam-and-lambda-authorizers-for-amazon-api-gateway-http-apis/
Thanks, @sriram-mv for your response but, no, that does not help at all. I saw that doc already and I have to say it doesn’t help either. It only mentions CORS works only if the
DefinitionBody
property is specified but, what is the min required insideDefinitionBody
to make it work? Where can I find an example? There is no one blogging about it 😃I’ve tried this
As you can see, I’m specifying the
DefinitionBody
property but it doesn’t work either.And if I have to configure the
x-amazon-apigateway-cors
property insideDefinitionBody
, what is the point of having theCorsConfiguration
property? Am I missing something here or there is something not working as it should or, probably, the documentation is not enough?