はじめに
こんにちは、技術一課の山中です。
もう春ですね。春は好きです。こんな時だからこそ、春を感じて過ごしたいです。
Provisioned Concurrency とは
2019 年 12 月に発表された機能です。
https://aws.amazon.com/jp/about-aws/whats-new/2019/12/aws-lambda-announces-provisioned-concurrency/
この機能を利用すると、初期化処理の完了した AWS Lambda の実行環境を事前にプロビジョニングしておくことができます。 これまでは、 AWS Lambda のコールドスタート対策として、 CloudWatch Events で定期的に対象の Lambda ファンクションを起動するような方法がとられていましたが、この機能を利用するとより簡単に対策を行うことができそうです。
料金
通常の AWS Lambda の料金は以下のとおりです。 ※ 東京リージョンの場合
項目 | 料金 |
---|---|
リクエストに応じた料金 | 1 M リクエスト当たり、 0.20 USD |
コンピューティング料金 | 1 GB のコンピューティング環境で 1 秒間実行するにつき、 0.000016667 USD |
これに対して、 Provisioned Concurrency を有効にする場合、以下に基づいて料金が発生します。 ※ 東京リージョンの場合
項目 | 料金 |
---|---|
Provisioned Concurrency を有効にしている間かかる料金 | 1 GB のコンピューティング環境を 1 秒間確保すると、 0.000005384 USD |
リクエストに応じた料金 | 1 M リクエスト当たり、 0.20 USD |
コンピューティング料金 | 1 GB のコンピューティング環境で 1 秒間実行するにつき、 0.000012562 USD |
例えば、関数に 1,024 MB のメモリを割り当て、 Provisioned Concurrency を 2 時間有効にしたとしましょう。 プロビジョニングした同時実行数は 1,000 とします。 2 時間の間に関数を 120 万回実行し、 1 リクエストにつき 1 秒間稼動しました。 この場合の料金は以下のように計算されます。
1. Provisioned Concurrency の料金
- Provisioned Concurrency の料金は、 GB / 秒当たり、
0.000005384 USD
- Provisioned Concurrency が有効になっている合計秒数は、 2 時間 →
7,200 秒
- プロビジョニングした同時実行の合計 (GB) は、 1,024 MB → 1 GB なので、同時実行数 1,000 × 1 GB →
1,000 GB
- Provisioned Concurrency の合計量 (GB / 秒) は、 1,000 GB × 7,200 秒 →
7,200,000 GB / 秒
よって、以下のようになります。
Provisioned Concurrency 料金 → 7,200,000 GB/秒 x 0.000005384 USD = 39 USD
2. リクエスト料金
- リクエストに応じた料金は、 1,000,000 リクエスト当たり
0.20 USD
よって、以下のようになります。
リクエスト料金 → 1,200,000 リクエスト × 0.20 USD / 1,000,000 リクエスト = 0.24 USD
3. コンピューティング料金
- コンピューティング料金は、 GB / 秒当たり、
0.000012562 USD
- 合計コンピューティング時間 (秒) は、 1,200,000 リクエスト × 1 秒 / リクエスト → 1,200,000 秒
- 実行コンピューティングの合計量 (GB / 秒) は、 1,024 MB → 1 GB なので、 1,200,000 秒 × 1 GB →
1,200,000 GB / 秒
よって、以下のようになります。
合計コンピューティング料金 → 1,200,000 GB / 秒 × 0.000012562 USD = 15 USD
4. 合計料金
合計料金 → Provisioned Concurrency の料金 + リクエスト料金 + コンピューティング料金 = 39 USD + 0.24 USD + 15 USD = 54.24 USD
よって、この例では東京リージョンだと 54.24 USD
程度の料金が発生することがわかりました。
使ってみる
検証用に以下 2 つの Amazon API Gateway + AWS Lambda の組み合わせを用意しました。
- Provisioned Concurrency を有効にした API
リクエスト URL | プロビジョニングされた同時実行数 |
---|---|
xxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/enabled | 100 |
- Provisioned Concurrency は無効の API
リクエスト URL | プロビジョニングされた同時実行数 |
---|---|
xxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/disabled | 0 |
このリクエスト URL に対して、 ApacheBench からリクエストをして計測していきます。
Provisioned Concurrency を有効にした API
Provisioned Concurrency を有効にした API に対して、実行した結果は以下のとおりです。 一瞬でレスポンスが返ってきました。
$ ab -n 100 -c 100 https://xxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/enabled
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking xxxxxx.execute-api.ap-northeast-1.amazonaws.com (be patient).....done
Server Software:
Server Hostname: xxxxxx.execute-api.ap-northeast-1.amazonaws.com
Server Port: 443
SSL/TLS Protocol: TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,2048,128
Server Temp Key: ECDH P-256 256 bits
TLS Server Name: xxxxxx.execute-api.ap-northeast-1.amazonaws.com
Document Path: /dev/enabled
Document Length: 0 bytes
Concurrency Level: 100
Time taken for tests: 0.311 seconds
Complete requests: 100
Failed requests: 0
Total transferred: 48200 bytes
HTML transferred: 0 bytes
Requests per second: 321.42 [#/sec] (mean)
Time per request: 311.119 [ms] (mean)
Time per request: 3.111 [ms] (mean, across all concurrent requests)
Transfer rate: 151.29 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 11 59 14.9 71 73
Processing: 27 46 14.7 47 173
Waiting: 26 45 14.5 46 170
Total: 74 104 15.3 104 184
Percentage of the requests served within a certain time (ms)
50% 104
66% 110
75% 115
80% 119
90% 122
95% 125
98% 130
99% 184
100% 184 (longest request)
Provisioned Concurrency は無効の API
続いて Provisioned Concurrency は無効の API に対して実行します。 やはり、先ほどと比べるとかなりレスポンスまでに時間がかかっています。
$ ab -n 100 -c 100 https://xxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/disabled
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking xxxxxx.execute-api.ap-northeast-1.amazonaws.com (be patient).....done
Server Software:
Server Hostname: xxxxxx.execute-api.ap-northeast-1.amazonaws.com
Server Port: 443
SSL/TLS Protocol: TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,2048,128
Server Temp Key: ECDH P-256 256 bits
TLS Server Name: xxxxxx.execute-api.ap-northeast-1.amazonaws.com
Document Path: /dev/disabled
Document Length: 0 bytes
Concurrency Level: 100
Time taken for tests: 4.248 seconds
Complete requests: 100
Failed requests: 0
Total transferred: 48200 bytes
HTML transferred: 0 bytes
Requests per second: 23.54 [#/sec] (mean)
Time per request: 4248.289 [ms] (mean)
Time per request: 42.483 [ms] (mean, across all concurrent requests)
Transfer rate: 11.08 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 11 71 20.7 79 82
Processing: 58 990 301.6 957 3028
Waiting: 54 989 301.8 957 3028
Total: 83 1060 306.1 1036 3107
Percentage of the requests served within a certain time (ms)
50% 1036
66% 1068
75% 1093
80% 1112
90% 1164
95% 1213
98% 2301
99% 3107
100% 3107 (longest request)
この後にすぐ、同じリクエストを実行してみた結果が以下です。 この結果から、レスポンスで時間がかかっていた原因として、コールドスタートが起因していることが考えられますね。
$ ab -n 100 -c 100 https://xxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/disabled
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking xxxxxx.execute-api.ap-northeast-1.amazonaws.com (be patient).....done
Server Software:
Server Hostname: xxxxxx.execute-api.ap-northeast-1.amazonaws.com
Server Port: 443
SSL/TLS Protocol: TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,2048,128
Server Temp Key: ECDH P-256 256 bits
TLS Server Name: xxxxxx.execute-api.ap-northeast-1.amazonaws.com
Document Path: /dev/disabled
Document Length: 0 bytes
Concurrency Level: 100
Time taken for tests: 0.268 seconds
Complete requests: 100
Failed requests: 0
Total transferred: 48200 bytes
HTML transferred: 0 bytes
Requests per second: 373.68 [#/sec] (mean)
Time per request: 267.612 [ms] (mean)
Time per request: 2.676 [ms] (mean, across all concurrent requests)
Transfer rate: 175.89 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 66 69 5.0 66 80
Processing: 21 35 8.8 34 94
Waiting: 21 35 8.8 34 94
Total: 87 104 9.8 104 160
Percentage of the requests served within a certain time (ms)
50% 104
66% 107
75% 110
80% 110
90% 114
95% 121
98% 138
99% 160
100% 160 (longest request)
おわりに
金額に注意は必要ですが、 Provisioned Concurrency を使うとコールドスタートで使うのをためらっていたケースでも AWS Lambda を検討しやすくなりますね。