AWS Developer DVA-C02 · Development with AWS Services · medium

A web application hosted on https://app.example.com makes an XMLHttpRequest / fetch() POST call (with a custom Authorization: Bearer <jwt> header) directly from the user's browser to an Amazon API Gateway REST API hosted at https://api.example.org. The browser blocks the request with: Access to fetch at 'https://api.example.org' from origin 'https://app.example.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check. What must be configured on API Gateway to resolve this?

Published · Last updated · checked before publication — how we verify questions

Options

  • AEnable CORS on API Gateway to handle OPTIONS preflight and return Access-Control-Allow-*Correct answer
  • BPlace the API Gateway inside a Private VPC Subnet with EndpointConfiguration: PRIVATE only
  • CEnable AWS X-Ray active tracing on the API Gateway stage via tracingEnabled: true in SAM
  • DIncrease the API Gateway integration timeout from 29000 ms to 900000 ms on the POST route

Answer: A. Enable CORS on API Gateway to handle OPTIONS preflight and return Access-Control-Allow-*

When a browser sends a cross-origin request with a custom header (Authorization) or non-simple method, it first sends an HTTP OPTIONS preflight request. Enabling CORS (Cross-Origin Resource Sharing) on the API Gateway resource configures an OPTIONS method (and adds Access-Control-Allow-Origin, Access-Control-Allow-Headers, and Access-Control-Allow-Methods headers to both the preflight and actual responses).

Why the other options are wrong

  • B. Place the API Gateway inside a Private VPC Subnet with EndpointConfiguration: PRIVATE only — Making the API private prevents public browser clients from reaching it altogether.
  • C. Enable AWS X-Ray active tracing on the API Gateway stage via tracingEnabled: true in SAM — X-Ray tracing records performance spans and does not emit browser CORS headers.
  • D. Increase the API Gateway integration timeout from 29000 ms to 900000 ms on the POST route — API Gateway has a hard 29-second regional integration limit (unless using specific quota increases), and timeouts do not cause CORS preflight blocks.

All Development with AWS Services questions and the topic overview →

AWS and Amazon Web Services are trademarks of Amazon.com, Inc. PrepIQ Hub is not affiliated with, endorsed by, or sponsored by Amazon Web Services.