API Gateway 存取記錄的變數 - Amazon API Gateway

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

API Gateway 存取記錄的變數

在存取記錄中,您身為 API 開發人員且想要記錄誰已存取您的 API 以及發起人存取 API 的方式。您可以建立自己的日誌群組,或選擇由 API Gateway 管理的現有日誌群組。若要指定存取詳細資訊,您可以使用下列區分大小寫的$context變數。

如需資料轉換的參考變數清單,請參閱 API Gateway 資料轉換的變數

參數 描述
$context.accountId

API 擁有者 AWS 的帳戶 ID。

$context.apiId

API Gateway 指派給您 API 的識別碼。

$context.authorize.error 授權錯誤訊息。
$context.authorize.latency 授權延遲 (以毫秒為單位)。
$context.authorize.status 從授權嘗試傳回的狀態碼。
$context.authorizer.claims.property

成功驗證方法發起人之後,從 Amazon Cognito 使用者集區傳回之宣告的屬性。如需詳細資訊,請參閱 使用 Amazon Cognito 使用者集區做為授權方,藉以控制對 REST API 的存取

注意

呼叫 $context.authorizer.claims 會傳回 null。

$context.authorizer.error 從授權方傳回的錯誤訊息。
$context.authorizer.integrationLatency 授權方整合延遲 (以毫秒為單位)。
$context.authorizer.integrationStatus 從 Lambda 授權方傳回的狀態碼。
$context.authorizer.latency 授權方延遲 (以毫秒為單位)。
$context.authorizer.principalId

與用戶端所傳送並從 API Gateway Lambda 授權方 (先前稱作自訂授權方) 所傳回之字符相關聯的主要使用者身分。如需詳細資訊,請參閱 使用 API Gateway Lambda 授權方

$context.authorizer.property

API Gateway Lambda 授權方函數所傳回 context 映射之指定索引鍵/值組的字串化值。例如,如果授權方傳回下列 context 映射:

"context" : { "key": "value", "numKey": 1, "boolKey": true }

呼叫 $context.authorizer.key 會傳回 "value" 字串,呼叫 $context.authorizer.numKey 會傳回 "1" 字串,而呼叫 $context.authorizer.boolKey 會傳回 "true" 字串。

對於屬性,唯一支援的特殊字元是底線 (_) 字元。

如需詳細資訊,請參閱使用 API Gateway Lambda 授權方

$context.authorizer.requestId AWS 端點的請求 ID。
$context.authorizer.status 從授權方傳回的狀態碼。
$context.authenticate.error 從驗證嘗試傳回的錯誤訊息。
$context.authenticate.latency 驗證延遲 (以毫秒為單位)。
$context.authenticate.status 從驗證嘗試傳回的狀態碼。
$context.awsEndpointRequestId

AWS 端點的請求 ID。

$context.customDomain.basePathMatched

傳入請求相符的 API 映射路徑。適用於用戶端使用自訂網域名稱來存取 API 時。例如,如果用戶端向 https://api.example.com/v1/orders/1234 傳送請求,並且請求讓 API 映射與路徑 v1/orders 相符,則該值為 v1/orders。如需進一步了解,請參閱將 API 階段映射至 REST API 的自訂網域名稱

$context.deploymentId

API 部署的 ID。

$context.domainName

用來調用 API 的完整網域名稱。這應該與傳入的 Host 標頭相同。

$context.domainPrefix

$context.domainName 的第一個標籤。

$context.endpointType

API 的端點類型。

$context.error.message

包含 API Gateway 錯誤訊息的字串。此變數只能用於 Velocity 範本語言引擎無法處理的 GatewayResponse 本文映射範本及存取記錄中的簡單變數替換。如需詳細資訊,請參閱使用 CloudWatch 指標監控 WebSocket API 執行設定閘道回應以自訂錯誤回應

$context.error.messageString $context.error.message 的引用值,即 "$context.error.message"
$context.error.responseType

GatewayResponse類型。此變數只能用於 Velocity 範本語言引擎無法處理的 GatewayResponse 本文映射範本及存取記錄中的簡單變數替換。如需詳細資訊,請參閱使用 CloudWatch 指標監控 WebSocket API 執行設定閘道回應以自訂錯誤回應

$context.error.validationErrorString

字串,其中包含詳細的驗證錯誤訊息。

$context.extendedRequestId API Gateway 產生和指派給 API 請求的延伸 ID。延伸請求 ID 包含有助於偵錯和疑難排解的資訊。
$context.httpMethod

使用的 HTTP 方法。有效值包含:DELETEGETHEADOPTIONSPATCHPOSTPUT

$context.identity.accountId

與請求相關聯的 AWS 帳戶 ID。

$context.identity.apiKey

