aws / aws/aws-sam-cli

AWS::Serverless::HttpApi CorsConfiguration not working

Open
#2,637 9 comments 2 reactions 0 assignees View on GitHub
maintainer/need-followup stage/bug-repro type/bug type/documentation
Dominant language
Python
Stars
6.7k
Forks
1.2k
Avg merge
1d 10h
Merged PRs (30d)
52

Description

### Description:
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

```YAML
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
![image](https://user-images.githubusercontent.com/5972646/108440319-e582b300-7220-11eb-843a-4a4ce968d0ab.png)

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.
![image](https://user-images.githubusercontent.com/5972646/108440773-c59fbf00-7221-11eb-9374-913410ea1506.png)

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)

1. OS: Linux
2. `sam --version`: 1.18.1
3. AWS region: us-east-1

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.