aws / aws-sdk-js

AWS SDK for JavaScript in the browser and Node.js (In Maintenance Mode, End-of-Life on 09/08/2025). The AWS SDK for JavaScript v3 in the browser and Node.js is available here: https://github.com/aws/aws-sdk-js-v3
https://aws.amazon.com/developer/language/javascript/
Apache License 2.0
7.6k stars 1.55k forks source link

CORS Error When Accessing Swagger API Deployed on AWS EC2 #4671

Closed ashu1230 closed 2 months ago

ashu1230 commented 2 months ago

Describe the issue

I have deployed a NestJS backend on an AWS EC2 instance. The Swagger UI is accessible, but when I try to hit the API using Swagger, I receive the following error:

ISSUE: Failed to fetch. Possible Reasons:

  1. CORS
  2. Network Failure
  3. URL Scheme must be "http" or "https" for CORS request. Details: The API works correctly when tested directly on the EC2 instance (e.g., using curl). The Swagger UI is accessed via the public IP address of the EC2 instance. The error occurs when trying to access the API endpoint through the Swagger UI. My backend is running on http://localhost:3033, and the Swagger UI is being accessed via the public IP of the EC2 instance.

Troubleshooting Steps Taken: Confirmed that the backend API is accessible directly from the EC2 instance. Verified that the server is configured to handle CORS requests by setting the appropriate CORS headers in the NestJS application. Checked the network configurations and security groups in AWS EC2 to ensure proper access.

Environment: NestJS version: AWS EC2 Instance: Amazon linus(t2.micor). Swagger version: 6.0.5 Browser: Chrome, Edge Node.js version: Latest CORE: 9.0.0 Request URL: http://localhost:3033/api/v1/auth/signin

Error Image

Question: How can I resolve this CORS issue, and what configurations are required to allow the Swagger UI to make successful API requests in this setup?

Links

Request URL: http://localhost:3033/api/v1/auth/signin

zshzbh commented 2 months ago

@ashu1230,

I don't think this is AWS SDK JS specific issue. Could you please check with the API owner to make sure your request URL is on the allowlist?

ashu1230 commented 2 months ago

@zshzbh

Yes it is.

ashu1230 commented 2 months ago

issue now solved

zshzbh commented 2 months ago

Thanks for closing the ticket, just want to get more info for future reference.

May I know how does this issue get resolved on your end?