Interview Prep/AWS/Route 53, CloudFront, API Gateway and Messaging

The browser shows a CORS error only when the API returns 401 or 4XX, while successful calls work.

AdvancedTroubleshootingRoute 53, CloudFront, API Gateway and Messaging
Answer

Likely causes → The Lambda adds Access-Control-Allow-Origin to its own responses, but errors produced by API Gateway itself (authorizer denies, throttling, missing routes) are gateway responses without CORS headers. The browser then hides the real 401 behind a CORS error.

Diagnose →

curl -i -X GET https://abc123.execute-api.us-east-1.amazonaws.com/prod/me -H 'Origin: https://app.example.com'
aws apigateway get-gateway-responses --rest-api-id abc123 --query 'items[].[responseType,responseParameters]'

Fix →

aws apigateway put-gateway-response --rest-api-id abc123 --response-type DEFAULT_4XX \
  --response-parameters '{"gatewayresponse.header.Access-Control-Allow-Origin":"'"'"'https://app.example.com'"'"'"}'
aws apigateway create-deployment --rest-api-id abc123 --stage-name prod

Do the same for DEFAULT_5XX. On HTTP APIs, use the built-in CORS configuration, which covers this.

Prevent → Define CORS once at the API level in IaC, and test error paths from a browser origin.

aws All AWS questions Roadmap: AWS core services

More on Route 53, CloudFront, API Gateway and Messaging