對於需要 API 金鑰的 API 方法,此變數是與方法請求相關聯的 API 金鑰。對於不需要 API 金鑰的方法,此變數為 null。如需詳細資訊,請參閱 API Gateway 中 REST API 的用量計畫和 API 金鑰

$context.identity.apiKeyId 與需要 API 金鑰之 API 請求相關聯的 API 金鑰 ID。
$context.identity.caller

已簽署請求之發起人的主體識別符。支援使用 IAM 授權的資源。

$context.identity.cognitoAuthenticationProvider

提出請求的發起人所使用的所有 Amazon Cognito 驗證提供者清單 (以逗號分隔)。僅在使用 Amazon Cognito 登入資料簽署請求時才可使用。

例如,適用於 Amazon Cognito 使用者集區的身分,cognito-idp. region.amazonaws.com/user_pool_id,cognito-idp.region.amazonaws.com/user_pool_id:CognitoSignIn:token subject claim

如需有關 Amazon Cognito 驗證提供者的詳細資訊,請參閱《Amazon Cognito 開發人員指南》中的使用聯合身分

$context.identity.cognitoAuthenticationType

提出請求的發起人的 Amazon Cognito 驗證類型。僅在使用 Amazon Cognito 登入資料簽署請求時才可使用。可能的值包括用於已驗證身分的 authenticated 和未經驗證身分的 unauthenticated

$context.identity.cognitoIdentityId

提出請求的發起人的 Amazon Cognito 身分 ID。僅在使用 Amazon Cognito 登入資料簽署請求時才可使用。

$context.identity.cognitoIdentityPoolId

提出請求的發起人的 Amazon Cognito 身分集區 ID。僅在使用 Amazon Cognito 登入資料簽署請求時才可使用。

$context.identity.principalOrgId

AWS 組織 ID

$context.identity.sourceIp

對 API Gateway 端點提出請求之即時 TCP 連線的來源 IP 位址。

$context.identity.clientCert.clientCertPem

用戶端在交互 TLS 驗證期間所呈現的 PEM 編碼用戶端憑證。當用戶端使用已啟用交互 TLS 的自訂網域名稱存取 API 時會顯示。只有在交互 TLS 驗證失敗時才會顯示在存取記錄檔中。

$context.identity.clientCert.subjectDN

用戶端提供之憑證主體的辨別名稱。當用戶端使用已啟用交互 TLS 的自訂網域名稱存取 API 時會顯示。只有在交互 TLS 驗證失敗時才會顯示在存取記錄檔中。

$context.identity.clientCert.issuerDN

用戶端提供之憑證發行者的辨別名稱。當用戶端使用已啟用交互 TLS 的自訂網域名稱存取 API 時會顯示。只有在交互 TLS 驗證失敗時才會顯示在存取記錄檔中。

$context.identity.clientCert.serialNumber

憑證的序號。當用戶端使用已啟用交互 TLS 的自訂網域名稱存取 API 時會顯示。只有在交互 TLS 驗證失敗時才會顯示在存取記錄檔中。

$context.identity.clientCert.validity.notBefore

憑證無效之前的日期。當用戶端使用已啟用交互 TLS 的自訂網域名稱存取 API 時會顯示。只有在交互 TLS 驗證失敗時才會顯示在存取記錄檔中。

$context.identity.clientCert.validity.notAfter

憑證無效之後的日期。當用戶端使用已啟用交互 TLS 的自訂網域名稱存取 API 時會顯示。只有在交互 TLS 驗證失敗時才會顯示在存取記錄檔中。

$context.identity.vpcId

對 API Gateway 端點提出請求之 VPC 的 VPC ID。

$context.identity.vpceId

對 API Gateway 端點提出請求之 VPC 端點的 VPC 端點 ID。只會在您擁有私有 API 時顯示。

$context.identity.user

將針對資源存取授權之使用者的主體識別符。支援使用 IAM 授權的資源。

$context.identity.userAgent

API 發起人的 User-Agent 標頭。

$context.identity.userArn

身分驗證之後識別之有效使用者的 Amazon Resource Name (ARN)。如需詳細資訊,請參閱https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users.html

$context.integration.error 從整合傳回的錯誤訊息。
$context.integration.integrationStatus 對於 Lambda 代理整合,從 傳回的狀態碼 AWS Lambda,而不是從後端 Lambda 函數程式碼傳回的狀態碼。
$context.integration.latency 整合延遲 (以毫秒為單位)。等同於 $context.integrationLatency
$context.integration.requestId AWS 端點的請求 ID。等同於 $context.awsEndpointRequestId
$context.integration.status 從整合傳回的狀態碼。對於 Lambda 代理整合而言,這是您的 Lambda 函數程式碼傳回的狀態碼。
$context.integrationLatency 整合延遲 (以毫秒為單位)。
$context.integrationStatus 對於 Lambda 代理整合,此參數代表從 傳回的狀態碼 AWS Lambda,而不是從後端 Lambda 函數程式碼傳回的狀態碼。
$context.isCanaryRequest

