Course contentsShow
← Cloud and Infrastructure
Lesson 534 of 2,98910. API Gateway and Event-Driven PatternsFree lesson

CORS Configuration

Configure Cross-Origin Resource Sharing settings to enable browser-based applications to securely access your APIs from different domains.

CORS Configuration

You'll learn how to configure Cross-Origin Resource Sharing (CORS) settings in your API Gateway so browser-based applications can safely access your APIs from different domains.

What Is CORS?

CORS is a security mechanism built into web browsers that controls whether a web page from one domain (like https://myapp.com) can make requests to an API hosted on a different domain (like https://api.mycompany.com). Without proper CORS configuration, browsers block these cross-origin requests by default to protect users from malicious scripts.

Think of CORS as a bouncer at a club checking a guest list. Your API Gateway is the bouncer, and CORS settings are the list of domains (origins) you've pre-approved to access your API from a browser.

How CORS Works in API Gateway

When a browser makes a cross-origin request, it first sends a "preflight" OPTIONS request asking permission. Your API Gateway responds with CORS headers like:

  • Access-Control-Allow-Origin: Which domains can access (e.g., https://myapp.com or * for all)
  • Access-Control-Allow-Methods: Which HTTP methods are permitted (GET, POST, PUT, etc.)
  • Access-Control-Allow-Headers: Which custom headers the browser can send
  • Access-Control-Max-Age: How long the browser can cache the preflight response

Configuration Considerations

Configure CORS at the API Gateway stage rather than in each backend function. Specify exact origins instead of wildcards (*) in production for better security. Remember that CORS only protects browsers—server-to-server requests bypass CORS entirely.

Key Takeaway: CORS configuration in your API Gateway enables secure browser-based access to your APIs by explicitly allowing trusted domains to make cross-origin requests, preventing unauthorized domains from accessing your endpoints.