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?
- 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.Cevap
- BCreate 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.
- CConfigure 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.
- DCreate 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.
Cevap
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.
Adım Adım Çözüm
Anahtar Kavram
Using path-based cache behaviors in Amazon CloudFront to route traffic to different origins with distinct caching policies.