こんにちは。AWS CLIが好きな福島です。
はじめに
今回はRoute 53の委任セットを使い、サブドメインを委任したため、その方法をご紹介します。
※委任セットの利用はマネジメントコンソールからはできなく、AWS CLIなどを使う必要があります。
委任セットについては、以下のブログをご参照ください。
【Route 53】ドメインを委任する際に便利な委任セットについて - サーバーワークスエンジニアブログ
利用するコマンド,サブコマンド
まず、AWS CLIの構造は以下の通りです。
aws <command> <subcommand> [options and parameters]
上記を前提に今回使う <command>,<subcommand>
は、以下の通りです。
<command>
route53
<subcommand>
* list-reusable-delegation-sets ⇒委任セットのリストを出力するコマンド * create-reusable-delegation-set ⇒委任セットを作るコマンド * create-hosted-zone ⇒ホストゾーンを作成するコマンド * change-resource-record-sets ⇒レコードを更新するコマンド
やること
今回は、example.comのサブドメイン([dev|stg].example.com)をアカウントBに委任する想定でやってみます。
また、やることは
①アカウントBでホストゾーンを作成
②アカウントAのexample.comのゾーンにNSレコードを追加
です。
手順
アカウントBに対する操作実施
①委任セットの作成
- コマンド
aws route53 create-reusable-delegation-set \ --caller-reference `date +"%Y-%m-%d-%H-%M-%S"`
※--caller-referenceの値は、任意の値になりますが、今回は作成した日付を指定しています。
- 実行例
# aws route53 create-reusable-delegation-set \ --caller-reference `date +"%Y-%m-%d-%H-%M-%S"` { "Location": "https://route53.amazonaws.com/2013-04-01/delegationset/N013183735EMVZ1OKH7ZD", "DelegationSet": { "Id": "/delegationset/N013183735EMVZ1OKH7ZD", "CallerReference": "2022-03-29-16-20-58", "NameServers": [ "ns1.example.com", "ns2.example.com", "ns3.example.com", "ns4.example.com." ] } } #
②委任セットの確認
- コマンド
aws route53 list-reusable-delegation-sets
- 実行例
# aws route53 list-reusable-delegation-sets { "DelegationSets": [ { "Id": "/delegationset/N013183735EMVZ1OKH7ZD", "CallerReference": "2022-03-29-16-20-58", "NameServers": [ "ns1.example.com", "ns2.example.com", "ns3.example.com", "ns4.example.com." ] } ], "IsTruncated": false, "MaxItems": "100" } #
③委任セットを使ったホストゾーンの作成
- コマンド
delegation_set_id="委任セットのIDを指定します。" hostzone_name="作成するホストゾーン名を指定します。"
aws route53 create-hosted-zone \ --delegation-set-id ${delegation_set_id} \ --name ${hostzone_name} \ --caller-reference `date +"%Y-%m-%d-%H-%M-%S"`
※--caller-referenceの値は、任意の値になりますが、今回は作成した日付を指定しています。
- 実行例
delegation_set_id="/delegationset/N013183735EMVZ1OKH7ZD" dev_hostzone_name="dev.example.com" stg_hostzone_name="stg.example.com"
- dev.example.com用
# aws route53 create-hosted-zone \ --delegation-set-id ${delegation_set_id} \ --name ${dev_hostzone_name} \ --caller-reference `date +"%Y-%m-%d-%H-%M-%S"` { "Location": "https://route53.amazonaws.com/2013-04-01/hostedzone/Z06585782GPJ56W5YRTAF", "HostedZone": { "Id": "/hostedzone/Z06585782GPJ56W5YRTAF", "Name": "dev.example.com.", "CallerReference": "2022-03-29-17-11-05", "Config": { "PrivateZone": false }, "ResourceRecordSetCount": 2 }, "ChangeInfo": { "Id": "/change/C06722572BOY2LGRKW9NC", "Status": "PENDING", "SubmittedAt": "2022-03-29T08:11:10.312000+00:00" }, "DelegationSet": { "Id": "/delegationset/N013183735EMVZ1OKH7ZD", "CallerReference": "2022-03-29-16-20-58", "NameServers": [ "ns1.example.com", "ns2.example.com", "ns3.example.com", "ns4.example.com." ] } }
- stg.example.com用
# aws route53 create-hosted-zone \ --delegation-set-id ${delegation_set_id} \ --name ${stg_hostzone_name} \ --caller-reference `date +"%Y-%m-%d-%H-%M-%S"` { "Location": "https://route53.amazonaws.com/2013-04-01/hostedzone/Z01879773IHGKDTKC5SGZ", "HostedZone": { "Id": "/hostedzone/Z01879773IHGKDTKC5SGZ", "Name": "stg.example.com.", "CallerReference": "2022-03-29-17-11-32", "Config": { "PrivateZone": false }, "ResourceRecordSetCount": 2 }, "ChangeInfo": { "Id": "/change/C039067734UN1NIDJYMVO", "Status": "PENDING", "SubmittedAt": "2022-03-29T08:11:37.006000+00:00" }, "DelegationSet": { "Id": "/delegationset/N013183735EMVZ1OKH7ZD", "CallerReference": "2022-03-29-16-20-58", "NameServers": [ "ns1.example.com", "ns2.example.com", "ns3.example.com", "ns4.example.com." ] } }
アカウントAに対する操作
④NSレコードの登録の準備
本作業はマネジメントコンソールから実施可能ですが、今回はAWS CLIから実施します。
登録するレコードを任意のファイルに記載します。
- change-resource-record-sets.txt
{ "Changes" : [ { "Action":"CREATE", "ResourceRecordSet":{ "Name": "dev.example.com", "Type": "NS", "TTL": 172800, "ResourceRecords": [ { "Value": "ns1.example.com." }, { "Value": "ns2.example.com." }, { "Value": "ns3.example.com." }, { "Value": "ns4.example.com." } ] } }, { "Action":"CREATE", "ResourceRecordSet":{ "Name": "stg.example.com", "Type": "NS", "TTL": 172800, "ResourceRecords": [ { "Value": "ns1.example.com." }, { "Value": "ns2.example.com." }, { "Value": "ns3.example.com." }, { "Value": "ns4.example.com." } ] } } ] }
⑤NSレコードの登録
- コマンド
host_zone_id="ホストゾーンのIDを指定します。" file_name="④で作成したファイル名を指定します。"
aws route53 change-resource-record-sets \ --hosted-zone-id ${host_zone_id} \ --change-batch file://${file_name}
- 実行例
host_zone_id="Z06572723NPNMW8P6UN9G" file_name="change-resource-record-sets.txt"
# aws route53 change-resource-record-sets \ --hosted-zone-id ${host_zone_id} \ --change-batch file://${file_name} { "ChangeInfo": { "Id": "/change/C03608951ASY1V0JA4MPL", "Status": "PENDING", "SubmittedAt": "2022-03-29T08:10:01.324000+00:00" } }
⑥動作確認
[dev|stg].example.comのネームサーバを確認し、委任セットと同様のネームサーバになっていれば、無事にサブドメインを委任できています。
- コマンド
dig -t NS dev.example.com +short dig -t NS stg.example.com +short
- 実行例
# dig -t NS dev.example.com +short ns1.example.com ns2.example.com ns3.example.com ns4.example.com # dig -t NS stg.example.com +short ns1.example.com ns2.example.com ns3.example.com ns4.example.com #
終わりに
今回は、Route 53を使い、サブドメインを委任してみました。 どなかたのお役に立てれば幸いです。
福島 和弥 (記事一覧)
2019/10 入社
AWS CLIが好きです。
AWS資格12冠。2023 Japan AWS Partner Ambassador/APN ALL AWS Certifications Engineer。