AWS Lambdaでの関数実行時、Task timed out after 6.01 seconds
が頻発するようになった。どうやらデフォルトの実行時間が6000ms
となっており、それを超えるとタイムアウトとなる模様。
処理自体に改善の余地はあるため、修正するときがきたかと思っていたが、デフォルトの実行時間上限を簡単に引き伸ばす方法があるようなので、暫定対応として実施した。
ちなみにタイムアウト時間の上限は15分。
以下に解決方法をメモしておく。
Serverless Frameworkを使用している場合
今回、私の環境ではServerless Frameworkを使用していた。

Serverless: Zero-Friction Serverless Apps On AWS Lambda & Beyond.
Easily build auto-scaling, low-overhead applications on AWS Lambda, API Gateway, DynamoDB, and other managed services wi...
Serverless Frameworkでは、以下のように全体もしくは関数ごとにtimeout時間の上限設定ができる。
service: testService
provider:
name: aws
runtime: nodejs12.x
timeout: 10 # 全体のタイムアウト時間の指定 デフォルトは6(s)
...
functions:
testFunction:
handler: functions/test/index.test
events:
- http:
path: test
method: any
cors: true
timeout: 60 # この関数のみタイムアウト時間上限を引き上げる
Lambda設定側から変更する(GUI)
Lambda設定画面からタイムアウト時間を引き上げたい関数を選択し、
「設定」→「一般設定」→編集

赤枠の中を設定すればおk。
timeoutについて
- デフォルトは6秒
- 関数ごとに設定でき、上限は15分(Serveless Frameworkを利用する場合は一括でも設定可能)
- API Gatewayを利用する場合は、API Gatewayのtimeoutが30秒なので注意
- 環境により異なるが、timeout後は自動的に再施行される(環境によって再施行回数変動)
参考

Serverless Framework - AWS Lambda Functions
How to configure AWS Lambda functions in the Serverless Framework

AWS SDK での、Lambda 関数の再試行とタイムアウトに関する問題のトラブルシューティング
AWS SDK で AWS Lambda 関数を呼び出すと、関数のタイムアウト、API リクエストの失敗、API アクションの重複などの問題が発生します。