Amplify "add hosting" not setting "Restrict Bucket Access" in Cloudfront
Adding hosting via amplify add hosting while selecting the PROD environment (i.e. with Cloudfront enabled) will lead to an AccessDenied error when accessing the published website via the Cloudfront URL.
Manually setting the Restrict Bucket Access to Yes in the Cloudfront panel solves the issue. See also https://stackoverflow.com/questions/59822923/amplify-publish-causes-accessdenied-error/59839507#59839507 .
Steps to reproduce the behavior:
- Create a simple website
amplify initamplify add hosting- Select
PROD amplify pushandamplify publish- Navigate to the Cloudfront URL given by the
amplify publishcommand output - See an
AccessDeniederror in the browser
To Continue:
- Check the Cloudfront setting ->
Origin and Origin Groups - Select the only
Originand clickEdit - See
Restrict Bucket Accessset toNo
Actual behavior
AccessDenied error trying to access the website via the Cloudfront URL.
Expected behavior Website being accessible via the Cloudfront URL.
Amplify CLI Version 4.12.0
Environment
npx: Installierte 1 in 2.328s
System:
OS: macOS 10.15.2
CPU: (4) x64 Intel(R) Core(TM) i5-6287U CPU @ 3.10GHz
Memory: 259.23 MB / 16.00 GB
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 13.3.0 - ~/.nvm/versions/node/v13.3.0/bin/node
Yarn: 1.21.1 - /usr/local/bin/yarn
npm: 6.13.1 - ~/.nvm/versions/node/v13.3.0/bin/npm
Watchman: 4.7.0 - /usr/local/bin/watchman
Browsers:
Chrome: 79.0.3945.117
Chrome Canary: 81.0.4026.0
Firefox: 55.0.3
Safari: 13.0.4
npmPackages:
@quasar/app: ^1.0.0 => 1.5.2
@quasar/extras: ^1.0.0 => 1.4.2
@vue/eslint-config-standard: ^4.0.0 => 4.0.0
aws-amplify: ^2.2.2 => 2.2.2
aws-amplify-vue: ^1.1.2 => 1.1.2
aws-appsync: ^3.0.2 => 3.0.2
axios: ^0.18.1 => 0.18.1
babel-eslint: ^10.0.1 => 10.0.3
eslint: ^5.10.0 => 5.16.0
eslint-loader: ^2.1.1 => 2.2.1
eslint-plugin-vue: ^5.0.0 => 5.2.3
graphql: ^14.5.8 => 14.5.8
graphql-tag: ^2.10.1 => 2.10.1
quasar: ^1.0.0 => 1.7.4
vue-apollo: ^3.0.2 => 3.0.2
npmGlobalPackages:
npm: 6.13.1
(Not) Related
Although #611 gives the same error, but it didn't solve my problem. My website is deployed to the eu-central-1 region, but adding the region to the origin or waiting for 24h didn't help. Only setting the Restrict Bucket Access option (as mentioned above) solved my problem.
My Thoughts
As manually setting the Restrict Bucket Access to Yes in Cloudfront solved the AccessDenied error, I think amplify add hosting should automatically set this option to Yes while deploying a website.
@morgler When you see the access denied message is the url that of the bucket or the cloudfront url? Reference Issue below: https://github.com/aws-amplify/amplify-cli/pull/2369
We did have a change which will only allow access to the provided cloudfront url and this make take time to set up, as initially it will redirect to the bucket is denied access.
@SwaySway The url is the cloudfront url. Of course right after the first publish, it redirected me to the s3 bucket url, which also gave the AccessDenied error. But after some time it stayed on the cloudfront url (also accessed it from different browsers and devices).
I understand that it is expected to see AccessDenied if visiting the S3 bucket url directly, because preventing direct access to the files is one reason to use cloudfront in the first place. But it seems that also cloudfront itself had no access rights to the S3 bucket. Setting the Restrict Bucket Access to Yes in cloudfront seems to somehow have enabled the S3 access for cloudfront (just wild guessing!).
@morgler
When you use Amplify CLI to setup the CloudFront distribution for hosting, the Restrict Bucket Access is already set to yes.
You can verified this by creating a new Amplify Project, add hosting (select PROD config), and check the CloudFront after the amplify push is complete.
The "Access Denied" error can be there for a long time when the CloudFront distribution is first setup or when you publish an update with new webpages, it is because it can take a long time for the CloudFront edges to build up the caches.
Just hit this...not great behaviour. Given the answer here is wait some time, how are people doing graceful deployments? I absolutely cannot have my services report 'Access Denied' errors for up to 24hrs on every new publish...
edit: I get it, the delay is only on the first publish, every thing else is hashed on content. BTW it looks like all of the hosting docs have gone missing or have regressed. Someone should have a look into that, it's much easier to read docs in a few minutes rather than wasting hours trying to guess what's going on.
One other point, that would be nice to see in the docs somewhere, is a note about cloudfront cache invalidation on subsequent pushes. 'amplify publish -c' is your friend.
@dconlon-monigo I would recommend using Amplify console for your hosting needs to avoid this behavior. Amplify console has a warmed up Cloudfront pool - to avoid wait period like this one. Please take a look at our documentation out here - https://aws.amazon.com/amplify/console/
Also, we have our documentation around hosting out here - https://aws-amplify.github.io/docs/cli-toolchain/quickstart#hosting
This issue not resolved. please reopen
@2manoj1
Could you send us your CloudFront Distribution id through email? just to help us to troubleshoot it. [email protected]
@UnleashedMind problem resolved for me.
This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.
Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.