LoginSignup
2
2

More than 3 years have passed since last update.

AWS Lambda 実行環境は稀に起動失敗するので、リトライ処理を意識する

Last updated at Posted at 2021-03-06

一行説明

  • タイトルの通りですが、AWS Lambda は偶発的に実行環境の起動失敗=実行失敗が起き得るので、リトライ処理を適宜組み込みましょう、というお話です

環境

  • API Gateway 経由で API と Lambda 関数を紐付けて実行
[クライアント] --- [API Gateway] --- [Lambda]

事象

AWS Lambda 実行環境の起動に失敗し、Lambda / API Gateway 実行ログに以下が出力される(複数パターンあり)

Lambda 実行ログ

  • 何も出力されない
  • START ログしか出力されない
    • 以下、ログ出力例
12:34:56 START RequestId: 12345678-abcd-efgh-ijklmbopqrst Version: $LATEST 
// 本来は以下のような実行ログがつづけて出力されるが、START しか無い
// 12:34:56 {"Level":"INFO","Time":"2021-01-01T12:34:56.789+0900",<自前で実装したログ出力>} 
// 12:34:56 END RequestId: 12345678-abcd-efgh-ijklmbopqrst
// 12:34:56 REPORT RequestId: 12345678-abcd-efgh-ijklmbopqrst Duration: 212.22 ms Billed Duration: 300 ms Memory Size: 256 MB Max Memory Used: 50 MB

API Gateway 実行ログ

  • HTTP ステータスコードが 504、Lambda リクエスト ID が発行されない
    • Lambda invocation failed with status: 504. Lambda request id: N/A
    • N/A なため、この場合 Lambda の実行ログは確認できず
  • HTTP ステータスコードが 500、Lambda リクエスト ID が発行される
    • Lambda invocation failed with status: 500. Lambda request id: (略)
    • Lambda の ServiceException が発生
    • 以下、ログ出力例
(12345678-abcd-efgh-ijklmbopqrst) Endpoint request URI: https://lambda.ap-northeast-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:ap-northeast-1:<accoundId>:function:<Lambda関数名>/invocations
(12345678-abcd-efgh-ijklmbopqrst) Endpoint request headers: {x-amzn-lambda-integration-tag=12345678-abcd-efgh-ijklmbopqrst, Authorization=*(中略) [TRUNCATED]
(12345678-abcd-efgh-ijklmbopqrst) Endpoint request body after transformations: {<リクエストボディ情報> [TRUNCATED]
(12345678-abcd-efgh-ijklmbopqrst) Endpoint response headers: {Date=Mon, 01 Jan 2021 03:34:56 GMT, Content-Type=application/json, Content-Length=86, Connection=keep-alive, x-amzn-RequestId=<リクエストID>, x-amzn-ErrorType=ServiceException}
(12345678-abcd-efgh-ijklmbopqrst) Endpoint response body before transformations: 
{
    "Message": "An error occurred and the request cannot be processed.",
    "Type": "Service"
}
(12345678-abcd-efgh-ijklmbopqrst) Lambda invocation failed with status: 500. Lambda request id: *****

発生頻度

  • 連続運転試験や負荷試験などを実行した際、数万回に一回程度の割合で発生

原因

  • Lambda 関数の一時的な問題により、当該実行環境(実行コンテキスト)において Lambda 関数の起動自体ができなかったため
    • Lambda の ServiceException サービスエラーの原因や詳細は非公開

回避策

2
2
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
2
2