Static Hosting With S3 via CLI
Contents
The AWS console is great, but of course can be cumbersome. I always try to figure out how to do things in AWS via CLI, as that eventually will lead to automation and being able to replicate your work in the future more easily.
Below I will outline how I created this site using Hugo, S3, and CloudFront
Caveat: THE BUCKET NAME MUST BE YOUR DOMAIN NAME
1. Create a bucket
s3api create-bucket --bucket computerbryan.com --acl private --region us-west-2 --create-bucket-configuration LocationConstraint=us-west-2
2. Create bucket for logs
s3api create-bucket --bucket computerbryan-hugo-logs --acl private --region us-west-2 --create-bucket-configuration LocationConstraint=us-west-2
3. Set log delivery
s3api put-bucket-acl --bucket computerbryan.com --grant-read URI=http://acs.amazonaws.com/groups/s3/LogDelivery
s3api put-bucket-acl --bucket computerbryan.com --grant-read URI=http://acs.amazonaws.com/groups/s3/LogDelivery
s3api put-bucket-acl --bucket computerbryan.com --grant-write URI=http://acs.amazonaws.com/groups/s3/LogDelivery --grant-read URI=http://acs.amazonaws.com/groups/s3/LogDelivery --grant-read-acp URI=http://acs.amazonaws.com/groups/s3/LogDelivery
4. Set bucket to be a website
s3 website s3://dynaop.com --index-document index.html --error-document error.html
** Optional** If you had more complex routing and configuration to do you could also do it this way:
{
"IndexDocument": {
"Suffix": "index.html"
},
"ErrorDocument": {
"Key": "404.html"
},
"RoutingRules": [
{
"Redirect": {
"ReplaceKeyWith": "index.html"
},
"Condition": {
"KeyPrefixEquals": "/"
}
}
]
}
s3api put-bucket-website --bucket dynaop.com --website-configuration file://website.json
5. Create json for bucket policy, policy.json
{
"Id": "Policy1534744389938",
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1534744358891",
"Action": [
"s3:GetObject"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::computerbryan.com/*",
"Principal": "*"
},
{
"Sid": "Stmt1534744386030",
"Action": [
"s3:ListBucket"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::computerbryan.com",
"Principal": "*"
}
]
}
Load Policy
s3api put-bucket-policy --bucket dynaop.com --policy file://policy.json
Note this policy opens everything up in the bucket! Don’t put anything in here you don’t want the world seeing!
Other note: you must go to the bucket’s domain name for index to resolve, ie: http://computerbryan.com.s3-website-us-west-2.amazonaws.com/
This policy will allow you to access the bucket from the AWS console as well.
Setup DNS and create route53.json
{
"Comment":"Updates CNAME to a specified value",
"Changes":[{
"Action":"UPSERT",
"ResourceRecordSet":{
"Name":"computerbryan.com",
"Type":"A",
"AliasTarget":{
"HostedZoneId": "Z3BJ6K6RIION7M",
"DNSName":"s3-website-us-west-2.amazonaws.com",
"EvaluateTargetHealth" : false
}
}
},
{
"Action":"UPSERT",
"ResourceRecordSet":{
"Name":"www.computerbryan.com",
"Type":"A",
"AliasTarget":{
"HostedZoneId": "Z3BJ6K6RIION7M",
"DNSName":"s3-website-us-west-2.amazonaws.com",
"EvaluateTargetHealth" : false
}
}
}
]
}
route53 change-resource-record-sets --hosted-zone-id ZB628JLAKPCI4 --change-batch file://dns.json
Sync to BUCKET
s3 sync --delete ./public s3://computerbryan.com --acl public-read
I will eventually update this with how to connect CloudFront for CDN. Enjoy!
Author BPT
LastMod 2018-08-20
License 2018 Bryan P. Taylor