使用 Route 53 示例 AWS CLI - AWS SDK 代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 Route 53 示例 AWS CLI

以下代码示例向您展示了如何在 Route 53 中使用来执行操作和实现常见场景。 AWS Command Line Interface

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

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

主题

操作

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

AWS CLI

创建、更新或删除资源记录集

以下change-resource-record-sets命令使用文件C:\awscli\route53\change-resource-record-sets.json中的hosted-zone-idZ1R8UBAEXAMPLE和JSON格式化配置创建资源记录集:

aws route53 change-resource-record-sets --hosted-zone-id Z1R8UBAEXAMPLE --change-batch file://C:\awscli\route53\change-resource-record-sets.json

有关更多信息,请参阅POST ChangeResourceRecordSets 《亚马逊 Route 53 API 参考》。

JSON文件中的配置取决于您要创建的资源记录集的类型:

BasicWeightedAliasWeighted AliasLatencyLatency AliasFailoverFailover 别名

基本语法

{ "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" } }, {...} ] }

以下代码示例演示如何使用 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

以下命令owner从 ID 指定的托管区域资源中移除名为的标签:

aws route53 change-tags-for-resource --resource-type hostedzone --resource-id Z1523434445 --remove-tag-keys owner

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

AWS CLI

创建运行状况检查

以下create-health-check命令使用文件C:\awscli\route53\create-health-check.json中的呼叫者引用2014-04-01-18:47和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命令。

有关更多信息,请参阅《亚马逊 Route 53 开发者指南》中的 Amazon Route 53 健康检查和DNS故障转移。

以下代码示例演示如何使用 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 开发人员指南中的使用托管区。

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

AWS CLI

删除运行状况检查

以下delete-health-check命令会删除运行状况检查,其值为health-check-ide75b48d9-547a-4c3d-88a5-ae4002397608

aws route53 delete-health-check --health-check-id e75b48d9-547a-4c3d-88a5-ae4002397608

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

AWS CLI

删除托管区

以下 delete-hosted-zone 命令将删除 idZ36KTIQEXAMPLE 的托管区:

aws route53 delete-hosted-zone --id Z36KTIQEXAMPLE

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

AWS CLI

获取资源记录集更改的状态

以下get-change命令获取请求的状态和其他信息,该change-resource-record-sets请求的状态和其它信息Id/change/CWPIK4URU2I5S

aws route53 get-change --id /change/CWPIK4URU2I5S

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

AWS CLI

获取有关运行状况检查的信息

以下get-health-check命令获取有关具有以下值的运行状况检查的信息02ec8401-9879-4259-91fa-04e66d094674health-check-id

aws route53 get-health-check --health-check-id 02ec8401-9879-4259-91fa-04e66d094674

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

AWS CLI

获取有关托管区的信息

以下 get-hosted-zone 命令获取 idZ1R8UBAEXAMPLE 的托管区的相关信息:

aws route53 get-hosted-zone --id Z1R8UBAEXAMPLE

以下代码示例演示如何使用 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

以下代码示例演示如何使用 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" }

以下代码示例演示如何使用 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

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

AWS CLI

列出查询日志记录配置

以下list-query-logging-configs示例列出了有关您 AWS 账户中托管区域前 100 个查询日志配置的信息Z1OX3WQEXAMPLE

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查询

以下代码示例演示如何使用 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.']"