AWS Route53を使ったGithub Pagesのカスタムドメイン設定

# Prerequisite

自分でGithub Pagesを持っている

https://github.com/nakamasato/nakamasato.github.io/tree/ead78609599ff6de87a877cd2d6143e6f2304f11

自分のドメインをGithub Pagesに設定する

# 1. Domain購入

お名前ドットコムで買った。 

https://www.onamae.com/

nakamasato.com

# 2. Github PagesのDomain設定

https://docs.github.com/en/pages/configuring-a-custom-domain-for-your-github-pages-site/managing-a-custom-domain-for-your-github-pages-site

この中でDNSレコードの設定があるので、[AWSのRoute53](https://aws.amazon.com/route53/)を使用

# 3. AWSのRoute53でHostedZone作成 (Terraform)

<!---
1. Terraform Cloudで新しいWorkspace作成　（aws-route53）
1. Terraform Variablesに以下を設定
    1. `AWS_ACCESS_KEY_ID`
    1. `AWS_SECRET_ACCESS_KEY`
    1. `AWS_DEFAULT_REGION`
1. Terraform ユーザに以下の権限を付与

    ```hcl:terraform-cloud-policy.tf
      statement {
        sid    = "route53"
        effect = "Allow"
        actions = [
          "route53:*",
        ]
        resources = [
          "*",
        ]
      }
    ```

    `terraform apply` or applied by Terraform Cloud

--->


1. Route53のゾーンとレコードを作成


    Terraform の場合: 

    ```hcl:main.tf
    resource "aws_route53_zone" "nakamasato-com" {
      name    = "nakamasato.com"
      comment = "my domain"
      tags = {
        Environment = "test"
      }
    }

    resource "aws_route53_record" "about" {
      zone_id = aws_route53_zone.nakamasato-com.zone_id
      name    = "about"
      type    = "CNAME"
      ttl     = "5"
    
      records = ["nakamasato.github.io"]
    }
    ```


    cliの場合:

    ```
    aws route53 create-hosted-zone --name nakamasato.com --caller-reference 2021-10-24-12:10
    ```
    
    `--caller-reference` は失敗したときに同じRequestをRetryしてくれる用の uniqueなString


    ```json:route53.json
    {
      "Comment": "create record for about",
      "Changes": [
        {
          "Action": "CREATE",
          "ResourceRecordSet": {
            "Name": "about.nakamasato.com.",
            "Type": "CNAME",
            "TTL": 5,
            "ResourceRecords": [
              {
                "Value": "nakamasato.github.io"
              }
            ]
          }
        }
      ]
    }
    ```

    ```
    aws route53 change-resource-record-sets --hosted-zone-id ZXXXXXXXXXX --change-batch file://route53.json
    ```


1. Domain設定でネームサーバをdns1.onamae.comからroute53のネームサーバへ切り替える
    ![Screen Shot 2021-06-20 at 17.49.05.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/7059/9f9fecaa-9980-b3a6-ae7c-77391fcd557c.png)

1. 確認

    ```bash
    nslookup -type=ns nakamasato.com
    Server:         192.168.3.1
    Address:        192.168.3.1#53

    Non-authoritative answer:
    nakamasato.com  nameserver = ns-437.awsdns-54.com.
    nakamasato.com  nameserver = ns-1430.awsdns-50.org.
    nakamasato.com  nameserver = ns-2010.awsdns-59.co.uk.
    nakamasato.com  nameserver = ns-696.awsdns-23.net.

    Authoritative answers can be found from:
    ns-437.awsdns-54.com    internet address = 205.251.193.181
    ns-696.awsdns-23.net    internet address = 205.251.194.184
    ns-1430.awsdns-50.org   internet address = 205.251.197.150
    ns-2010.awsdns-59.co.uk internet address = 205.251.199.218
    ```

1. `about.nakamasato.com` を Github Pagesに設定 + Enforce HTTPS をクリック

    ![スクリーンショット 2021-10-24 12.33.01.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/7059/3998104c-b587-1ee9-4ea0-7a924cab96c8.png)


1. https://about.nakamasato.com
    ![Screen Shot 2021-06-20 at 17.48.22.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/7059/98c24da2-3221-4fa3-984a-d8e80b3c34eb.png)

# Route53の料金
個人で使う場合はTrafficも少ないと思うので、以下の２つで良さそうなので、月1ドルくらいを想定
- 0.50USD ホストゾーンごと /月
- 0.40USD 100 万クエリごと


# 参考

- https://deanattali.com/blog/multiple-github-pages-domains/
- https://qiita.com/ysKey2/items/0545e13ec05def42ad55
- https://aws.amazon.com/jp/route53/pricing/