š AWS S3 CORS: Allowing Your Website to Talk to S3


One of my āaha!ā moments with AWS was when I hosted a static website on S3. Everything looked greatāuntil my browser blocked requests to my S3 bucket. The error? CORS policy issue.
Letās demystify CORS and see how to fix it in S3.
š What is CORS?
CORS (Cross-Origin Resource Sharing) is a browser security feature. It prevents a website on one domain from freely accessing resources on another domain.
Example:
Your website is hosted on
mywebsite.com
.Your assets (images, JSON, fonts) are stored in an S3 bucket.
Without CORS, the browser blocks access.
š ļø Setting Up CORS on S3
Hereās how to configure it:
Go to your S3 bucket ā Permissions tab.
Scroll to Cross-origin resource sharing (CORS).
Add a configuration like this:
[
{
"AllowedHeaders": ["*"],
"AllowedMethods": ["GET", "POST"],
"AllowedOrigins": ["*"],
"ExposeHeaders": []
}
]
This example allows all origins to read from your bucket. For production, replace "*"
with your specific domain (e.g., "
https://mywebsite.com
"
).
ā Best Practices
Donāt keep
"*"
in productionāitās too open.Restrict methods to only what you need (e.g.,
GET
for images).Use HTTPS origins for security.
š Conclusion
CORS might look intimidating at first, but itās simply about telling browsers: āHey, itās okay for this website to fetch data from my bucket.ā
So the next time you face that scary red CORS error, just remember: all it takes is a small JSON config in your S3 bucket!
Subscribe to my newsletter
Read articles from Ruchika Gurbaxani directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Ruchika Gurbaxani
Ruchika Gurbaxani
AWS Cloud Enthusiast | Cybersecurity Aware Cloud-savvy and security-conscious, I'm a tech enthusiast with a strong foundation in Python, C, Networking, and Cloud Computing. Currently preparing for AWS certifications, Iāve completed the Mastercard Cybersecurity Virtual Experience and hold the ZTCA (Zscaler Zero Trust Certified Associate) certification. My projects reflect a passion for secure, scalable cloud solutionsānotably SilentWhisper, an anonymous communication app. Recognized for achievements in NASAās Scientist for a Day, Smart India Hackathon, and creative competitions like painting at Raman Science Center. I'm also active on LeetCode, HackerRank, and GitHub, where I regularly enhance my problem-solving and development skills.