1: Create and configure s3 bucket
Setting up an s3 bucket
So.. I want to set up an S3 bucket to host the pubgolf static website.
I have the AWS cli set up on my laptop in wsl2 as the laptop is running Windows 10.
So to create the pubgolf bucket I can run this command:
aws s3 mb s3://pubgolf.co.uk
aws s3 mb s3://www.pubgolf.co.uk
This has created two buckets that match the domain name.
Set up static hosting on the bucket
- setup a json file for the configuration option. this lets us specify what files are used. ie index.htm and error.htm
{ "IndexDocument": { "Suffix": "index.htm" }, "ErrorDocument": { "Key": "error.htm" } }
The default is normally .html but I origionally set up the site to be .htm
- use the s3api to setup the bucket
aws s3api put-bucket-website --bucket pubgolf.co.uk --website-configuration file://website.json
The main bucket is now set up to use static hosting.
- set up public access
aws s3api put-public-access-block \ --bucket pubgolf.co.uk \ --public-access-block-configuration "BlockPublicAcls=false,IgnorePublicAcls=false,BlockPublicPolicy=false,RestrictPublicBuckets=false"
- setup bucket policy
s3-static-host.json
{ "Version": "2012-10-17", "Statement": [ { "Sid": "PublicReadGetObject", "Effect": "Allow", "Principal": "*", "Action": [ "s3:GetObject" ], "Resource": [ "arn:aws:s3:::pubgolf.co.uk/*" ] } ] }
then attach this to the bucket
aws s3api put-bucket-policy --bucket pubgolf.co.uk --policy file://s3-static-host.json
The main bucket is now configured.
- setup www.pubgolf.co.uk to redirect to pubgolf.co.uk bucket
Create redirect policy redirect.json
{
"RedirectAllRequestsTo": {
"HostName": "pubgolf.co.uk"
}
}
Then run the command to apply the policy to the www.pubgolf.co.uk bucket.
aws s3api put-bucket-website --bucket www.pubgolf.co.uk --website-configuration file://redirect.json
The S3 buckets are now configured correctly in AWS but don’t have any files.
Upload files to the s3 bucket
I have the source files locally on my machine for the live pubgolf site.
I’m going to use the command
aws s3 sync source s3://pubgolf.co.uk
Opening the s3 bucket in a webbrowser I get the pubgolf website bucket showing files.