使用 AWS CLI 的 Route 53 範例 - AWS Command Line Interface

使用 AWS CLI 的 Route 53 範例

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

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

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

主題

動作

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

AWS CLI

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

下列 change-resource-record-sets 命令會使用 C:\awscli\route53\change-resource-record-sets.json 檔案中的 hosted-zone-id Z1R8UBAEXAMPLE 和 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 參考》中的「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" } }, {...} ] }

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

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

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

以下程式碼範例顯示如何使用 delete-health-check

AWS CLI

刪除運作狀態檢查

下列 delete-health-check 命令會使用 e75b48d9-547a-4c3d-88a5-ae4002397608health-check-id 刪除運作狀態檢查:

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 命令會使用 Z36KTIQEXAMPLEid 刪除託管區域:

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

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

AWS CLI

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

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

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

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

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

AWS CLI

取得託管區域的相關資訊

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

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 範例會針對託管區域 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 查詢

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