使用 路由 53 範例 AWS CLI - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 路由 53 範例 AWS CLI

下列程式碼範例示範如何搭配 AWS Command Line Interface Route 53 使用 來執行動作和實作常見案例。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會示範如何呼叫個別服務函數,但您可以在相關案例中查看內容中的動作。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

主題

動作

下列程式碼範例示範如何使用 change-resource-record-sets

AWS CLI

建立、更新或刪除資源記錄集

下列change-resource-record-sets命令會使用 檔案 hosted-zone-idZ1R8UBAEXAMPLE和 JSON 格式組態來建立資源記錄集C:\awscli\route53\change-resource-record-sets.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 ChangeResourceRecordSets POST 參考中的 WordWord。 Amazon Route 53 API

JSON 檔案中的組態取決於您要建立的資源記錄集類型:

AliasLatencyLatency BasicWeightedAliasWeighted AliasFailoverFailover Word 別名

基本語法

{ "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命令會使用 檔案中的呼叫者參考2014-04-01-18:47和 JSON 格式組態來建立運作狀態檢查C:\awscli\route53\create-health-check.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

下列程式碼範例示範如何使用 create-hosted-zone

AWS CLI

若要建立託管區域

下列create-hosted-zone命令example.com會使用呼叫者參考 新增名為 的託管區域2014-04-01-18:47。選用的註解包含空格,因此必須以引號括住:

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

下列程式碼範例示範如何使用 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

下列程式碼範例示範如何使用 delete-hosted-zone

AWS CLI

刪除託管區域

下列delete-hosted-zone命令會使用 id的 刪除託管區域Z36KTIQEXAMPLE

aws route53 delete-hosted-zone --id Z36KTIQEXAMPLE
  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DeleteHostedZone

下列程式碼範例示範如何使用 get-change

AWS CLI

若要取得資源記錄集變更的狀態

下列get-change命令會取得具有 Id之 的change-resource-record-sets請求的狀態和其他資訊/change/CWPIK4URU2I5S

aws route53 get-change --id /change/CWPIK4URU2I5S
  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 GetChange

下列程式碼範例示範如何使用 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

下列程式碼範例示範如何使用 get-hosted-zone

AWS CLI

取得託管區域的相關資訊

下列get-hosted-zone命令會使用 id的 取得託管區域的相關資訊Z1R8UBAEXAMPLE

aws route53 get-hosted-zone --id Z1R8UBAEXAMPLE
  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 GetHostedZone

下列程式碼範例示範如何使用 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

下列程式碼範例示範如何使用 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
  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 ListHostedZones

下列程式碼範例示範如何使用 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.']"