---
# System prepended metadata

title: AWS Route53 API 指令集
tags: [AWS 功能]

---

# AWS Route53 API 指令集

###### tags: `AWS 功能`

## 指令
* 設定紀錄   (change-resource-record-sets)
```
# aws route53 change-resource-record-sets --hosted-zone-id <HOSTED-ZONE-ID> --change-batch file://<FILENAME.JSON>
```

* 查看紀錄   (list-resource-record-sets)
```
# aws route53 list-resource-record-sets --hosted-zone-id <HOSTED-ZONE-ID>
# aws route53 list-resource-record-sets --hosted-zone-id Z06088691BAIDOYIIIQL4
    {
        "ResourceRecordSets": [
            {
                "Name": "unique-cdn.com.",
                "Type": "NS",
                "TTL": 60,
                "ResourceRecords": [
                    {
                        "Value": "ns1.unique-cdn.com."
                    },
                    {
                        "Value": "ns2.unique-cdn.com."
                    },
                    {
                        "Value": "ns3.unique-cdn.com."
                    },
                    {
                        "Value": "ns4.unique-cdn.com"
                    }
                ]
            },
            {
                "Name": "unique-cdn.com.",
                "Type": "SOA",
                "TTL": 60,
                "ResourceRecords": [
                    {
                        "Value": "ns1.unique-cdn.com. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400"
                    }
                ]
            },
            {
                "Name": "ns1.unique-cdn.com.",
                "Type": "A",
                "TTL": 300,
                "ResourceRecords": [
                    {
                        "Value": "205.251.199.235"
                    }
                ]
            },
            {
                "Name": "ns1.unique-cdn.com.",
                "Type": "AAAA",
                "TTL": 300,
                "ResourceRecords": [
                    {
                        "Value": "2600:9000:5307:eb00::1"
                    }
                ]
            },
            {
                "Name": "ns2.unique-cdn.com.",
                "Type": "A",
                "TTL": 300,
                "ResourceRecords": [
                    {
                        "Value": "205.251.195.236"
                    }
                ]
            },
            {
                "Name": "ns2.unique-cdn.com.",
                "Type": "AAAA",
                "TTL": 300,
                "ResourceRecords": [
                    {
                        "Value": "2600:9000:5303:ec00::1"
                    }
                ]
            },
            {
                "Name": "ns3.unique-cdn.com.",
                "Type": "A",
                "TTL": 300,
                "ResourceRecords": [
                    {
                        "Value": "205.251.197.85"
                    }
                ]
            },
            {
                "Name": "ns3.unique-cdn.com.",
                "Type": "AAAA",
                "TTL": 300,
                "ResourceRecords": [
                    {
                        "Value": "2600:9000:5305:5500::1"
                    }
                ]
            },
            {
                "Name": "ns4.unique-cdn.com.",
                "Type": "A",
                "TTL": 300,
                "ResourceRecords": [
                    {
                        "Value": "205.251.192.211"
                    }
                ]
            },
            {
                "Name": "ns4.unique-cdn.com.",
                "Type": "AAAA",
                "TTL": 300,
                "ResourceRecords": [
                    {
                        "Value": "2600:9000:5300:d300::1"
                    }
                ]
            }
        ]
    }
# aws route53 list-resource-record-sets --hosted-zone-id Z0921207JOM59RW2BXM7 | jq -r '.ResourceRecordSets[].Name' | sed -n '3,100p'
    ns1.witty-cdn.com.
    ns1.witty-cdn.com.
    ns2.witty-cdn.com.
    ns2.witty-cdn.com.
    ns3.witty-cdn.com.
    ns3.witty-cdn.com.
    ns4.witty-cdn.com.
    ns4.witty-cdn.com.
```
* 查看紀錄   (list-resource-record-sets) <查詢機器組IP>
    * 查詢萬用取代方式 `*.example.com` -> `\\052.example.com`
```
# aws route53 list-resource-record-sets --hosted-zone-id <HOSTED-ZONE-ID> | jq -r '.ResourceRecordSets[].ResourceRecords[].Value' | grep -v "ns" | sort -ru
# aws route53 list-resource-record-sets --hosted-zone-id Z02539672GP6JHGHU4HZT | jq -r '.ResourceRecordSets[].ResourceRecords[].Value' | grep -v "ns" | sort -ru
    47.242.112.100
    137.220.183.81
```

