Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable CORS for API Gateway in Cloudformation template

I'm creating AWS Cloudformation template for my environment and I can't find a way to enable CORS for API Gateway method.

I can configure it using AWS console (here is the official doc), but how can I do it in the Cloudformation template?

like image 716
dds Avatar asked Oct 27 '16 19:10

dds


People also ask

How do I enable CORS in CloudFormation?

You can still set-up CORS yourself when importing an API from swagger or when defining an API via CloudFormation, but you must specify all the parameters for setting up the OPTIONS method as well as adding the CORS specific headers to your other methods.

How do I fix the CORS issue in AWS API gateway?

Cross-Origin Resource Sharing (CORS) errors occur when a server doesn't return the HTTP headers required by the CORS standard. To resolve a CORS error from an API Gateway REST API or HTTP API, you must reconfigure the API to meet the CORS standard.

How do I enable CORS on API gateway with Lambda proxy integration?

To enable CORS for the Lambda proxy integration, you must add Access-Control-Allow-Origin: domain-name to the output headers . domain-name can be * for any domain name. The output body is marshalled to the frontend as the method response payload.


1 Answers

After some trial and error, I found that the following CloudFormation template snippet will produce an equivalent OPTIONS method when compared to the CORS console wizard:

OptionsMethod:   Type: AWS::ApiGateway::Method   Properties:     AuthorizationType: NONE     RestApiId:       Ref: MyApi     ResourceId:       Ref: MyResourceOnWhichToEnableCORS     HttpMethod: OPTIONS     Integration:       IntegrationResponses:       - StatusCode: 200         ResponseParameters:           method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"           method.response.header.Access-Control-Allow-Methods: "'POST,OPTIONS'"           method.response.header.Access-Control-Allow-Origin: "'*'"         ResponseTemplates:           application/json: ''       PassthroughBehavior: WHEN_NO_MATCH       RequestTemplates:         application/json: '{"statusCode": 200}'       Type: MOCK     MethodResponses:     - StatusCode: 200       ResponseModels:         application/json: 'Empty'       ResponseParameters:           method.response.header.Access-Control-Allow-Headers: false           method.response.header.Access-Control-Allow-Methods: false           method.response.header.Access-Control-Allow-Origin: false 

*Note 1: This is an example of taking the defaults for a POST. Obviously, you'll need to update Access-Control-Allow-Methods to include the values you need.

*Note 2: Kudos to the AWS CloudFormation team for recently introducing YAML support. If you need to convert to/from YAML/JSON, I have found this site handy: http://www.json2yaml.com/

like image 134
dannymac Avatar answered Oct 23 '22 04:10

dannymac