使用 Lambda 內容物件擷取 Node.js 函數資訊 - AWS Lambda

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

使用 Lambda 內容物件擷取 Node.js 函數資訊

當 Lambda 執行您的函數時,它會將內容物件傳遞至處理常式。此物件提供的方法和各項屬性提供了有關調用、函式以及執行環境的資訊。

內容方法
  • getRemainingTimeInMillis() - 傳回執行逾時前剩餘的毫秒數。

內容屬性
  • functionName – Lambda 函數的名稱。

  • functionVersion – 函數的版本

  • invokedFunctionArn - 用於調用此函數的 Amazon Resource Name (ARN)。指出調用者是否指定版本號或別名。

  • memoryLimitInMB - 分配給函數的記憶體數量。

  • awsRequestId - 調用請求的識別符。

  • logGroupName - 函數的日誌群組。

  • logStreamName - 函數執行個體的記錄串流。

  • identity - (行動應用程式) 已授權請求的 Amazon Cognito 身分的相關資訊。

    • cognitoIdentityId - 已驗證的 Amazon Cognito 身分。

    • cognitoIdentityPoolId - 授權調用的 Amazon Cognito 身分集區。

  • clientContext - (行動應用程式) 用戶端應用程式提供給 Lambda 的用戶端內容。

    • client.installation_id

    • client.app_title

    • client.app_version_name

    • client.app_version_code

    • client.app_package_name

    • env.platform_version

    • env.platform

    • env.make

    • env.model

    • env.locale

    • custom - 用戶端應用程式所設定的自訂值。

  • callbackWaitsForEmptyEventLoop – 根據預設 (true),在使用回呼型函數處理常式時,Lambda 會在回呼執行後等待事件迴圈為空,再結束函數叫用。將 設定為 false以傳送回應,並在回呼執行後立即結束調用,而不是等待事件迴圈為空。未處理的事件會在下次調用期間繼續執行。請注意,Lambda 僅支援 Node.js 22 和更早執行時間的回呼式函數處理常式。

以下範例函式紀錄內文資訊和傳回日誌的位置。

範例 index.js 檔案
exports.handler = async function(event, context) { console.log('Remaining time: ', context.getRemainingTimeInMillis()) console.log('Function name: ', context.functionName) return context.logStreamName }