```
# aws route53 list-resource-record-sets --hosted-zone-id <HOSTED-ZONE-ID> | grep -v "ns"
# aws route53 list-resource-record-sets --hosted-zone-id Z02539672GP6JHGHU4HZT | grep -v "ns"
```
* 新增託管   (create-hosted-zone)
```
# aws route53 create-hosted-zone --name <DOMAIN-NAME> --caller-reference <CALLER-REFERENCE>
# aws route53 create-hosted-zone --name yichien-test.com --caller-reference 20230116
    {
        "Location": "https://route53.amazonaws.com/2013-04-01/hostedzone/Z0618504TQGNZLAPRBIY",
        "HostedZone": {
            "Id": "/hostedzone/Z0618504TQGNZLAPRBIY",
            "Name": "yichien-test.com.",
            "CallerReference": "20230116",
            "Config": {
                "PrivateZone": false
            },
            "ResourceRecordSetCount": 2
        },
        "ChangeInfo": {
            "Id": "/change/C08367662TOO88TZPBQ2M",
            "Status": "PENDING",
            "SubmittedAt": "2023-01-16T09:34:50.850Z"
        },
        "DelegationSet": {
            "NameServers": [
                "ns-448.awsdns-56.com",
                "ns-1865.awsdns-41.co.uk",
                "ns-1210.awsdns-23.org",
                "ns-760.awsdns-31.net"
            ]
        }
    }
# aws route53 create-hosted-zone --name yichien-test.com --caller-reference yichien-test | jq -r '.HostedZone.Id' | cut -d "/" -f 3
    Z06185346Z014DQXONLY
```

* 查詢全部託管區域   (list-hosted-zones)
```
# aws route53 list-hosted-zones
# aws route53 list-hosted-zones
    {
        "HostedZones": [
            {
                "Id": "/hostedzone/Z06088691BAIDOYIIIQL4",
                "Name": "unique-cdn.com.",
                "CallerReference": "unique",
                "Config": {
                    "PrivateZone": false
                },
                "ResourceRecordSetCount": 10
            },
            {
                "Id": "/hostedzone/Z0921207JOM59RW2BXM7",
                "Name": "witty-cdn.com.",
                "CallerReference": "witty",
                "Config": {
                    "PrivateZone": false
                },
                "ResourceRecordSetCount": 14
            }
        ]
    }
# aws route53 list-hosted-zones | jq -r '.HostedZones[].Name'
    unique-cdn.com.
    witty-cdn.com.
# aws route53 list-hosted-zones | jq -r '.HostedZones[].Id' | cut -d "/" -f 3
    Z06088691BAIDOYIIIQL4
    Z0921207JOM59RW2BXM7
```

* 查詢單一託管區域   (get-hosted-zone)
```
# aws route53 get-hosted-zone --id <HOSTED-ZONE-ID>
# aws route53 get-hosted-zone --id Z06088691BAIDOYIIIQL4
    {
        "HostedZone": {
            "Id": "/hostedzone/Z06088691BAIDOYIIIQL4",
            "Name": "unique-cdn.com.",
            "CallerReference": "unique",
            "Config": {
                "PrivateZone": false
            },
            "ResourceRecordSetCount": 10
        },
        "DelegationSet": {
            "Id": "/delegationset/N01679382462526Z9CG5T",
            "CallerReference": "unique",
            "NameServers": [
                "ns-2027.awsdns-61.co.uk",
                "ns-1004.awsdns-61.net",
                "ns-1365.awsdns-42.org",
                "ns-211.awsdns-26.com"
            ]
        }
    }
```

* 刪除託管   (delete-hosted-zone)
```
# aws route53 delete-hosted-zone --id <HOSTED-ZONE-ID>
# aws route53 delete-hosted-zone --id Z0618504TQGNZLAPRBIY
    {
        "ChangeInfo": {
            "Id": "/change/C00292182L6HEBBC4YC9Q",
            "Status": "PENDING",
            "SubmittedAt": "2023-01-16T09:36:16.072Z"
        }
    }
```

* 查詢全部運作狀態   (list-health-checks)
```
# aws route53 list-health-checks
# aws route53 list-health-checks
    {
        "HealthChecks": [
            {
                "Id": "71019adf-9b56-4329-849c-a5ee10f3bb4c",
                "CallerReference": "221228_07:47:42",
                "HealthCheckConfig": {
                    "IPAddress": "192.169.0.106",
                    "Port": 80,
                    "Type": "HTTP",
                    "ResourcePath": "/index.html",
                    "FullyQualifiedDomainName": "192.169.0.106",
                    "RequestInterval": 30,
                    "FailureThreshold": 10,
                    "MeasureLatency": false,
                    "Inverted": false,
                    "Disabled": false,
                    "EnableSNI": false
                },
                "HealthCheckVersion": 1
            }
        ]
    }
# aws route53 list-health-checks | jq -r '.HealthChecks[].Id'
    e4dbb08f-8e39-4d3f-940a-75d219608897
```

* 查詢單一運作狀態   (get-health-check)
```
# aws route53 get-health-check --health-check-id <HEALTH-CHECK-ID>
# aws route53 get-health-check --health-check-id 71019adf-9b56-4329-849c-a5ee10f3bb4c
    {
        "HealthCheck": {
            "Id": "71019adf-9b56-4329-849c-a5ee10f3bb4c",
            "CallerReference": "221228_07:47:42",
            "HealthCheckConfig": {
                "IPAddress": "192.169.0.106",
                "Port": 80,
                "Type": "HTTP",
                "ResourcePath": "/index.html",
                "FullyQualifiedDomainName": "192.169.0.106",
                "RequestInterval": 30,
                "FailureThreshold": 10,
                "MeasureLatency": false,
                "Inverted": false,
                "Disabled": false,
                "EnableSNI": false
            },
            "HealthCheckVersion": 1
        }
    }
```

