Skip to content

Cloudfront cannot access s3 bucket

0

I'm trying to host a simple website from an S3 bucket with a unique domain name through SSL. I can access the site properly when I go through the S3 url <bucketname>.s3-website-us-east-1.amazonaws.com. but when I try to access it from the domain I get the following error

403 ERROR The request could not be satisfied. Bad request. We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner. If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation. Generated by cloudfront (CloudFront) Request ID: uHNAwyDpZLtFr85DBpchktobmdV-2KfVwEpdw8a-xFGRFIXPgoFDpg==

I have the origin domain set up as <bucketname>.s3.us-east-1.amazonaws.com and the S3 bucket policy looks like this

{ "Version": "2008-10-17", "Id": "PolicyForCloudFrontPrivateContent", "Statement": [ { "Sid": "PublicReadGetObject", "Effect": "Allow", "Principal": "", "Action": "s3:GetObject", "Resource": "arn:aws:s3:::<bucketname>/" }, { "Sid": "AllowCloudFrontServicePrincipal", "Effect": "Allow", "Principal": { "Service": "cloudfront.amazonaws.com" }, "Action": "s3:GetObject", "Resource": "arn:aws:s3:::<bucketname>/*", "Condition": { "StringEquals": { "AWS:SourceArn": "arn:aws:cloudfront::<USERID>:distribution/<DISRIBUTIONID>" } } } ] }

I have an OAC for S3 set up.

I have an SSL cert through ACM that I've created records for the domain in route 53.

my default behaviors on cloudfront are to Redirect HTTP to HTTPS allowed methods of GET, HEAD, OPTIONS, PUT, POST, PATCH, DELETE no Restrict viewer access caching optimized cahe policy request policy of Managed-UserAgentRefererHeaders a custom cors policy

I have allowed public access on the bucket and static web hosting. I've also made the files in the bucket public through the ACL

I've cleared my browser cache, tried running in a new browser, tried running in a new incognio tab all produce the same result of a 403 error. What can I do to resolve this?

asked 2 years ago660 views

2 Answers
1

You can’t use OAC when the S3 bucket is configured as a website (custom) origin. The S3 bucket in cloudfront has to be configured as a regular S3 bucket.

  • If your origin is an Amazon S3 bucket configured as a website endpoint, you must set it up with CloudFront as a custom origin. That means you can't use OAC (or OAI).

https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-restricting-access-to-s3.html

EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

0
Accepted Answer

Cloudfront doesn't work with static web host S3 endpoints, meaning that your endpoint should look like this <bucketname>.s3.us-east-1.amazonaws.com.

In fact you can disable static website hosting. In your bucket access use this configuration bucket access And in the bucket ACL write this policy

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::<bucketname>/*"
        }
    ]
}

Object Ownership should be Bucket owner enforced

After that in you Cloudfront distribution Your origins should look like this with the s3 endpoint (OAC) origins In you Behaviors Enter image description here Finally in the settings of your distribution you should add Alternate domain name (CNAME) - optional (the one you have registered at Route53 which is your domain it has to be of type CNAME) And in custom ssl you should load your ACM certificate of the domain you want

Be sure to register in Route53 the distribution link that Cloudfront gives you (you can also access to your content with this link if you dont have anything related to CORS that might block your content)

EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

  • The bucket policy statement allowing everyone, including completely authenticated users, to read from the bucket, as well as advice about disabling two of the Block Public Access settings for the bucket is not the way OAC is configured. The policy statement AllowCloudFrontServicePrincipal in the original statement is correct, and when it is in effect, all the Block Public Access options can and should be selected for the bucket, and is advisable to do also at the account level, applying to all buckets. CloudFront will use OAC for authenticated (not anonymous) access to the bucket.

  • Leo is correct. This bucket policy is not least privileged and not limited to cloudfront

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.