Skip to content

CORS Configuration Issue with AWS Elastic Beanstalk and .env File Variables

0

Hi everyone,

I am facing a CORS issue with my AWS setup, and I could really use some guidance on how to resolve it.

Here’s the setup: Frontend: React app built with Vite, deployed via CloudFront Backend: Node.js/Express app deployed via Elastic Beanstalk Database: MongoDB Atlas

Problem: I am trying to configure my API URLs correctly using environment variables from my .env file. However, despite following the guidelines and ensuring that both my backend and frontend URLs are correct, I am still facing CORS errors when making requests from the frontend to the backend.

Here’s what I’ve tried so far:

The VITE_API_URL and FRONTEND_URL are set in my .env files, but I’m still getting a CORS error when calling the backend from the frontend (using Axios). I’ve configured CORS in the backend (Express) using the cors library, but the CORS headers don’t seem to be sent correctly. In my backend .env, I am setting the FRONTEND_URL (CloudFront URL), but the frontend still gets blocked due to CORS policies. I’ve tried hardcoding the URL to ensure it’s working, but when I use the dynamic environment variable, I continue to receive the error message: ERR_CONNECTION_REFUSED and 403 Forbidden. Example of .env files: Backend .env:

VITE_API_URL=https://your-backend-api-url FRONTEND_URL=https://djuvfvjpgozm1.cloudfront.net Frontend .env:

VITE_API_URL=https://djuvfvjpgozm1.cloudfront.net Error message: When trying to access the backend, the browser console shows:

CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Failed to fetch: POST http://localhost:5001/register net::ERR_CONNECTION_REFUSED Question:

How should I correctly set up the .env variables for both the frontend and backend, ensuring that the frontend API requests can pass through CORS without issues?

How do I properly configure the CORS policy in my Express backend to allow the CloudFront URL as an origin?

What could be the cause of the ERR_CONNECTION_REFUSED issue, and how can I ensure the production API URL is correctly used instead of localhost when deploying to Elastic Beanstalk?

Any help or advice would be greatly appreciated. Thanks in advance!

1 Answer
0

hi,

Frontend needs to talk to backend URL, and backend need to allow request only from CloudFront via CORS.

Frontend .evn file

VITE_API_URL=https://your-backend-api-url

FRONTEND_URL=https://djuvfvjpgozm1.cloudfront.net

[ Vite_API_URL needs to point to Beanstalk URL (e.g., https://my-app.us-east-1.elasticbeanstalk.com).]

your backend .eve need to know which origin to allow:

The CloudFront URL that hosts your frontend

FRONTEND_URL=https://djuvfvjpgozm1.cloudfront.net [this CF URL that backend will allow as an origin in CORS configuration]

CORS Configuration:

your Express app CORS is configured properly?

Express app needs to be configured to allow requests from CloudFront URL.

// Get the frontend URL from environment variables const frontendURL = process.env.FRONTEND_URL || 'https://djuvfvjpgozm1.cloudfront.net';

Note: after update <npm run build> and redeploy to CloudFront.

your CloudFront distribution forwards the Origin header to the backend here is the link: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/controlling-origin-requests.html?utm_source

Hope it will help Best

answered a year ago

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.