

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

# 為 API Gateway API 設定 DNS 備援的自訂運作狀態檢查
<a name="dns-failover"></a>

您可以使用 Amazon Route 53 運作狀態檢查，從主要 區域中的 API Gateway API 控制 DNS 容錯移轉 AWS 區域 至次要 區域中的 API Gateway API。這可以幫助緩解發生區域問題時的影響。如果您使用自訂網域，可以執行容錯移轉而不需要用戶端變更 API 端點。

當您針對別名記錄選擇 [Evaluate Target Health](https://docs.aws.amazon.com/Route53/latest/APIReference/API_AliasTarget.html#Route53-Type-AliasTarget-EvaluateTargetHealth>Evaluate Target Health) (評估目標運作狀態) 時，只有在區域中無法使用 API Gateway 服務時，這些記錄才會失效。在某些情況下，您自己的 API Gateway API 可能會在這段時間之前發生中斷。若要直接控制 DNS 備援，請為您的 API Gateway API 設定自訂 Route 53 運作狀態檢查。在此範例中，您可以使用 CloudWatch 警示來協助操作員控制 DNS 備援。如需設定容錯移轉時的更多範例和其他考量事項，請參閱[使用 Route 53 建立災難復原機制](https://aws.amazon.com/blogs/networking-and-content-delivery/creating-disaster-recovery-mechanisms-using-amazon-route-53/)和[使用 AWS Lambda 和 CloudWatch 對 VPC 中的私有資源執行 Route 53 運作狀態檢查](https://aws.amazon.com/blogs/networking-and-content-delivery/performing-route-53-health-checks-on-private-resources-in-a-vpc-with-aws-lambda-and-amazon-cloudwatch/)。

**Topics**
+ [先決條件](#dns-failover-prereqs)
+ [步驟 1：設定資源](#dns-failover-intial-setup)
+ [步驟 2：啟動容錯移轉至次要區域](#dns-failover-initiate)
+ [步驟 3：測試容錯移轉](#dns-failover-test)
+ [步驟 4：傳回主要區域](#dns-failover-return)
+ [後續步驟：自訂和定期測試](#dns-failover-next-steps)

## 先決條件
<a name="dns-failover-prereqs"></a>

若要完成此程序，您必須建立並設定以下資源：
+ 您擁有的網域名稱。
+ 該網域名稱的 ACM 憑證為二 AWS 區域。如需更多詳細資訊，請參閱[自訂網域名稱的先決條件](how-to-custom-domains.md#how-to-custom-domains-prerequisites)。
+ 您網域名稱的 Route 53 託管區域。如需詳細資訊，請參閱 Amazon Route 53 開發人員指南中的[使用託管區域](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/hosted-zones-working-with.html)。

如需如何為網域名稱建立 Route 53 容錯移轉 DNS 記錄的詳細資訊，請參閱《Amazon Route 53 開發人員指南》中的[選擇路由政策](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/routing-policy.html)。如需如何監控 CloudWatch 警報的詳細資訊，請參閱《Amazon Route 53 開發人員指南》中的[監控 CloudWatch 警示](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/health-checks-creating-values.html#health-checks-creating-values-cloudwatch)。

## 步驟 1：設定資源
<a name="dns-failover-intial-setup"></a>

在此範例中，建立下列資源來設定網域名稱的 DNS 備援：
+ API Gateway APIs分為兩個 AWS 區域
+ 兩個名稱相同的 API Gateway 自訂網域名稱 AWS 區域
+ API Gateway API 映射，將您的 API Gateway API 連線至自訂網域名稱
+ 網域名稱的 Route 53 容錯移轉 DNS 記錄
+ 次要區域中的 CloudWatch 警示
+ 根據次要區域中的 CloudWatch 警示進行 Route 53 運作狀態檢查

首先，請確定您擁有主要和次要區域中的所有必要資源。次要區域應包含警示和運作狀態檢查。如此一來，您就不必依賴主要區域來執行容錯移轉。如需建立這些資源的範例 CloudFormation 範本，請參閱 [samples/primary.zip](samples/primary.zip)和 [samples/secondary.zip](samples/secondary.zip)。

**重要**  
容錯移轉至次要區域之前，請確定所有必要的資源都可用。否則您的 API 將無法為次要區域的流量做好準備。

## 步驟 2：啟動容錯移轉至次要區域
<a name="dns-failover-initiate"></a>

在下列範例中，待命區域會接收 CloudWatch 指標並啟動容錯移轉。我們使用自訂指標，需要操作員介入才能啟動容錯移轉。

```
aws cloudwatch put-metric-data \
    --metric-name Failover \
    --namespace HealthCheck \
    --unit Count \
    --value 1 \
    --region us-west-1
```

針對您設定的 CloudWatch 警示將指標資料取代為對應的資料。

## 步驟 3：測試容錯移轉
<a name="dns-failover-test"></a>

呼叫您的 API 並驗證是否可從次要區域獲得回應。如果您在步驟 1 中使用範例範本，回應會在容錯移轉後從 `{"message": "Hello from the primary Region!"}` 改為 `{"message": "Hello from the secondary Region!"}`。

```
curl https://my-api.example.com

{"message": "Hello from the secondary Region!"}
```

## 步驟 4：傳回主要區域
<a name="dns-failover-return"></a>

若要傳回主要區域，請傳送讓運作狀態檢查通過的 CloudWatch 指標。

```
aws cloudwatch put-metric-data \
    --metric-name Failover \
    --namespace HealthCheck \
    --unit Count \
    --value 0 \
    --region us-west-1
```

針對您設定的 CloudWatch 警示將指標資料取代為對應的資料。

呼叫您的 API 並驗證是否可從主要區域獲得回應。如果您在步驟 1 中使用範例範本，回應會從 `{"message": "Hello from the secondary Region!"}` 改為 `{"message": "Hello from the primary Region!"}`。

```
curl https://my-api.example.com

{"message": "Hello from the primary Region!"}
```

## 後續步驟：自訂和定期測試
<a name="dns-failover-next-steps"></a>

此範例示範設定 DNS 備援的一種方法。您可以將各種 CloudWatch 指標或 HTTP 端點用於管理容錯移轉的運作狀態檢查。定期測試容錯移轉機制，以確保正常運作，並且操作員熟悉您的容錯移轉程序。