LoginSignup
13
12

More than 5 years have passed since last update.

「API Gatewayのタイムアウトが29秒に延びましたよ!」

Last updated at Posted at 2016-03-12

JAWS DAYS 2016参加してきました。
初参加でしたがAWS触れ合える楽しいイベントでした。

API GatewayのTimeoutは延長されています。

え、マジで?
というわけでさっそく試してみました。

APIの作成

プロジェクトを作る

まずはServerlessを使ってプロジェクトを作成します。

$ serverless project create
 _______                             __
|   _   .-----.----.--.--.-----.----|  .-----.-----.-----.
|   |___|  -__|   _|  |  |  -__|   _|  |  -__|__ --|__ --|
|____   |_____|__|  \___/|_____|__| |__|_____|_____|_____|
|   |   |             The Serverless Application Framework
|       |                           serverless.com, v0.4.2
`-------'

Serverless: Initializing Serverless Project...  

コードを書く

実コードはこんな感じです。

module.exports.respond = function(event, cb) {
  console.log("event : " + JSON.stringify(event));

  var response = {
    message: "Your Serverless function ran successfully!"
  };

  setTimeout(function() {
    return cb(null, response);
  }, event.delay);
};

リクエストで指定した分だけSleepしてから、レスポンスを返すだけですね。簡単です。
※コードはGitHubにもあります。

デプロイする

API Gateway と Lambda にデプロイします。slsならコマンドで一発ですね。

$ sls dash deploy

〜〜〜中略〜〜〜

Serverless: Select the assets you wish to deploy:
    nodejscomponent/sleep
      function - nodejscomponent/sleep
      endpoint - nodejscomponent/sleep@sleep~GET
    - - - - -
  > Deploy
    Cancel

確認

実行してみる

$ curl -w "\n%{time_total} sec\n" https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/sleep?delay=0
{"message":"Your Serverless function ran successfully!"}
0.406 sec

いい感じに帰ってきます。
それでは、delayの値を増やしつつ確認して見ましょう。

10秒制限は?

$ curl -w "\n%{time_total} sec\n" https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/sleep?delay=0
{"message":"Your Serverless function ran successfully!"}
0.406 sec
$ curl -w "\n%{time_total} sec\n" https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/sleep?delay=5000
{"message":"Your Serverless function ran successfully!"}
5.304 sec
$ curl -w "\n%{time_total} sec\n" https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/sleep?delay=10000
{"message":"Your Serverless function ran successfully!"}
10.471 sec
$ curl -w "\n%{time_total} sec\n" https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/sleep?delay=15000
{"message":"Your Serverless function ran successfully!"}
15.526 sec
$ curl -w "\n%{time_total} sec\n" https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/sleep?delay=20000
{"message":"Your Serverless function ran successfully!"}
20.509 sec
$ curl -w "\n%{time_total} sec\n" https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/sleep?delay=25000
{"message":"Your Serverless function ran successfully!"}
25.474 sec

ドキュメントに記載されている10秒の制限は問題ないですね。
さらに続けます。

25秒以上

$ curl -w "\n%{time_total} sec\n" https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/sleep?delay=26000
{"message":"Your Serverless function ran successfully!"}
26.353 sec
$ curl -w "\n%{time_total} sec\n" https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/sleep?delay=27000
{"message":"Your Serverless function ran successfully!"}
28.844 sec
$ curl -w "\n%{time_total} sec\n" https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/sleep?delay=28000
{"message":"Your Serverless function ran successfully!"}
28.346 sec
$ curl -w "\n%{time_total} sec\n" https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/sleep?delay=29000
{"message":"Your Serverless function ran successfully!"}
29.373 sec
$ curl -w "\n%{time_total} sec\n" https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/sleep?delay=30000
{"message": "Endpoint request timed out"}
30.819 sec

だいたい30秒になるとEndpoint request timed outになっているのがわかります。

まとめ

中の人の言うとおりTimeout値が29秒まで延長されているのがわかりました!

一般的なAPIで29秒も待たせるのはありえませんが
 1.LambdaがENIを作成
 2.やむを得ない事情
によって遅延してしまう場合でも、Timeoutにならずレスポンスを返せるのは助かりますね。

参考

以上です。

13
12
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
13
12