Azure DNS のワイルドカードレコードを試してみた

Apache や Nginx で SNI を使って FQDN ごとコンテンツを出し分けたり、リバースプロキシ用途で SNI ごとにそれぞれのバックエンドサーバーに転送したりしています。例えば SaaS アプリを提供する側として、サブドメインを顧客領域として提供する場合、毎回サブドメインを追加するのは面倒なので、今回試してみた Azure DNS のワイルドカードレコードを使って楽をしようと思います。

## 例えば Google DNS の 正引き

```bash:bash
$ host dns.google
dns.google has address 8.8.8.8
dns.google has address 8.8.4.4
dns.google has IPv6 address 2001:4860:4860::8844
dns.google has IPv6 address 2001:4860:4860::8888
```

## Google DNS に Ping を試す

```bash:bash
$ ping -c 4 dns.google
PING dns.google (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: icmp_seq=0 ttl=116 time=9.174 ms
64 bytes from 8.8.8.8: icmp_seq=1 ttl=116 time=15.317 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=116 time=16.383 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=116 time=13.862 ms

--- dns.google ping statistics ---
4 packets transmitted, 4 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 9.174/13.684/16.383/2.753 ms
```

## Azure DNS に CNAME でワイルドカードレコードを登録

```bash:bash
$ az network dns record-set cname set-record \
  --resource-group mnrlabo-rg \
  --zone-name mnrsdev.com \
  --record-set-name "*.ex" \
  --cname dns.google
```

## Azure ポータルでワイルドカードレコードを確認

![azure-dns-wildcard-01.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/675885/ba610d32-b573-6274-83e0-2415ce3632a0.png)

## 例えば aaa というサブドメインの場合

```bash:bash
$ host aaa.ex.mnrsdev.com
aaa.ex.mnrsdev.com is an alias for dns.google.
dns.google has address 8.8.8.8
dns.google has address 8.8.4.4
dns.google has IPv6 address 2001:4860:4860::8844
dns.google has IPv6 address 2001:4860:4860::8888

$ ping -c 4 aaa.ex.mnrsdev.com
PING dns.google (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: icmp_seq=0 ttl=116 time=9.542 ms
64 bytes from 8.8.8.8: icmp_seq=1 ttl=116 time=15.680 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=116 time=7.332 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=116 time=12.921 ms

--- dns.google ping statistics ---
4 packets transmitted, 4 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 7.332/11.369/15.680/3.187 ms
```

## 例えば bbb というサブドメインの場合

```bash:bash
$ host bbb.ex.mnrsdev.com
bbb.ex.mnrsdev.com is an alias for dns.google.
dns.google has address 8.8.4.4
dns.google has address 8.8.8.8
dns.google has IPv6 address 2001:4860:4860::8844
dns.google has IPv6 address 2001:4860:4860::8888

$ ping -c 4 bbb.ex.mnrsdev.com
PING dns.google (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: icmp_seq=0 ttl=116 time=7.245 ms
64 bytes from 8.8.8.8: icmp_seq=1 ttl=116 time=7.194 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=116 time=7.896 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=116 time=15.948 ms

--- dns.google ping statistics ---
4 packets transmitted, 4 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 7.194/9.571/15.948/3.692 ms
```

## 後片付け

```bash:bash
$ az network dns record-set cname remove-record \
  --resource-group mnrlabo-rg \
  --zone-name mnrsdev.com \
  --record-set-name "*.ex" \
  --cname dns.google
```

## 参考

https://learn.microsoft.com/ja-jp/azure/dns/dns-zones-records#wildcard-records

https://learn.microsoft.com/ja-jp/cli/azure/network/dns/record-set/cname?view=azure-cli-latest#az-network-dns-record-set-cname-remove-record