0
0

Lambdaからタイムアウト時間を取得する

Posted at

Lambdaからタイムアウト時間を取得する

以前実装した内容でLambdaからとあるAPIを呼び出すというものがありました。
そのAPIは返答に時間がかかる場合があり、Lambdaのタイムアウト時間を超過してしまうこともあり、対策を打ちました。
対策としてはLambdaのタイムアウト時間に到達する前にカスタムのエラーを出力するようにしました。ここでLambdaのfunction内でタイムアウト時間を取得する必要があったので共有します。

実際のコード

  • 呼び出される側
    LambdaClientGetFunctionCommand を使用してLambdaの情報の詳細を取得していきます。
    rccApiErrorはカスタムのエラーなので気にしないでください
    getLambdaDetails関数でLambda関数の詳細を取得しますが、取得する際にLambdaの関数名を与えます。
    後はsendコマンドを使った結果をreturnします。
const { LambdaClient, GetFunctionCommand } = require('@aws-sdk/client-lambda')
const rccApiError = require('customerror')

const REGION = process.env.AWS_REGION || ''

const client = new LambdaClient({ region: REGION })

/**
 * 指定したLambda関数の詳細情報を取得
 * @param {string} functionName 取得対象のLambda関数の名前
 * @returns {Promise<Object>} Lambda関数の詳細情報を含むオブジェクト
 * @throws {rccApiError} Lambda関数が見つからない場合、その他のエラーが発生した場合にスロー
 */
exports.getLambdaDetails = async (functionName) => {
    try {
        const command = new GetFunctionCommand({ FunctionName: functionName })
        const response = await client.send(command)

        return response
    } catch (error) {
        throw new rccApiError(400, error)
    }
}
  • 呼び出す側
    getLambdaDetailsにLambda関数名を与えて取得します。
    詳細が取得できるので、その中からlambdaDetails.Configuration.Timeoutでタイムアウトを取得できます。
const sharedLambdaDetails = require('sharedLambdaDetails')

const lambdaDetails = await sharedLambdaDetails.getLambdaDetails(SYNCHRONIZE_FUNC_NAME)
const lambdaTimeout = lambdaDetails.Configuration.Timeout
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0