## SHELL 腳本
### 簡單路由 (Simple)
```
#!/bin/bash

zone_id="Z0921207JOM59RW2BXM7"
action="CREATE"
name="test.witty-cdn.com"
record_type="A"
ttl="300"
value="127.0.0.1"

echo "
{
    \"Changes\": [
        {
            \"Action\": \""$action"\",
            \"ResourceRecordSet\": {
                \"Name\": \""$name"\",
                \"Type\": \""$record_type"\",
                \"TTL\": "$ttl",
                \"ResourceRecords\": [{\"Value\": \""$value"\"}]
            }
        }
    ]
}" > test_shell.json

chmod +x test_shell.json
aws route53 change-resource-record-sets --hosted-zone-id "$zone_id" --change-batch file://test_shell.json
```
### 地理位置路由 (GeoLocation)
```
#!/bin/bash

zone_id="Z0921207JOM59RW2BXM7"
action="CREATE"
name="test.witty-cdn.com"
record_type="A"
setid="test"
country="TW"
ttl="300"
value="127.0.0.1"

echo "
{
    \"Changes\": [
        {
            \"Action\": \""$action"\",
            \"ResourceRecordSet\": {
                \"Name\": \""$name"\",
                \"Type\": \""$record_type"\",
                \"SetIdentifier\": \""$setid"\",
                \"GeoLocation\": {\"CountryCode\": \""$country"\"},
                \"TTL\": "$ttl",
                \"ResourceRecords\": [{\"Value\": \""$value"\"}]
            }
        }
    ]
}" > test_shell.json

chmod +x test_shell.json
aws route53 change-resource-record-sets --hosted-zone-id "$zone_id" --change-batch file://test_shell.json
```
### 延遲路由 (Latency)
```
#!/bin/bash

zone_id="Z0921207JOM59RW2BXM7"
action="CREATE"
name="test.witty-cdn.com"
record_type="A"
setid="test"
region="ap-northeast-1"
ttl="300"
value="127.0.0.1"
health_check=$(aws route53 list-health-checks | jq -r '.HealthChecks[].Id')

echo "
{
    \"Changes\": [
        {
            \"Action\": \""$action"\",
            \"ResourceRecordSet\": {
                \"Name\": \""$name"\",
                \"Type\": \""$record_type"\",
                \"SetIdentifier\": \""$setid"\",
                \"Region\": \""$region"\",
                \"TTL\": "$ttl",
                \"HealthCheckId\": \""$health_check"\",
                \"ResourceRecords\": [{\"Value\": \""$value"\"}]
            }
        }
    ]
}" > test_shell.json

chmod +x test_shell.json
aws route53 change-resource-record-sets --hosted-zone-id "$zone_id" --change-batch file://test_shell.json
```
### 容錯路由 (Failover)
```
#!/bin/bash

zone_id="Z0921207JOM59RW2BXM7"
action="CREATE"
name="test.witty-cdn.com"
record_type="A"
setid="test"
failover="PRIMARY"
ttl="300"
value="127.0.0.1"
health_check=$(aws route53 list-health-checks | jq -r '.HealthChecks[].Id')

echo "
{
    \"Changes\": [
        {
            \"Action\": \""$action"\",
            \"ResourceRecordSet\": {
                \"Name\": \""$name"\",
                \"Type\": \""$record_type"\",
                \"SetIdentifier\": \""$setid"\",
                \"Failover\": \""$failover"\",
                \"TTL\": "$ttl",
                \"HealthCheckId\": \""$health_check"\",
                \"ResourceRecords\": [{\"Value\": \""$value"\"}]
            }
        }
    ]
}" > test_shell.json

chmod +x test_shell.json
aws route53 change-resource-record-sets --hosted-zone-id "$zone_id" --change-batch file://test_shell.json
```


## Health-Check 設定
```
{
  "IPAddress": "192.169.0.106",
  "Port": 80,
  "Type": "HTTP",
  "ResourcePath": "index.html",   #可以不用
  "FullyQualifiedDomainName": "192.169.0.106",
  "EnableSNI": false,   #可以不用
  "RequestInterval": 30,
  "FailureThreshold": 10
}
```
```
aws route53 create-health-check --caller-reference 12280257 --health-check-config  file://create-health-check.json | jq -r '.HealthCheck.Id'
7d5825c9-fd0b-47ae-9964-dbf0cae746cd
```
* shell
```
#!/bin/bash
ip="192.169.0.106"
the_type="HTTP"
path="index.html"
name="192.169.0.106"
reference=$(date +"%y%m%d_%T")
echo "
{
  \"IPAddress\": \""$ip"\",
  \"Port\": 80,
  \"Type\": \""$the_type"\",
  \"ResourcePath\": \""$path"\",
  \"FullyQualifiedDomainName\": \""$name"\",
  \"EnableSNI\": false,
  \"RequestInterval\": 30,
  \"FailureThreshold\": 10
}" > create-health-check.json

aws route53 create-health-check --caller-reference "$reference" --health-check-config  file://create-health-check.json | jq -r '.HealthCheck.Id'
```


