

# 使用 AWS CLI 的 Route 53 示例
<a name="cli_route-53_code_examples"></a>

以下代码示例演示如何通过将 AWS Command Line Interface与 Route 53 结合使用，来执行操作和实现常见场景。

*操作是大型程序的代码摘录*，必须在上下文中运行。您可以通过操作了解如何调用单个服务函数，还可以通过函数相关场景的上下文查看操作。

每个示例都包含一个指向完整源代码的链接，您可以从中找到有关如何在上下文中设置和运行代码的说明。

**Topics**
+ [操作](#actions)

## 操作
<a name="actions"></a>

### `change-resource-record-sets`
<a name="route-53_ChangeResourceRecordSets_cli_topic"></a>

以下代码示例演示了如何使用 `change-resource-record-sets`。

**AWS CLI**  
**创建、更新或删除资源记录集**  
以下 `change-resource-record-sets` 命令使用 `hosted-zone-id` `Z1R8UBAEXAMPLE` 以及 `C:\awscli\route53\change-resource-record-sets.json` 文件中的 JSON 格式配置创建资源记录集：  

```
aws route53 change-resource-record-sets --hosted-zone-id Z1R8UBAEXAMPLE --change-batch file://C:\awscli\route53\change-resource-record-sets.json
```
有关更多信息，请参阅《Amazon Route 53 API Reference》**中的“POST ChangeResourceRecordSets”。  
JSON 文件中的配置取决于要创建的资源记录集的类型：  
BasicWeightedAliasWeighted AliasLatencyLatency AliasFailoverFailover Alias  
**基本语法**：  

```
{
  "Comment": "optional comment about the changes in this change batch request",
  "Changes": [
    {
      "Action": "CREATE"|"DELETE"|"UPSERT",
      "ResourceRecordSet": {
        "Name": "DNS domain name",
        "Type": "SOA"|"A"|"TXT"|"NS"|"CNAME"|"MX"|"PTR"|"SRV"|"SPF"|"AAAA",
        "TTL": time to live in seconds,
        "ResourceRecords": [
          {
            "Value": "applicable value for the record type"
          },
          {...}
        ]
      }
    },
    {...}
  ]
}
```
**加权语法**：  

```
{
  "Comment": "optional comment about the changes in this change batch request",
  "Changes": [
    {
      "Action": "CREATE"|"DELETE"|"UPSERT",
      "ResourceRecordSet": {
        "Name": "DNS domain name",
        "Type": "SOA"|"A"|"TXT"|"NS"|"CNAME"|"MX"|"PTR"|"SRV"|"SPF"|"AAAA",
        "SetIdentifier": "unique description for this resource record set",
        "Weight": value between 0 and 255,
        "TTL": time to live in seconds,
        "ResourceRecords": [
          {
            "Value": "applicable value for the record type"
          },
          {...}
        ],
        "HealthCheckId": "optional ID of an Amazon Route 53 health check"
      }
    },
    {...}
  ]
}
```
**别名语法**：  

```
{
  "Comment": "optional comment about the changes in this change batch request",
  "Changes": [
    {
      "Action": "CREATE"|"DELETE"|"UPSERT",
      "ResourceRecordSet": {
        "Name": "DNS domain name",
        "Type": "SOA"|"A"|"TXT"|"NS"|"CNAME"|"MX"|"PTR"|"SRV"|"SPF"|"AAAA",
        "AliasTarget": {
          "HostedZoneId": "hosted zone ID for your CloudFront distribution, Amazon S3 bucket, Elastic Load Balancing load balancer, or Amazon Route 53 hosted zone",
          "DNSName": "DNS domain name for your CloudFront distribution, Amazon S3 bucket, Elastic Load Balancing load balancer, or another resource record set in this hosted zone",
          "EvaluateTargetHealth": true|false
        },
        "HealthCheckId": "optional ID of an Amazon Route 53 health check"
      }
    },
    {...}
  ]
}
```
**加权别名语法**：  

```
{
  "Comment": "optional comment about the changes in this change batch request",
  "Changes": [
    {
      "Action": "CREATE"|"DELETE"|"UPSERT",
      "ResourceRecordSet": {
        "Name": "DNS domain name",
        "Type": "SOA"|"A"|"TXT"|"NS"|"CNAME"|"MX"|"PTR"|"SRV"|"SPF"|"AAAA",
        "SetIdentifier": "unique description for this resource record set",
        "Weight": value between 0 and 255,
        "AliasTarget": {
          "HostedZoneId": "hosted zone ID for your CloudFront distribution, Amazon S3 bucket, Elastic Load Balancing load balancer, or Amazon Route 53 hosted zone",
          "DNSName": "DNS domain name for your CloudFront distribution, Amazon S3 bucket, Elastic Load Balancing load balancer, or another resource record set in this hosted zone",
          "EvaluateTargetHealth": true|false
        },
        "HealthCheckId": "optional ID of an Amazon Route 53 health check"
      }
    },
    {...}
  ]
}
```
**延迟语法**：  

```
{
  "Comment": "optional comment about the changes in this change batch request",
  "Changes": [
    {
      "Action": "CREATE"|"DELETE"|"UPSERT",
      "ResourceRecordSet": {
        "Name": "DNS domain name",
        "Type": "SOA"|"A"|"TXT"|"NS"|"CNAME"|"MX"|"PTR"|"SRV"|"SPF"|"AAAA",
        "SetIdentifier": "unique description for this resource record set",
        "Region": "Amazon EC2 region name",
        "TTL": time to live in seconds,
        "ResourceRecords": [
          {
            "Value": "applicable value for the record type"
          },
          {...}
        ],
        "HealthCheckId": "optional ID of an Amazon Route 53 health check"
      }
    },
    {...}
  ]
}
```
**延迟别名语法**：  

```
{
  "Comment": "optional comment about the changes in this change batch request",
  "Changes": [
    {
      "Action": "CREATE"|"DELETE"|"UPSERT",
      "ResourceRecordSet": {
        "Name": "DNS domain name",
        "Type": "SOA"|"A"|"TXT"|"NS"|"CNAME"|"MX"|"PTR"|"SRV"|"SPF"|"AAAA",
        "SetIdentifier": "unique description for this resource record set",
        "Region": "Amazon EC2 region name",
        "AliasTarget": {
          "HostedZoneId": "hosted zone ID for your CloudFront distribution, Amazon S3 bucket, Elastic Load Balancing load balancer, or Amazon Route 53 hosted zone",
          "DNSName": "DNS domain name for your CloudFront distribution, Amazon S3 bucket, Elastic Load Balancing load balancer, or another resource record set in this hosted zone",
          "EvaluateTargetHealth": true|false
        },
        "HealthCheckId": "optional ID of an Amazon Route 53 health check"
      }
    },
    {...}
  ]
}
```
**失效转移语法**：  

```
{
  "Comment": "optional comment about the changes in this change batch request",
  "Changes": [
    {
      "Action": "CREATE"|"DELETE"|"UPSERT",
      "ResourceRecordSet": {
        "Name": "DNS domain name",
        "Type": "SOA"|"A"|"TXT"|"NS"|"CNAME"|"MX"|"PTR"|"SRV"|"SPF"|"AAAA",
        "SetIdentifier": "unique description for this resource record set",
        "Failover": "PRIMARY" | "SECONDARY",
        "TTL": time to live in seconds,
        "ResourceRecords": [
          {
            "Value": "applicable value for the record type"
          },
          {...}
        ],
        "HealthCheckId": "ID of an Amazon Route 53 health check"
      }
    },
    {...}
  ]
}
```
**失效转移别名语法**：  

```
{
  "Comment": "optional comment about the changes in this change batch request",
  "Changes": [
    {
      "Action": "CREATE"|"DELETE"|"UPSERT",
      "ResourceRecordSet": {
        "Name": "DNS domain name",
        "Type": "SOA"|"A"|"TXT"|"NS"|"CNAME"|"MX"|"PTR"|"SRV"|"SPF"|"AAAA",
        "SetIdentifier": "unique description for this resource record set",
        "Failover": "PRIMARY" | "SECONDARY",
        "AliasTarget": {
          "HostedZoneId": "hosted zone ID for your CloudFront distribution, Amazon S3 bucket, Elastic Load Balancing load balancer, or Amazon Route 53 hosted zone",
          "DNSName": "DNS domain name for your CloudFront distribution, Amazon S3 bucket, Elastic Load Balancing load balancer, or another resource record set in this hosted zone",
          "EvaluateTargetHealth": true|false
        },
        "HealthCheckId": "optional ID of an Amazon Route 53 health check"
      }
    },
    {...}
  ]
}
```
+  有关 API 详细信息，请参阅*《AWS CLI 命令参考》*中的 [ChangeResourceRecordSets](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/route53/change-resource-record-sets.html)。

### `change-tags-for-resource`
<a name="route-53_ChangeTagsForResource_cli_topic"></a>

以下代码示例演示了如何使用 `change-tags-for-resource`。

**AWS CLI**  
以下命令将名为 `owner` 的标签添加到由 ID 指定的运行状况检查资源：  

```
aws route53 change-tags-for-resource --resource-type healthcheck --resource-id 6233434j-18c1-34433-ba8e-3443434 --add-tags Key=owner,Value=myboss
```
以下命令将从由 ID 指定的托管区资源中移除名为 `owner` 的标签：  

```
aws route53 change-tags-for-resource --resource-type hostedzone --resource-id Z1523434445 --remove-tag-keys owner
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [ChangeTagsForResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/route53/change-tags-for-resource.html)。

### `create-health-check`
<a name="route-53_CreateHealthCheck_cli_topic"></a>

以下代码示例演示了如何使用 `create-health-check`。

**AWS CLI**  
**创建运行状况检查**  
以下 `create-health-check` 命令使用调用方参考 `2014-04-01-18:47` 和文件 `C:\awscli\route53\create-health-check.json` 中的 JSON 格式配置创建运行状况检查：  

```
aws route53 create-health-check --caller-reference 2014-04-01-18:47 --health-check-config file://C:\awscli\route53\create-health-check.json
```
JSON 语法：  

```
{
  "IPAddress": "IP address of the endpoint to check",
  "Port": port on the endpoint to check--required when Type is "TCP",
  "Type": "HTTP"|"HTTPS"|"HTTP_STR_MATCH"|"HTTPS_STR_MATCH"|"TCP",
  "ResourcePath": "path of the file that you want Amazon Route 53 to request--all Types except TCP",
  "FullyQualifiedDomainName": "domain name of the endpoint to check--all Types except TCP",
  "SearchString": "if Type is HTTP_STR_MATCH or HTTPS_STR_MATCH, the string to search for in the response body from the specified resource",
  "RequestInterval": 10 | 30,
  "FailureThreshold": integer between 1 and 10
}
```
要将运行状况检查添加到 Route 53 资源记录集，请使用 `change-resource-record-sets` 命令。  
有关更多信息，请参阅《Amazon Route 53 开发人员指南》**中的“Amazon Route 53 运行状况检查和 DNS 故障转移”。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [CreateHealthCheck](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/route53/create-health-check.html)。

### `create-hosted-zone`
<a name="route-53_CreateHostedZone_cli_topic"></a>

以下代码示例演示了如何使用 `create-hosted-zone`。

**AWS CLI**  
**创建托管区**  
以下 `create-hosted-zone` 命令使用调用方参考 `2014-04-01-18:47` 添加名为 `example.com` 的托管区。可选注释中包含一个空格，因此必须用引号括起来：  

```
aws route53 create-hosted-zone --name example.com --caller-reference 2014-04-01-18:47 --hosted-zone-config Comment="command-line version"
```
有关更多信息，请参阅《Amazon Route 53 开发人员指南》**中的“使用托管区”。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [CreateHostedZone](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/route53/create-hosted-zone.html)。

### `delete-health-check`
<a name="route-53_DeleteHealthCheck_cli_topic"></a>

以下代码示例演示了如何使用 `delete-health-check`。

**AWS CLI**  
**删除运行状况检查**  
以下 `delete-health-check` 命令将删除 `health-check-id` 为 `e75b48d9-547a-4c3d-88a5-ae4002397608` 的运行状况检查：  

```
aws route53 delete-health-check --health-check-id e75b48d9-547a-4c3d-88a5-ae4002397608
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [DeleteHealthCheck](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/route53/delete-health-check.html)。

### `delete-hosted-zone`
<a name="route-53_DeleteHostedZone_cli_topic"></a>

以下代码示例演示了如何使用 `delete-hosted-zone`。

**AWS CLI**  
**删除托管区**  
以下 `delete-hosted-zone` 命令将删除 `id` 为 `Z36KTIQEXAMPLE` 的托管区：  

```
aws route53 delete-hosted-zone --id Z36KTIQEXAMPLE
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [DeleteHostedZone](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/route53/delete-hosted-zone.html)。

### `get-change`
<a name="route-53_GetChange_cli_topic"></a>

以下代码示例演示了如何使用 `get-change`。

**AWS CLI**  
**获取资源记录集更改的状态**  
以下 `get-change` 命令获取 `Id` 为 `/change/CWPIK4URU2I5S` 的 `change-resource-record-sets` 请求的状态和其他信息：  

```
aws route53 get-change --id /change/CWPIK4URU2I5S
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [GetChange](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/route53/get-change.html)。

### `get-health-check`
<a name="route-53_GetHealthCheck_cli_topic"></a>

以下代码示例演示了如何使用 `get-health-check`。

**AWS CLI**  
**获取有关运行状况检查的信息**  
以下 `get-health-check` 命令获取有关 `health-check-id` 为 `02ec8401-9879-4259-91fa-04e66d094674` 的运行状况检查的信息：  

```
aws route53 get-health-check --health-check-id 02ec8401-9879-4259-91fa-04e66d094674
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [GetHealthCheck](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/route53/get-health-check.html)。

### `get-hosted-zone`
<a name="route-53_GetHostedZone_cli_topic"></a>

以下代码示例演示了如何使用 `get-hosted-zone`。

**AWS CLI**  
**获取有关托管区的信息**  
以下 `get-hosted-zone` 命令获取 `id` 为 `Z1R8UBAEXAMPLE` 的托管区的相关信息：  

```
aws route53 get-hosted-zone --id Z1R8UBAEXAMPLE
```
+  有关 API 详细信息，请参阅*《AWS CLI 命令参考》*中的 [GetHostedZone](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/route53/get-hosted-zone.html)。

### `list-health-checks`
<a name="route-53_ListHealthChecks_cli_topic"></a>

以下代码示例演示了如何使用 `list-health-checks`。

**AWS CLI**  
**列出与当前 AWS 账户关联的运行状况检查**  
以下 `list-health-checks` 命令列出与当前 AWS 账户关联的前 100 个运行状况检查的详细信息：  

```
aws route53 list-health-checks
```
如果您有超过 100 个运行状况检查，或者想要将它们按小于 100 的组列出，请包含 `--maxitems` 参数。例如，要一次列出一个运行状况检查，请使用以下命令：  

```
aws route53 list-health-checks --max-items 1
```
要查看下一个运行状况检查，请从上一个命令的响应中获取 `NextToken` 的值，并将其包含在 `--starting-token` 参数中，例如：  

```
aws route53 list-health-checks --max-items 1 --starting-token Z3M3LMPEXAMPLE
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [ListHealthChecks](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/route53/list-health-checks.html)。

### `list-hosted-zones-by-name`
<a name="route-53_ListHostedZonesByName_cli_topic"></a>

以下代码示例演示了如何使用 `list-hosted-zones-by-name`。

**AWS CLI**  
以下命令最多列出 100 个按域名排序的托管区：  

```
aws route53 list-hosted-zones-by-name
```
输出：  

```
{
  "HostedZones": [
      {
          "ResourceRecordSetCount": 2,
          "CallerReference": "test20150527-2",
          "Config": {
              "Comment": "test2",
              "PrivateZone": false
          },
          "Id": "/hostedzone/Z119WBBTVP5WFX",
          "Name": "2.example.com."
      },
      {
          "ResourceRecordSetCount": 2,
          "CallerReference": "test20150527-1",
          "Config": {
              "Comment": "test",
              "PrivateZone": false
          },
          "Id": "/hostedzone/Z3P5QSUBK4POTI",
          "Name": "www.example.com."
      }
  ],
  "IsTruncated": false,
  "MaxItems": "100"
}
```
以下命令列出按名称排序的托管区，开头为 `www.example.com`：  

```
aws route53 list-hosted-zones-by-name --dns-name www.example.com
```
输出：  

```
{
  "HostedZones": [
      {
          "ResourceRecordSetCount": 2,
          "CallerReference": "mwunderl20150527-1",
          "Config": {
              "Comment": "test",
              "PrivateZone": false
          },
          "Id": "/hostedzone/Z3P5QSUBK4POTI",
          "Name": "www.example.com."
      }
  ],
  "DNSName": "www.example.com",
  "IsTruncated": false,
  "MaxItems": "100"
}
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [ListHostedZonesByName](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/route53/list-hosted-zones-by-name.html)。

### `list-hosted-zones`
<a name="route-53_ListHostedZones_cli_topic"></a>

以下代码示例演示了如何使用 `list-hosted-zones`。

**AWS CLI**  
**列出与当前 AWS 账户关联的托管区**  
如下 `list-hosted-zones` 命令列出与当前 AWS 账户关联的前 100 个托管区的摘要信息：  

```
aws route53 list-hosted-zones
```
如果您有超过 100 个托管区，或者想要将它们按小于 100 的数量分组列出，请包含 `--max-items` 参数。例如，要一次列出一个托管区，请使用以下命令：  

```
aws route53 list-hosted-zones --max-items 1
```
要查看有关下一个托管区的信息，请从上一个命令的响应中获取 `NextToken` 的值，并将其包含在 `--starting-token` 参数中，例如：  

```
aws route53 list-hosted-zones --max-items 1 --starting-token Z3M3LMPEXAMPLE
```
+  有关 API 详细信息，请参阅 *AWS CLI 命令参考*中的 [ListHostedZones](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/route53/list-hosted-zones.html)。

### `list-query-logging-configs`
<a name="route-53_ListQueryLoggingConfigs_cli_topic"></a>

以下代码示例演示了如何使用 `list-query-logging-configs`。

**AWS CLI**  
**列出查询日志记录配置**  
以下 `list-query-logging-configs` 示例针对托管区 `Z1OX3WQEXAMPLE`，列出了 AWS 账户中前 100 个查询日志配置的相关信息。  

```
aws route53 list-query-logging-configs \
    --hosted-zone-id Z1OX3WQEXAMPLE
```
输出：  

```
{
    "QueryLoggingConfigs": [
        {
            "Id": "964ff34e-ae03-4f06-80a2-9683cexample",
            "HostedZoneId": "Z1OX3WQEXAMPLE",
            "CloudWatchLogsLogGroupArn": "arn:aws:logs:us-east-1:111122223333:log-group:/aws/route53/example.com:*"
        }
    ]
}
```
有关更多信息，请参阅《Amazon Route 53 开发人员指南》**中的[记录 DNS 查询](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/query-logs.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [ListQueryLoggingConfigs](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/route53/list-query-logging-configs.html)。

### `list-resource-record-sets`
<a name="route-53_ListResourceRecordSets_cli_topic"></a>

以下代码示例演示了如何使用 `list-resource-record-sets`。

**AWS CLI**  
**列出托管区中的资源记录集**  
以下 `list-resource-record-sets` 命令列出了有关指定托管区中前 100 个资源记录集的摘要信息。  

```
aws route53 list-resource-record-sets --hosted-zone-id Z2LD58HEXAMPLE
```
如果托管区中包含的资源记录集超过 100 个，或者想要将它们按小于 100 的数量分组列出，请包含 `--maxitems` 参数。例如，要一次列出一个资源记录集，请使用以下命令：  

```
aws route53 list-resource-record-sets --hosted-zone-id Z2LD58HEXAMPLE --max-items 1
```
要查看有关托管区中下一个资源记录的信息，请从上一个命令的响应中获取 `NextToken` 的值，并将其包含在 `--starting-token` 参数中，例如：  

```
aws route53 list-resource-record-sets --hosted-zone-id Z2LD58HEXAMPLE --max-items 1 --starting-token Z3M3LMPEXAMPLE
```
要查看具有特定名称的所有资源记录集，请使用 `--query` 参数将其过滤掉。例如：  

```
aws route53 list-resource-record-sets --hosted-zone-id Z2LD58HEXAMPLE --query "ResourceRecordSets[?Name == 'example.domain.']"
```
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [ListResourceRecordSets](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/route53/list-resource-record-sets.html)。