I am trying to build a website which the main area doesn't need authentication. But the subdomain requires user to login. I am trying to achieve it by using CloudFront stands in front of two S3 buckets. I have a lambda function I obtained online which would be inserted into one of the behavior.
As for the s3, I created two buckets, one is www.xxx.com.s3.amazonaws.com and the second one is www.xxx.com.relj.s3-website-us-east-1.amazonaws.com. Static website hosting are enabled on both buckets. Disabled Public access, and with bucket policy for main bucket is
{ "Version": "2008-10-17", "Id": "PolicyForCloudFrontPrivateContent", "Statement": [ { "Sid": "1", "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity EXXXCD2PW2IQU" }, "Action": "s3:GetObject", "Resource": "arn:aws:s3:::www.xxx.com/*" } ] }
Bucket policy for the other bucket is
{ "Version": "2008-10-17", "Id": "PolicyForCloudFrontPrivateContent", "Statement": [ { "Sid": "1", "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity EXXXCD2PW2IQU" }, "Action": "s3:GetObject", "Resource": "arn:aws:s3:::www.xxx.com.relj/*" } ] }
For my cloud front setup, I have two origin, www.xxx.com.s3.amazonaws.com and www.xxx.com.relj.s3-website-us-east-1.amazonaws.com. I setup three behavior, one is default, the other one is /, and the last one is relj/. Last one is pointing to www.xxx.com.relj.s3-website-us-east-1.amazonaws.com with lambda function attached.
when I access the main site, dXXjxu7k28es7y.cloudfront.net, I can see my website. But when I do dXXjxu7k28es7y.cloudfront.net/relj/index.html, I see the login prompt. After I put in the correct user name and password, it gives me 403. I tried even putting everything in relj bucket as public, I still can't access it through dXXjxu7k28es7y.cloudfront.net/relj/index.html. I am able to access it from https://s3.amazonaws.com/www.xxx.com.relj/index.html.
The response is
403, Forbidden date: Sat, 27 Jan 2018 01:15:07 GMT x-amz-error-code: AccessDenied last-modified: Fri, 26 Jan 2018 22:23:50 GMT server: AmazonS3 etag: "b5aa4b118fdf2980dd7e4d7d81db9a08" x-amz-error-message: Access Denied content-type: text/html via: 1.1 bdfe34c94134f86b07ebb7714d12d095.cloudfront.net (CloudFront) x-cache: Error from cloudfront connection: keep-alive content-length: 51 x-amz-cf-id: hRebSOcNA38KGcXkA2LrzXftvqxaSB7ffVqbqcrWj0_2rQua9aQAkA== I am new to aws so all these policies are new to me. From what I have seen, the lambda executed so it's reaching the url. I can provide more info. Thanks for reading.
Server: Amazon S3, thex-amz-error-*and atext/html(not XML) error content indicate the 403 originates at an S3 web site endpoint. Exactly how to fix it will depend on exactly what you need to accomplish.relj/index.htmlor justindex.html?