To create the bucket and enable static web hosting:
- Go to the S3 console (via searching "S3" in the top search bar)
- Click "Create bucket"
- Name the bucket whatever unique name you like - bucket names are globally unique. Example: fit3170-chloe
- Leave the "ACLs disabled" setting as default
- Untick the "block all public access" checkbox
- Tick "I acknowledge"
- Leave the other settings as default and click "create bucket"
- Click the bucket you just created
- Click "properties"
- Scroll to the bottom where it says "static website hosting"
- Click "edit" then select "enable"
- Type "index.html" for the index document
- Leave the other values as default
- On the properties panel, the “static website hosting” should now have a “bucket website endpoint”. Click this to open this in a new tab. We haven't uploaded the web app or properly set permissions yet so you'll get a 403 error - but keep this tab open as we'll use it later.
Next, change the bucket policy to allow access to all objects:
- Go to the permissions tab in the bucket -> bucket policy -> edit
- Paste the following, replacing "example-bucket" with your bucket name:
{ "Version":"2012-10-17", "Statement":[{ "Sid":"PublicReadGetObject", "Effect":"Allow", "Principal": "*", "Action":["s3:GetObject"], "Resource":["arn:aws:s3:::example-bucket/*"] }] }
Make sure to leave the "/*" at the end of the bucket name. Save the policy.
Then, upload the front end to the bucket for hosting:
- Go back to your front end shell session
- Stop the front end from running if it's running (ctrl+C)
- Run
aws s3 cp --recursive build s3://example-bucket
, replacingexample-bucket
with your bucket name. This will upload thebuild
folder to S3. - Go to the page where you opened the static website before and refresh it - it should work now!
Cool - so now we have the back end running on EC2 still, and saving things to a local JSON file, while the front end is statically hosted on S3. Nice! Next up is to improve reliability by storing data on a remote database instead of just in a JSON file - so that if our server dies, we can just replace it with another one that connects to the same database, and no data will be lost!