AWS - S3 - CORS

 

CORS is a feature of HTTP that uses headers to allow browsers to display content that a web server requested from a different origin. 

To allow your content to appear, configure a CORS policy on your Amazon S3 bucket. 

Proper configuration of the CORS policy makes sure that the appropriate headers are returned.

You can configure a CORS rule on your bucket using the Amazon S3 console or AWS CLI.

Cross-origin resource sharing (CORS) defines a way for client web applications that are loaded in one domain to interact with resources in a different domain. 

To configure your bucket to allow cross-origin requests, you add a CORS configuration to the bucket. 

A CORS configuration is a document that defines rules that identify the origins that you will allow to access your bucket, the operations (HTTP methods) supported for each origin, and other operation-specific information.

I am starting with creating an S3 bucket and setting up a static website. 


Post configuring a static website on s3. It's accessible on the FQDN. 

Now, Edit the bucket permission -> CORS.



Now, the bucket has CORS setup and it allows GET and HEAD calls from the Allowed Origins which is the same as the bucket endpoint (Static Page).  You can configure Allowed Origin as any domain name. It refers to which DOMAIN name the website can be accessed.                                                                                                                                                                                                                                                               To test CORS, I created a EC2 instance and doing a CURL with the below headers:
                                                                                                                                                                                                                               For example, if you make a request to http://www.rsinfominds.com/ in Chrome, the request header contains Origin:http://www.rsinfominds.com (the browser automatically sends this), and the response header from the server contains Access-Control-Allow-Origin:http://www.rsinfominds.com.                                                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                         This is my current CORS Configuration:
[
    {
        "AllowedHeaders": [
            "Authorization"
        ],
        "AllowedMethods": [
            "GET"
        ],
        "AllowedOrigins": [
            "http://rsinfominds.com
        ],
        "ExposeHeaders": [
            "Access-Control-Allow-Origin"
        ]
    }
]

This allows access to the S3 website from the allowed origins "https://rsinfominds.com"

So, In my curl I was calling as below:

 # curl -i  http://rsinfominds-cors-demo.s3-website-us-east-1.amazonaws.com/index.html -H  "Access-Control-Request-Method: GET" --request OPTIONS -H "Origin: http://www.example.com"
                                                                                                                                                                                  And it returned 403 and "This CORS request is not allowed".                                                                
   
                                                                                                                                                                                   When I change the Origin to "http://rsinfominds.com" in the curl it works.                                                                                                

                                                                                                                                                        

Comments

Popular posts from this blog

K8s - ETCD

SRE/DevOps Syllabus

K8s - Deployment and HPA replicas