7
votes

I deploy a simple web app to S3 via amplify publish. The hosting has Cloudfront enabled (I selected the PROD environment in amplify while setting up hosting) and I'm working in the eu-central-1 region. But whenever I try to access the Cloudfront URL, I receive an AccessDenied error.

I followed a tutorial at https://medium.com/quasar-framework/creating-a-quasar-framework-application-with-aws-amplify-services-part-1-4-9a795f38e16d an the only thing I did differently was the region (tutorial uses us-east-1 while I use eu-central-1).

The config of S3 and Cloudfront was done by amplify and so should be working in theory:

Cloudfront:

  • Origin Domain Name or Path: quasar-demo-hosting-bucket-dev.s3-eu-central-1.amazonaws.com (originally it was without the eu-central-1, but I added it manually after it didn't work).
  • Origin ID: hostingS3Bucket
  • Origin Type: S3 Origin

S3 Bucket Policy:

{
    "Version": "2012-10-17",
    "Id": "MyPolicy",
    "Statement": [
        {
            "Sid": "APIReadForGetBucketObjects",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity ********"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::quasar-demo-hosting-bucket-dev/*"
        }
    ]
}

Research showed me that Cloudfront can have temporary trouble to access S3 buckets in other regions. But I manually added the region to the origin in Cloudfront AND I have waited for 24h. I still get the "access denied".

I suspect this has something to do with the S3 bucket not being in the default us-east-1 region and amplify not setting up Cloudfront correctly in that case.

How can I get amplify to set the S3 bucket and Cloudfront up correctly so that I can access my website through the Cloudfront URL?

3
could you please share your configuration of S3 and Cloudfront distribution.Raj Paliwal
I added the information to the question @RajPaliwal. Everything was automatically configured that way via amplify. The only thing I changed (after it didn't work) was to add the eu-central-1 part to the S3 path configured in Cloudfront.morgler

3 Answers

5
votes

Thanks for the additional information.

your S3 Bucket Policy looks Ok.

  • Regarding Origin Domain name or Path, It is always S3 bucket appears in the drop down so no need to update it with region

  • However there is one setting missing in your Cloudfront Origin.
    you need to select Restrict Bucket access to Yes

    As per AWS documentation
    If you want to require that users always access your Amazon S3 content using CloudFront URLs, not Amazon S3 URLs, click Yes. This is useful when you are using signed URLs or signed cookies to restrict access to your content. In the Help, see "Serving Private Content through CloudFront

  • Now create new Identity or select Existing Identity

  • Click on Create button to save Origin.

enter image description here

8
votes

For those whom the first solution does not work, also make sure that the javascript.config.DistributionDir in your project-config.json file is configured correctly. That can also cause the AccessDenied error (as I just learned the hard way).

Amplify expects your app entrypoint (index.html) to be at the first level within the directory you have configured. So if you accept the amplify default config (dist) and are using a project that puts the built files at a deeper level in the hierarchy (dist/<project name> in the case of angular 8), then it manifests as a 403 AccessDenied error after publishing. This is true of both the amplify and s3 hosting options.

docs: https://docs.aws.amazon.com/amplify/latest/userguide/manual-deploys.html (see the end)

3
votes

While the answer by @raj-paliwal helped me tremendously solving my original problem, Amplify has since fixed the problem with a new option.

If you type Amplify add hosting (or Amplify update hosting for an existing site), Amplify gives you the option of Hosting with Amplify Console.

enter image description here

Choosing this will also create a hosting environment with S3 and CloudFront, but Amplify will manage everything for you. With this option I had no problems at all. It seems that this first option fixes the bug I encountered.

If you want to upgrade an existing site from manual CloudFront and S3 hosting to a Hosting with Amplify Console, you have to call amplify update hosting and select the new option.