如果請求導向 Canary,則傳回 true;如果請求未導向 Canary,則傳回 false。只會在啟用 Canary 時顯示。

$context.path 請求路徑。例如,對於 https://{rest-api-id}.execute-api.{region}.amazonaws.com/{stage}/root/child 的非代理請求 URL,$context.path 值是 /{stage}/root/child
$context.protocol 請求通訊協定,例如 HTTP/1.1
注意

API Gateway API 可以接受 HTTP/2 請求,但 API Gateway 會使用 HTTP/1.1 將請求傳送至後端整合。因此,即使用戶端傳送使用 HTTP/2 的請求,請求通訊協定也會記錄為 HTTP/1.1。

$context.requestId

請求的 ID。用戶端可以覆寫此請求 ID。使用 $context.extendedRequestId 即可取得 API Gateway 產生的唯一請求 ID。

$context.requestOverride.header.header_name

請求標題會覆寫。如果此參數已經定義,它包含要使用的標頭 (而不是在 Integration Request (整合請求) 窗格中定義的 HTTP Headers (HTTP 標頭))。如需詳細資訊,請參閱 覆寫 API Gateway 中 REST APIs的 API 請求和回應參數和狀態碼

$context.requestOverride.path.path_name

請求路徑會覆寫。如果此參數已經定義,它包含要使用的請求路徑 (而不是在 Integration Request (整合請求) 窗格中定義的 URL Path Parameters (URL 路徑參數))。如需詳細資訊,請參閱 覆寫 API Gateway 中 REST APIs的 API 請求和回應參數和狀態碼

$context.requestOverride.querystring.querystring_name

請求查詢字串會覆寫。如果此參數已經定義,它包含要使用的請求查詢字串 (而不是在 Integration Request (整合請求) 窗格中定義的 URL Query String (URL 查詢字串))。如需詳細資訊,請參閱覆寫 API Gateway 中 REST APIs的 API 請求和回應參數和狀態碼

$context.responseLatency 回應延遲 (以毫秒為單位)。
$context.responseLength 回應承載長度 (以位元組為單位)。
$context.responseOverride.header.header_name 回應標題會覆寫。如果此參數已經定義,它包含要傳回的標頭 (而不是在 Integration Response (整合回應) 窗格中定義為 Default mapping (預設映射)Response header (回應標頭))。如需詳細資訊,請參閱 覆寫 API Gateway 中 REST APIs的 API 請求和回應參數和狀態碼
$context.responseOverride.status 回應狀態碼會覆寫。如果此參數已經定義,它包含要傳回的狀態碼 (而不是在 Integration Response (整合回應) 窗格中定義為 Default mapping (預設映射)Method response status (方法回應狀態))。如需詳細資訊,請參閱 覆寫 API Gateway 中 REST APIs的 API 請求和回應參數和狀態碼
$context.requestTime CLF 格式化請求時間 (dd/MMM/yyyy:HH:mm:ss +-hhmm)。
$context.requestTimeEpoch Epoch 格式化的要求時間,以毫秒為單位。
$context.resourceId

API Gateway 指派給您的資源的識別碼。

$context.resourcePath

您資源的路徑。例如,對於非代理請求 URI https://{rest-api-id}.execute-api.{region}.amazonaws.com/{stage}/root/child$context.resourcePath 值是 /root/child。如需詳細資訊,請參閱 教學:建立具有 HTTP 非代理整合的 REST API

$context.stage

API 請求的部署階段 (例如,BetaProd)。

$context.status 方法回應狀態。
$context.waf.error 從 傳回的錯誤訊息 AWS WAF。
$context.waf.latency 以毫秒為單位的 AWS WAF 延遲。
$context.waf.status 從 傳回的狀態碼 AWS WAF。
$context.xrayTraceId

X-Ray 追蹤的追蹤 ID。如需詳細資訊,請參閱AWS X-Ray 使用 API Gateway REST APIs設定

$context.wafResponseCode

AWS WAF 收到的回應:WAF_ALLOWWAF_BLOCK。若該階段與 web ACL 不相關聯,將不會設定此值。如需詳細資訊,請參閱 使用 AWS WAF 來保護 API Gateway APIs 中的 REST API

$context.webaclArn

Web ACL 的完整 ARN,用來決定是否允許或封鎖請求。若該階段與 web ACL 不相關聯,將不會設定此值。如需詳細資訊,請參閱使用 AWS WAF 來保護 API Gateway APIs 中的 REST API