

# ルーティングルールに関する問題のトラブルシューティング
<a name="rest-api-routing-rules-troubleshoot"></a>

次のトラブルシューティングガイダンスは、ルーティングルールの問題を解決するのに役立ちます。

## API Gateway が API にトラフィックを送信した方法がわからない
<a name="rest-api-routing-rules-logging"></a>

REST API ステージのアクセスログを使用して、ルーティングルールのログ記録とトラブルシューティングを行うことができます。`$context.customDomain.routingRuleIdMatched` 変数を使用して、API Gateway が API へのトラフィックの送信に使用したルーティングルール ID を表示できます。API Gateway が API へのトラフィックの送信に使用した API マッピングを表示するには、`$context.customDomain.basePathMatched` 変数を使用します。

 ルーティングルールをログに記録するには、アカウントに[適切な CloudWatch Logs ロール](set-up-logging.md#set-up-access-logging-permissions) ARN を設定し、ロググループを作成する必要があります。

次のアクセスロググループの例では、ルーティングルールと API マッピングのトラブルシューティングに関連する情報を取得できます。API Gateway は、使用したルーティングメカニズムのコンテキスト変数のみを入力します。それ以外の場合、コンテキスト変数は `-` です。

------
#### [ CLF ]

```
$context.path $context.customDomain.routingRuleIdMatched $context.customDomain.basePathMatched $context.requestId $context.extendedRequestId
```

------
#### [ JSON ]

```
{"requestPath": "$context.path", "routingRuleId" : "$context.customDomain.routingRuleIdMatched", "API mapping" : "$context.customDomain.basePathMatched", "requestId":"$context.requestId", "extendedRequestId":"$context.extendedRequestId"}
```

------
#### [ XML ]

```
<request id="$context.requestId"> <requestPath>$context.path</requestPath> <ruleId>$context.customDomain.routingRuleIdMatched</ruleId> <ApiMapping>$context.customDomain.basePathMatched</ApiMapping> <extendedRequestId>$context.extendedRequestId</extendedRequestId> </request>
```

------
#### [ CSV ]

```
$context.path,$context.customDomain.routingRuleIdMatched,$context.customDomain.basePathMatched,$context.requestId,$context.extendedRequestId
```

------

また、カスタムドメイン名のルーティングモードを確認することをお勧めします。詳細については、「[カスタムドメイン名のルーティングモードを設定する](set-routing-mode.md)」を参照してください。

## カスタムドメイン名でルーティングルールを有効にできない
<a name="rest-routing-rules-access-denied"></a>

API Gateway から次のエラーが表示される場合があります。

```
Your account doesn’t have permission to use RoutingRules.
This might be caused by an IAM policy in your account with a deny statement on BasePathMapping or ApiMapping.
To grant permission for this account to use RoutingRules, use the UpdateAccount API.
This will impact any existing IAM policies that deny access to BasePathMapping or ApiMapping.
See API Gateway documentation for further details.
```

[BasePathMapping](https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazonapigatewaymanagement.html#amazonapigatewaymanagement-resources-for-iam-policies) または [ApiMapping](https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazonapigatewaymanagementv2.html#amazonapigatewaymanagementv2-resources-for-iam-policies) へのアクセスを拒否する IAM ポリシーがある場合、このエラーが発生します。カスタムドメイン名のルーティングルールを有効にすると、ポリシーは `BasePathMapping` または `ApiMapping` へのアクセスを拒否し続けますが、同じポリシーを使用して `RoutingRule` にアクセスできます。これにより、ユーザーはカスタムドメイン名のルーティング動作を変更できます。

例えば、次のようなポリシーがある場合です。

```
{
    "Sid": "DenyCreatingApiMappings",
    "Effect": "Deny",
    "Action": "apigateway:POST",
    "Resource": [
        "arn:aws:apigateway:us-west-2::/domainnames/example.com/apimappings"
    ]
}
```

`example.com` のルーティングルールを有効にすると、このポリシーは `ApiMapping` の作成へのアクセスを拒否し続けますが、`RoutingRule` の作成へのアクセスを拒否しません。

アカウントの IAM ポリシーを監査することをお勧めします。次のポリシー例では、`ApiMapping`、`BasePathMapping`、および `RoutingRule` の作成へのアクセスを拒否します。

```
{
    "Sid": "DenyCreatingBasePathMappingsApiMappings",
    "Effect": "Deny",
    "Action": "apigateway:POST",
    "Resource": [
        "arn:aws:apigateway:us-west-2::/domainnames/example.com/basepathmappings",
        "arn:aws:apigateway:us-west-2::/domainnames/example.com/apimappings"
    ]
},
{
    "Sid": "DenyCreatingRoutingRules",
    "Effect": "Deny",
    "Action": "apigateway:CreateRoutingRule",
    "Resource": [
        "arn:aws:apigateway:us-west-2:111122223333:/domainnames/example.com/routingrules/*"
    ]
}
```

すべてのポリシーが更新されたことが確認されたら、API のアカウントレベルの設定を更新して、リージョンのルーティングルールを有効にできます。

次の [update-account](https://docs.aws.amazon.com/cli/latest/reference/apigateway/update-account.html) コマンドを使用して、リージョンの API のアカウントレベル設定を更新します。

```
aws apigateway update-account --patch-operations 'op=remove,path=/features,value=BlockedForRoutingRules' --region {{us-west-2}}
```

API のアカウントレベルの設定を更新したら、カスタムドメイン名のルーティングモードを変更できます。IAM ポリシーを引き続き使用して、`RoutingRules`、`ApiMapping`、または `BasePathMapping` へのアクセスを拒否することもできます。