Skip to content

Backend and Frontend on same domain Cloudfront error

0

Currently I am using a cloudfront distribution to serve my frontend. I am looking to use the same domain for backend too. For that I have added a behavior in cloudfront distribution. All the /api/* requests are mapped to go to an API gateway origin. However when I make the api call to /api/* path pattern, Im getting 404 Not found. The request is reaching cloudfront but not getting redirected to the api gateway.

I have verified for typos, origin path is empty and the api gateway url is functional. Am I missing something?

2 Answers
0

Hello.

You’re using CloudFront to handle both your website (frontend) and your API (backend) on the same domain. To make this work you’ve set up CloudFront to forward all requests that start with** /api/*** to your API Gateway. However, when you try to access these API routes like /api/data you get an error, meaning CloudFront is not correctly sending the requests to the API Gateway.

Check Path Pattern

  • the path pattern in CloudFront for the API is set as /api/*. CloudFront is case-sensitive, so double-check for any discrepancies between the path pattern and the requests.

Verify API Gateway Permissions

  • CloudFront has permission to access your API Gateway. You might need to configure the API Gateway resource policy to allow CloudFront requests or set up an IAM role with the right permissions.

Clear CloudFront Cache

  • CloudFront might have cached an incorrect response. Create a cache invalidation to force CloudFront to update.

aws cloudfront create-invalidation --distribution-id <your-distribution-id> --paths "/api/*"

Check API Gateway Stage

  • If your API Gateway uses a stage like /prod, ensure that this stage is reflected in your CloudFront configuration. You may need to append /prod to the Origin Path in the CloudFront settings for API Gateway.

https://aws.amazon.com/blogs/networking-and-content-delivery/improve-single-page-application-spa-performance-with-a-same-domain-policy-using-amazon-cloudfront/#:~:text=One%20way%20to%20solve%20this%20issue%20is%20to,that%20can%20forward%20the%20requests%20to%20API%20Gateway.

https://repost.aws/knowledge-center/api-gateway-domain-cloudfront

EXPERT
answered a year ago
EXPERT
reviewed a year ago
  • I have tried the below things as said.

    • Checked for the spelling and case sensitivity.
    • API gateway resource policy is aptly set to give access to cloudfront
    • Invalidated cloudfront cache and tried.
    • Read the mentioned link

    Still my issue persists.

    When tried with REST API, Im getting

    • 403 Message: Forbidden response - when there is no origin path specified - (request is domain.com/api/staging/login)
    • 403 Message: Missing Authentication Token - when there is /staging (my stage name) specified in the origin path - (request is domain.com/api/login)

    When tried with HTTP API, Im getting

    • 404 Message: Not Found in all scenarios There is no resource policy for HTTP API so couldn't add any permissions

    In all the above scenarios, when I hit the request directly to the AWS gateway API endpoint, the response is 200 OK.

    Have tried many things from the internet but of no avail. Still looking forward for any new pointers.

0

Hey Minicode,

Check the Path Pattern:

  • Ensure the /api/* path pattern is correct and matches what you expect in CloudFront. The pattern is case-sensitive.
  • More details here: Using Cache Behaviors to Route Requests.

https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/distribution-web-values-specify.html#DownloadDistValuesPathPattern

Verify Permissions:

  • Make sure that CloudFront has permission to invoke the API Gateway. You may need to configure the API Gateway’s resource policy to allow CloudFront access.
  • You can learn more about setting up an API Gateway resource policy here: Controlling Access to API Gateway.

https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-resource-policies.html

Invalidate CloudFront Cache:

  • CloudFront might be caching a 404 response. Try invalidating the cache to ensure you’re seeing the latest responses from your API Gateway.
  • Read more about invalidating cache here: Invalidating Files in CloudFront.

https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Invalidation.html

Verify API Stage Configuration:

  • If your API Gateway has a base path or stage (like /prod), ensure this is correctly handled in CloudFront. You might need to adjust the origin settings.
  • For more details, see: Setting Up a Custom Domain Name for REST APIs.

https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-custom-domains.html

EXPERT
answered a year ago
  • I have tried the below things as said.

    • Checked for the spelling and case sensitivity.
    • API gateway resource policy is aptly set to give access to cloudfront
    • Invalidated cloudfront cache and tried.
    • Read the mentioned link

    Still my issue persists.

    When tried with REST API, Im getting

    • 403 Message: Forbidden response - when there is no origin path specified - (request is domain.com/api/staging/login)
    • 403 Message: Missing Authentication Token - when there is /staging (my stage name) specified in the origin path - (request is domain.com/api/login)

    When tried with HTTP API, Im getting

    • 404 Message: Not Found in all scenarios There is no resource policy for HTTP API so couldn't add any permissions

    In all the above scenarios, when I hit the request directly to the AWS gateway API endpoint, the response is 200 OK.

    Have tried many things from the internet but of no avail. Still looking forward for any new pointers.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.