

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

# IAM 資料庫身分驗證的疑難排解
<a name="UsingWithRDS.IAMDBAuth.Troubleshooting"></a>

您可在下列內容中找到某些常見 IAM 資料庫身分驗證問題的疑難排解想法，以及 IAM 資料庫身分驗證的 CloudWatch 日誌和指標的相關資訊。

## 將 IAM 資料庫身分驗證錯誤日誌匯出至 CloudWatch Logs
<a name="UsingWithRDS.IAMDBAuth.Troubleshooting.ErrorLogs"></a>

IAM 資料庫身分驗證錯誤日誌會存放在資料庫主機上，您可以將這些日誌匯出至您的 CloudWatch Logs 帳戶。使用此頁面中的日誌和修補方法對 IAM 資料庫身分驗證問題進行疑難排解。

您可以從 主控台 AWS CLI和 RDS API 啟用日誌匯出至 CloudWatch Logs。如需主控台指示，請參閱 [將資料庫日誌發佈至 Amazon CloudWatch Logs](USER_LogAccess.Procedural.UploadtoCloudWatch.md)。

若要在從 建立資料庫執行個體時，將您的 IAM 資料庫身分驗證錯誤日誌匯出至 CloudWatch Logs AWS CLI，請使用下列命令：

```
aws rds create-db-instance --db-instance-identifier {{mydbinstance}} \
--region {{us-east-1}} \
--db-instance-class {{db.t3.large}} \
--allocated-storage {{50}} \
--engine {{postgres}} \
--engine-version {{16}} \
--port {{5432}} \
--master-username {{master}} \
--master-user-password {{password}} \
--publicly-accessible \
--enable-iam-database-authentication \
--enable-cloudwatch-logs-exports=iam-db-auth-error
```

若要在從 修改資料庫執行個體資料庫時，將您的 IAM 資料庫身分驗證錯誤日誌匯出至 CloudWatch Logs AWS CLI，請使用下列命令：

```
aws rds modify-db-instance --db-instance-identifier {{mydbinstance}} \
--region {{us-east-1}} \
--cloudwatch-logs-export-configuration '{"EnableLogTypes":["iam-db-auth-error"]}'
```

若要驗證資料庫執行個體是否正在將 IAM 資料庫身分驗證日誌匯出至 CloudWatch Logs，請檢查 `EnabledCloudwatchLogsExports` 參數是否在 `describe-db-instances` 命令的輸出中設定為 `iam-db-auth-error`。

```
aws rds describe-db-instances --region us-east-1 --db-instance-identifier {{mydbinstance}}
            ...
            
             "EnabledCloudwatchLogsExports": [
                "iam-db-auth-error"
            ],
            ...
```

## IAM 資料庫身分驗證 CloudWatch 指標
<a name="UsingWithRDS.IAMDBAuth.Troubleshooting.CWMetrics"></a>

Amazon RDS 為您的 Amazon CloudWatch 帳戶提供近乎即時的 IAM 資料庫身分驗證指標。下表列出可使用 CloudWatch 的 IAM 資料庫身分驗證指標：


| 指標 | Description | 
| --- | --- | 
| `IamDbAuthConnectionRequests` | 使用 IAM 資料庫身分驗證提出的連線要求總數。 | 
| `IamDbAuthConnectionSuccess` | IAM 資料庫身分驗證要求成功總數。 | 
| `IamDbAuthConnectionFailure` | 失敗的 IAM 資料庫身分驗證請求總數。 | 
| `IamDbAuthConnectionFailureInvalidToken` | 由於字符無效而失敗的 IAM 資料庫身分驗證請求總數。 | 
| `IamDbAuthConnectionFailureInsufficientPermissions` | 由於政策或許可不正確而失敗的 IAM 資料庫身分驗證要求總數。 | 
| `IamDbAuthConnectionFailureThrottling` | 由於 IAM 資料庫身分驗證限流而失敗的 IAM 資料庫身分驗證要求總數。 | 
| `IamDbAuthConnectionFailureServerError` | 由於 IAM 資料庫身分驗證功能中的內部伺服器錯誤而失敗的 IAM 資料庫身分驗證要求總數。 | 

## 常見問題與解決方案
<a name="UsingWithRDS.IAMDBAuth.Troubleshooting.IssuesSolutions"></a>

 使用 IAM 資料庫身分驗證時可能會遇到下列問題。使用資料表中的修補步驟來解決問題：


| 錯誤 | 指標 | 原因 | 解決方案 | 
| --- | --- | --- | --- | 
| `[ERROR] Failed to authenticate the connection request for user {{db_user}} because the provided token is malformed or otherwise invalid. (Status Code: 400, Error Code: InvalidToken)` | `IamDbAuthConnectionFailure`<br />`IamDbAuthConnectionFailureInvalidToken` | 連線要求中的 IAM 資料庫驗證字符不是有效的 SigV4a 字符，或者格式不正確。 | 檢查應用程式中的字符產生策略。在某些情況下，請確定您使用有效的格式傳遞字符。截斷字符 (或不正確的字串格式) 會使字符無效。 | 
| `[ERROR] Failed to authenticate the connection request for user {{db_user}} because the token age is longer than 15 minutes. (Status Code: 400, Error Code:ExpiredToken)` | `IamDbAuthConnectionFailure`<br />`IamDbAuthConnectionFailureInvalidToken` | IAM 資料庫身分驗證字符已過期。字符的有效時間為 15 分鐘。 | 檢查您應用程式中的字符快取和/或字符重複使用邏輯。您不應重複使用超過 15 分鐘的字符。 | 
| `[ERROR] Failed to authorize the connection request for user {{db_user}} because the IAM policy assumed by the caller 'arn:aws:sts::123456789012:assumed-role/ <RoleName>/ <RoleSession>' is not authorized to perform `rds-db:connect` on the DB instance. (Status Code: 403, Error Code:NotAuthorized)` | `IamDbAuthConnectionFailure`<br />`IamDbAuthConnectionFailureInsufficientPermissions` | 此錯誤可能由下列原因造成：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.Troubleshooting.html) | 驗證您在應用程式中擔任的 IAM 角色和/或政策。請確定您擔任以產生字符的政策，與連線至資料庫的政策相同。 | 
| `[ERROR] Failed to authorize the connection request for user {{db_user}} due to IAM DB authentication throttling. (Status Code: 429, Error Code: ThrottlingException)` | `IamDbAuthConnectionFailure`<br />`IamDbAuthConnectionFailureThrottling` | 您在短時間內對資料庫提出太多連線要求。IAM 資料庫身分驗證限流限制為每秒 200 個連線。 | 降低使用 IAM 身分驗證建立新連線的速率。請考慮使用 RDS Proxy 實作連線集區，以便在應用程式中重複使用已建立的連線。 | 
| `[ERROR] Failed to authorize the connection request for user {{db_user}} due to an internal IAM DB authentication error. (Status Code: 500, Error Code: InternalError)` | `IamDbAuthConnectionFailure`<br />`IamDbAuthConnectionFailureThrottling` | 使用 IAM 資料庫身分驗證授權資料庫連線時發生內部錯誤。 | 請聯絡 https://aws.amazon.com/premiumsupport/ 以調查問題。 | 