AWS CloudFront access denied to S3 bucket

Amazon Web-ServicesAmazon S3Amazon Cloudfront

Amazon Web-Services Problem Overview


I am trying to setup CloudFront to serve static files hosted in my S3 bucket. I have setup distribution but I get AccessDenied when trying to browse to the CSS (/CSS/stlyle.css) file inside S3 bucket:

<Error>
    <Code>AccessDenied</Code>
    <Message>Access Denied</Message>
    <RequestId>E193C9CDF4319589</RequestId>
    <HostId>
xbU85maj87/jukYihXnADjXoa4j2AMLFx7t08vtWZ9SRVmU1Ijq6ry2RDAh4G1IGPIeZG9IbFZg=
    </HostId>
</Error>

I have set my CloudFront distribution to my S3 bucket and created new Origin Access Identity policy which was added automatically to the S3 bucket:

{
	"Version": "2008-10-17",
	"Id": "PolicyForCloudFrontPrivateContent",
	"Statement": [
		{
			"Sid": "1",
			"Effect": "Allow",
			"Principal": {
				"AWS": "arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity E21XQ8NAGWMBQQ"
			},
			"Action": "s3:GetObject",
			"Resource": "arn:aws:s3:::myhost.com.cdn/*"
		}
	]
}

Did I miss something?

I want all my files in this S3 bucket be served via CloudFront...

*** UPDATE ***

This cloud front guide says:

By default, your Amazon S3 bucket and all of the objects in it are private—only the AWS account that created the bucket has permission to read or write the objects in it. If you want to allow anyone to access the objects in your Amazon S3 bucket using CloudFront URLs, you must grant public read permissions to the objects. (This is one of the most common mistakes when working with CloudFront and Amazon S3. You must explicitly grant privileges to each object in an Amazon S3 bucket.)

So based on this I have added new permissions to all objects inside S3 bucket to Everyone Read/Download. Now I can access files.

But now when I access the file like https://d3u61axijg36on.cloudfront.net/css/style.css this is being redirected to S3 URI and HTTP. How do I disable this?

Amazon Web-Services Solutions


Solution 1 - Amazon Web-Services

To assist with your question, I recreated the situation via:

  • Created an Amazon S3 bucket with no Bucket Policy
  • Uploaded public.jpg and make it public via "Make Public"
  • Uploaded private.jpg and kept it private
  • Created an Amazon CloudFront web distribution:
  • Origin Domain Name: Selected my S3 bucket from the list
  • Restrict Bucket Access: Yes
  • Origin Access Identity: Create a New Identity
  • Grant Read Permissions on Bucket: Yes, Update Bucket Policy

I checked the bucket, and CloudFront had added a Bucket Policy similar to yours.

The distribution was marked as In Progress for a while. Once it said Enabled, I accessed the files via the xxx.cloudfront.net URL:

  • xxx.cloudfront.net/public.jpg redirected me to the S3 URL http://bucketname.s3.amazonaws.com/public.jpg. Yes, I could see the file, but it should not use a redirect.
  • xxx.cloudfront.net/private.jpg redirected me also, but I then received Access Denied because it is a private file in S3.

I then did some research and found that this is quite a common occurrence. Some people use a workaround by pointing their CloudFront distribution to the static hosted website URL, but this has the disadvantage that it will not work with the Origin Access Identity and I also suspect it won't receive the 'free S3 traffic to the edge' discount.

So, I waited overnight, tested it this morning and everything is working fine.

Bottom line: Even if it says ENABLED, things might take several hours (eg overnight) to get themselves right. It will then work as documented.

Solution 2 - Amazon Web-Services

I added index.html in Default Root Object under General tab of cloudFront Distribution Settings and it worked for me. As index.html was the root file for my project!

Solution 3 - Amazon Web-Services

Instead of choosing default s3 bucket for Origin Domain Name, please enter the <bucket-name>.s3-website.<region>.amazonaws.com as origin Domain Name(You can get this URL at Static website hosting property under S3 bucket properties).

Solution 4 - Amazon Web-Services

In my case I was using multiple origins with "Path Pattern" Behaviors along with an Origin Path in my S3 bucket:

Bad setup:

CloudFront Behavior: /images/* -> My-S3-origin

My-S3-origin: Origin Path: /images

S3 files: /images/my-image.jpg

GET Request: /images/my-image.jpg -> 403

What was happening was the entire CloudFront GET request gets sent to the origin: /image/my-image.jpg prefixed by Origin Path: /images, so the request into S3 looks like /images/images/my-image.jpg which doesn't exist.

Solution

remove Origin Path.

Solution 5 - Amazon Web-Services

This can happen if you are using a bucket that has just been newly created.

According official reply here: AWS Forun link, you have to wait for a couple of hours after creating a new bucket before you can have cloud front distribution working on it correctly.

Solution is to temporarily work from one of your old buckets and switch to the new bucket a couple of hours later.

Solution 6 - Amazon Web-Services

I also got 403 from CloudFront but my issue was a bit different so sharing it here as it might help others.

Make sure the Origin Access Id you've defined as part of the bucket policy is the right one:

{
    "Version": "2008-10-17",
    "Id": "PolicyForCloudFrontPrivateContent",
    "Statement": [
        {
            "Sid": "1",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity HERE_GOES_YOUR_ORIGIN_ACCESS_ID"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::deepen-frontend-prod/*"
        }
    ]
}

An Origin Access Identity Id roughly looks like E19F48VV5H01ZD.

You can find all of your origin access identities on https://console.aws.amazon.com/cloudfront/home#oai Look for the one you're using in your CloudFront config under Your Identities: enter image description here

Solution 7 - Amazon Web-Services

add this in Bucket policy

    {
        "Sid": "2",
        "Effect": "Allow",
        "Principal": {
            "AWS": "arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity ESDK2T2CSNT57"
        },
        "Action": "s3:GetObject",
        "Resource": "arn:aws:s3:::{Bucket name }/*"
    }

Solution 8 - Amazon Web-Services

Making the objects in the S3 bucket public-read is the fastest way to achieve this. However this is not recommended. Rather create a origin_access_identity under the s3 orgin policy and grant this identity access to the S3 bucket using a bucket policy. That way you can keep all the objects private.

Solution 9 - Amazon Web-Services

See 100% working solution...

It can be only solve at cloudFront panel by adding error response rule.

See Solution here - https://stackoverflow.com/questions/51218979/react-router-doesnt-work-in-aws-s3-bucket/71591815#71591815

Solution 10 - Amazon Web-Services

It worked for me once I added index.html to the Default Root Object under the General tab of cloudFront Distribution Settings.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPrimoz RomeView Question on Stackoverflow
Solution 1 - Amazon Web-ServicesJohn RotensteinView Answer on Stackoverflow
Solution 2 - Amazon Web-ServicesZohab AliView Answer on Stackoverflow
Solution 3 - Amazon Web-Servicesuday reddyView Answer on Stackoverflow
Solution 4 - Amazon Web-ServicesScott JungwirthView Answer on Stackoverflow
Solution 5 - Amazon Web-ServicesSureshView Answer on Stackoverflow
Solution 6 - Amazon Web-ServicesSaharView Answer on Stackoverflow
Solution 7 - Amazon Web-ServicesHosam ElzaghView Answer on Stackoverflow
Solution 8 - Amazon Web-ServicesMohit MuthaView Answer on Stackoverflow
Solution 9 - Amazon Web-ServicesKaushal SachanView Answer on Stackoverflow
Solution 10 - Amazon Web-Servicesmayur chavanView Answer on Stackoverflow