- Newest
- Most votes
- Most comments
To properly call your backend API from your frontend using the same domain, there are several aspects to check and configure correctly:
- CloudFront Path Pattern Configuration:
- Ensure your CloudFront distribution has the correct path pattern for API requests (/api/*) pointing to your ALB origin
- Verify the path pattern is case-sensitive and matches exactly what your frontend is requesting
- Origin Configuration:
- Check that the ALB origin in CloudFront is correctly configured with the proper domain name
- Ensure the origin path is empty if your API doesn't require a base path
- Verify that the ALB's security groups allow traffic from CloudFront
- Cache Behavior Settings:
- For API requests, you should disable caching or use appropriate cache settings
- Configure proper origin request policies to forward all necessary headers, cookies, and query strings
- Set appropriate TTL values (often 0 for APIs that need fresh responses)
- CloudFront Cache Invalidation:
- If you've made changes to your configuration, try invalidating the CloudFront cache for the /api/* paths
- API Gateway Integration (if applicable):
- If you're using API Gateway behind your ALB, ensure it's properly configured to handle the requests
- Health Checks and Routing:
- Verify that your ALB target groups have healthy targets
- Check that your ECS services are running and properly registered with the ALB
Since you mentioned that creating a separate domain (back.example.com) worked, the issue is likely related to how CloudFront is routing the /api/* requests to your ALB. The 502 error suggests that CloudFront is either not correctly forwarding the requests to your ALB or your backend service is not properly handling the requests.
If you want to maintain a single domain approach (web.example.com for both frontend and API), ensure your CloudFront behavior for /api/* is correctly configured to forward all necessary headers and that your backend service is properly set up to handle requests coming through CloudFront.
Sources
Backend and Frontend on same domain Cloudfront error | AWS re:Post
Using CloudFront as a Non-Cached Backend Request Proxy | AWS re:Post
answered a year ago
Relevant content
asked 2 years ago
