Skip to content

Instantly share code, notes, and snippets.

@kaorimatz
Last active August 29, 2015 14:05
Show Gist options
  • Save kaorimatz/ba3e1776cdfe89ac3672 to your computer and use it in GitHub Desktop.
Save kaorimatz/ba3e1776cdfe89ac3672 to your computer and use it in GitHub Desktop.
#!/bin/bash
# usage: ./create-install-domain.sh <github_repo> <domain>
# example: ./create-install-domain.sh kaorimatz/dotfiles dot.kaorimatz.org
set -e
set -u
S3_REGION=ap-northeast-1
S3_HOSTED_ZONE_ID=Z2M4EHUR26P7ZW
S3_WEBSITE_ENDPOINT=s3-website-ap-northeast-1.amazonaws.com
github_repo=$1
domain=$2
# Create an empty bucket
aws s3api create-bucket --bucket "$domain" --create-bucket-configuration LocationConstraint="$S3_REGION"
# Apply a routing rule which redirects to your install script
# https://docs.aws.amazon.com/AmazonS3/latest/dev/HowDoIWebsiteConfiguration.html
aws s3api put-bucket-website --region "$S3_REGION" --bucket "$domain" --website-configuration "$(cat <<EOF
{
"IndexDocument": {
"Suffix": "index.html"
},
"RoutingRules": [
{
"Redirect": {
"ReplaceKeyWith": "$github_repo/master/install.sh",
"HostName": "raw.githubusercontent.com",
"Protocol": "https",
"HttpRedirectCode": "301"
}
}
]
}
EOF
)"
# Get the hosted zone id of Route53
hosted_zone_name=$(echo "$domain" | awk -F '.' '{ printf "%s.%s.", $(NF-1), $NF }')
hosted_zone_id=$(aws route53 list-hosted-zones --output text --query "HostedZones[?Name==\`$hosted_zone_name\`].Id")
# Create A record pointing to the created bucket
aws route53 change-resource-record-sets --hosted-zone-id "$hosted_zone_id" --change-batch "$(cat <<EOF
{
"Changes": [
{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "$domain.",
"Type": "A",
"AliasTarget": {
"HostedZoneId": "$S3_HOSTED_ZONE_ID",
"DNSName": "$S3_WEBSITE_ENDPOINT.",
"EvaluateTargetHealth": false
}
}
}
]
}
EOF
)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment