如何在使用資源型政策調用 API Gateway API 時,疑難排解存取遭拒錯誤?

2 分的閱讀內容
0

在使用資源政策調用 Amazon API Gateway API 作業時,收到存取遭拒的錯誤。

解決方法

請依照下列疑難排解步驟進行公用或私有 API Gateway API 作業。

公用 API 作業

1.    檢查 API 作業是否具有根據 IP 位址允許或封鎖請求的資源政策。如果您尚未設定存取日誌記錄功能,請設定 API Gateway 的存取日誌記錄功能。存取日誌包含 API 作業的來源 IP 位址。確定 IP 位址不在拒絕清單中。IP 位址或 CIDR 範圍必須明確允許資源政策中的存取權。

僅允許三個 IP 位址的範例資源政策:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": "arn:aws:execute-api:<region>:<account_number>:<api_id>/*"
        },

        {
            "Effect": "Deny",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": "arn:aws:execute-api:<region>:<account_number>:<api_id>/*",
            "Condition": {
                "NotIpAddress": {
                    "aws:SourceIp": [
                        "52.36.146.106",
                        "34.216.36.206/32",
                        "205.251.233.179/32"
                    ]
                }
            }
        }
    ]
}

封鎖兩個 IP 位址的範例資源政策:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": [
                "arn:aws:execute-api:<region>:<account_number>:<api_id>/*"
            ]
        },
        {
            "Effect": "Deny",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": [
               "arn:aws:execute-api:<region>:<account_number>:<api_id>/*"
            ],
            "Condition" : {
                "IpAddress": {
                    "aws:SourceIp": ["52.36.146.106", "34.216.36.206/32" ]
                }
            }
        }
    ]
}

2.    API 請求的 HTTP 方法 (GET、POST) 和 API Gateway 資源 (/、/foo、/foo/bar) 必須明確允許存取。檢查類似於下列項目的資源政策屬性:

"Resource": [
               "arn:aws:execute-api:<region>:<account_number>:<api_id>/<stage>/<http_method>/<resource_path_specifier>/*"
            ],

3.    檢查資源政策是否使用其他類型的驗證方法或授權者。資源政策的 API Gateway 評估會受到驗證類型的影響。驗證類型範例包括 AWS Lambda AuthorizerAmazon Cognito 驗證。。請確定為 API 定義的驗證類型已正確設定資源政策。

4.    請務必重新部署 API 請求,以便讓所有變更生效。若要重新部署您的 API 請求,請重複步驟以將 REST API 部署到階段

5.    如果您使用的是 API Gateway 資源政策範例,請確保已為您的環境設定所有變數。

6.    如果您的資源政策僅允許在 Amazon Virtual Private Cloud (Amazon VPC) IP 位址內進行存取,請執行下列其中一項操作:

在您的資源政策中允許存取路由器 IP 位址和 NAT 閘道 IP 位址 (公用 IP 位址)。

- 或 -

將公用 API 端點變更為私有 API 端點

私有 API 作業

1.    如果資源政策具有 Amazon VPC 端點,請檢查條件鍵的政策。如果資源政策使用條件鍵 aws:SourceVpce,則其值必須是 Amazon VPC 端點 ID,而不是 VPC ID。如果資源政策使用條件鍵 aws:SourceVpc,則其值必須是 Amazon VPC ID,而不是 VPC 端點 ID。

若要檢查端點類型,請開啟 Amazon VPC 主控台,選擇「Endpoints」(端點),然後選擇您的端點。

2.    請務必重新部署 API 請求,以便讓所有變更生效。若要重新部署您的 API 請求,請重複步驟以將 REST API 部署到階段

3.    如果您使用的是 API Gateway 資源政策範例,請務必為您的環境設定所有變數。

4.    API 請求的 HTTP 方法 (GET、POST) 和 API Gateway 資源 (/、/foo、/foo/bar) 必須明確允許存取。檢查類似於下列項目的資源政策屬性:

"Resource": [
               "arn:aws:execute-api:<region>:<account_number>:<api_id>/<stage>/<http_method>/<resource_path_specifier>/*"
            ],

5.    檢查資源政策是否使用其他類型的驗證方法或授權者。資源政策的 API Gateway 評估會受到驗證類型的影響。驗證類型範例包括 AWS Lambda AuthorizerAmazon Cognito 驗證。。請確定為 API 定義的驗證類型已正確設定資源政策。