文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS SDK 範例
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 的 Route 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-idZ1R8UBAEXAMPLE--change-batch file://C:\awscli\route53\change-resource-record-sets.json如需詳細資訊,請參閱《Amazon Route 53 API 參考》中的 POST ChangeResourceRecordSets。
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" } }, {...} ] }-
如需 API 詳細資訊,請參閱《 AWS CLI 命令參考》中的 ChangeResourceRecordSets
。
-
以下程式碼範例顯示如何使用 change-tags-for-resource。
- AWS CLI
-
下列命令
owner會將名為 的標籤新增至 ID 指定的運作狀態檢查資源:aws route53 change-tags-for-resource --resource-typehealthcheck--resource-id6233434j-18c1-34433-ba8e-3443434--add-tagsKey=owner,Value=myboss下列命令
owner會從 ID 指定的託管區域資源中移除名為 的標籤:aws route53 change-tags-for-resource --resource-typehostedzone--resource-idZ1523434445--remove-tag-keysowner-
如需 API 詳細資訊,請參閱《 AWS CLI 命令參考》中的 ChangeTagsForResource
。
-
以下程式碼範例顯示如何使用 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-reference2014-04-01-18:47--health-check-config file://C:\awscli\route53\create-health-check.jsonJSON 語法:
{ "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 --nameexample.com--caller-reference2014-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-ide75b48d9-547a-4c3d-88a5-ae4002397608-
如需 API 詳細資訊,請參閱《 AWS CLI 命令參考》中的 DeleteHealthCheck
。
-
以下程式碼範例顯示如何使用 delete-hosted-zone。
- AWS CLI
-
刪除託管區域
下列
delete-hosted-zone命令會使用id的 刪除託管區域Z36KTIQEXAMPLE:aws route53 delete-hosted-zone --idZ36KTIQEXAMPLE-
如需 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-id02ec8401-9879-4259-91fa-04e66d094674-
如需 API 詳細資訊,請參閱《 AWS CLI 命令參考》中的 GetHealthCheck
。
-
以下程式碼範例顯示如何使用 get-hosted-zone。
- AWS CLI
-
取得託管區域的相關資訊
下列
get-hosted-zone命令會使用id的 取得託管區域的相關資訊Z1R8UBAEXAMPLE:aws route53 get-hosted-zone --idZ1R8UBAEXAMPLE-
如需 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-items1若要檢視下一個運作狀態檢查,
NextToken請從對上一個命令的回應中取得 的值,並將其包含在--starting-token參數中,例如:aws route53 list-health-checks --max-items1--starting-tokenZ3M3LMPEXAMPLE-
如需 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-namewww.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
。
-
以下程式碼範例顯示如何使用 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-items1若要檢視有關下一個託管區域的資訊,請從上一個命令的回應中取得
NextToken值,並將其包含在--starting-token參數中,例如:aws route53 list-hosted-zones --max-items1--starting-tokenZ3M3LMPEXAMPLE-
如需 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-idZ1OX3WQEXAMPLE輸出:
{ "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 查詢。
-
如需 API 詳細資訊,請參閱《 AWS CLI 命令參考》中的 ListQueryLoggingConfigs
。
-
以下程式碼範例顯示如何使用 list-resource-record-sets。
- AWS CLI
-
列出託管區域中的資源紀錄集
下列
list-resource-record-sets命令列出指定託管區域中前 100 個資源紀錄集的摘要資訊:aws route53 list-resource-record-sets --hosted-zone-idZ2LD58HEXAMPLE如果託管區域包含超過 100 個資源紀錄集,或者如果您想要在小於 100 的群組中列出它們,請包含
--maxitems參數。例如,若要一次列出一個資源記錄集,請使用下列命令:aws route53 list-resource-record-sets --hosted-zone-idZ2LD58HEXAMPLE--max-items1若要檢視託管區域中下一個資源紀錄集的相關資訊,請從對上一個命令的
NextToken回應取得 的值,並將其包含在--starting-token參數中,例如:aws route53 list-resource-record-sets --hosted-zone-idZ2LD58HEXAMPLE--max-items1--starting-tokenZ3M3LMPEXAMPLE若要檢視特定名稱的所有資源紀錄集,請使用
--query參數來篩選掉它們。例如:aws route53 list-resource-record-sets --hosted-zone-idZ2LD58HEXAMPLE--query"ResourceRecordSets[?Name == 'example.domain.']"-
如需 API 詳細資訊,請參閱《 AWS CLI 命令參考》中的 ListResourceRecordSets
。
-