Question

Difficulty: MediumHigh-Performing Content Delivery and Caching Solutions

A company runs a web application where the frontend static assets (images, CSS, and JavaScript files) are stored in an Amazon S3 bucket, and the backend dynamic API is hosted on Amazon EC2 instances behind an Application Load Balancer (ALB). The company wants to use a single Amazon CloudFront distribution to serve both the static assets and the dynamic API to prevent Cross-Origin Resource Sharing (CORS) issues. The dynamic API responses are user-specific and must never be cached, while the static assets should be cached at the edge to maximize performance and minimize origin load. Which configuration should a solutions architect recommend to meet these requirements?

  1. Create a cache behavior for the path pattern `/api/*` with the ALB as the origin, and associate it with the Managed-CachingDisabled cache policy. Create a default cache behavior (`*`) with the S3 bucket as the origin, and associate it with the Managed-CachingOptimized cache policy.Answer
  2. B
    Create a single default cache behavior (`*`) pointing to the ALB origin. Set the Minimum TTL, Default TTL, and Maximum TTL values to 0 to prevent caching of dynamic API responses, and configure the web application to retrieve static assets from S3 through ALB proxying.
  3. C
    Configure CloudFront to serve the static assets from the S3 bucket. Set up Route 53 with a latency routing policy to route all `/api/*` traffic directly to the Application Load Balancer, bypassing CloudFront to prevent caching of dynamic API responses.
  4. D
    Create a single default cache behavior (`*`) pointing to the S3 bucket origin. Write a custom Lambda@Edge function associated with the viewer request event to redirect all `/api/*` requests to the Application Load Balancer, and run continuous long-running API polling scripts on AWS Lambda to keep the connection warm.

Answer

Create a cache behavior for the path pattern `/api/*` with the ALB as the origin, and associate it with the Managed-CachingDisabled cache policy. Create a default cache behavior (`*`) with the S3 bucket as the origin, and associate it with the Managed-CachingOptimized cache policy.
Configuring separate cache behaviors allows path-based routing in CloudFront. The path pattern `/api/*` takes precedence over the default behavior (`*`), routing dynamic traffic to the ALB with caching disabled, while the default behavior routes static requests to S3 with caching optimized. This solves CORS issues by serving both under the same domain, ensures dynamic data is always fresh, and caches static assets to minimize origin load.

Step-by-Step Solution

1
Analyze the requirements for routing and caching dynamic vs static content.
The dynamic API `/api/*` needs to bypass caching entirely, while static assets under the default path `*` need to be cached.
This allows the application to serve both static and dynamic content under the same domain name, avoiding CORS issues.
2
Design the path-based cache behaviors in Amazon CloudFront.
Create a specific behavior for `/api/*` routed to the ALB, and a default behavior `*` routed to the S3 bucket.
CloudFront evaluates cache behaviors in order. A specific path pattern like `/api/*` will match API traffic first before falling back to the default behavior.
3
Select the appropriate cache policies for each behavior.
Apply the Managed-CachingDisabled policy (TTL = 0) to the `/api/*` behavior, and Managed-CachingOptimized policy to the default behavior.
This disables caching for API endpoints so that dynamic queries always fetch fresh, user-specific data, while static resources are cached at edge locations for high performance.

Key Concept

Using path-based cache behaviors in Amazon CloudFront to route traffic to different origins with distinct caching policies.
Rate this question