題名の通りですが、sam local start-api を実行する際に、Layerを使っているとリクエストのたびにコンテナのビルドが走ってしまうケースにあたりました。
その回避策として、--warm-containers
オプションがあるので使ってみました。
事前準備
以下を参考に Layer を使った SAM を用意
- sam init でベース作成
$ sam init --runtime python3.8
- Layer を利用するように
template.yaml
を修正
Resources: HelloWorldFunction: Type: AWS::Serverless::Function Properties: CodeUri: hello_world/ Handler: app.lambda_handler Runtime: python3.8 Layers: - !Ref MyLayer Events: HelloWorld: Type: Api Properties: Path: /hello Method: get MyLayer: Type: AWS::Serverless::LayerVersion Properties: Description: Layer description ContentUri: 'my_layer/' CompatibleRuntimes: - python3.8 Metadata: BuildMethod: python3.8
- build
$ sam build MyLayer $ sam build
sam local start-api
Layerを使うと都度リクエスト時に Layer がビルドされる
$ sam local start-api Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET] You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. You only need to restart SAM CLI if you update your AWS SAM template 2021-11-03 01:29:59 * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit) 🌟1回目のリクエスト Invoking app.lambda_handler (python3.8) MyLayer is a local Layer in the template Building image........................ 🌟都度 Layer のコンテナビルド Skip pulling image and use local one: samcli/lambda:python3.8-d69b815d5b5c18e87ebbd6a1c. Mounting /test/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated inside runtime container START RequestId: fc647118-e15b-44ed-9444-30fa307b5518 Version: $LATEST END RequestId: fc647118-e15b-44ed-9444-30fa307b5518 REPORT RequestId: fc647118-e15b-44ed-9444-30fa307b5518 Init Duration: 0.22 ms Duration: 1478.64 ms Billed Duration: 1500 ms Memory Size: 128 MB Max Memory Used: 128 MB No Content-Type given. Defaulting to 'application/json'. 2021-11-03 01:30:11 127.0.0.1 - - [03/Nov/2021 01:30:11] "GET /hello HTTP/1.1" 200 - 🌟2回目のリクエスト Invoking app.lambda_handler (python3.8) MyLayer is a local Layer in the template Building image........................🌟都度 Layer のコンテナビルド Skip pulling image and use local one: samcli/lambda:python3.8-d69b815d5b5c18e87ebbd6a1c. Mounting /test/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated inside runtime container START RequestId: 6c16e473-b30a-4c27-a9e4-d41a8f42a910 Version: $LATEST END RequestId: 6c16e473-b30a-4c27-a9e4-d41a8f42a910 REPORT RequestId: 6c16e473-b30a-4c27-a9e4-d41a8f42a910 Init Duration: 0.22 ms Duration: 1021.54 ms Billed Duration: 1100 ms Memory Size: 128 MB Max Memory Used: 128 MB No Content-Type given. Defaulting to 'application/json'. 2021-11-03 01:31:44 127.0.0.1 - - [03/Nov/2021 01:31:44] "GET /hello HTTP/1.1" 200 -
--warm-containers 使うと都度ビルドを回避できる
EAGERでもLAZYでもどっちでもOK
sam local start-api - AWS Serverless Application Model
AWS SAM CLI が各関数のコンテナを管理する方法を指定します。 以下の 2 つのオプションを使用できます。 EAGER: 起動時にすべての関数のコンテナがロードされ、呼び出し間で保持されます。 LAZY: 各関数が初めて呼び出される場合に限り、コンテナがロードされます。これらのコンテナは、追加の呼び出し用に保持されます。
❯❯❯ sam local start-api --warm-containers EAGER Initializing the lambda functions containers. MyLayer is a local Layer in the template Building image........................🌟最初にコンテナビルド Skip pulling image and use local one: samcli/lambda:python3.8-d69b815d5b5c18e87ebbd6a1c. Mounting /mnt/c/Users/horiuyas/Desktop/work/001_svc_cu/16_serverless/20211003_sam/test/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated inside runtime container Containers Initialization is done. Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET] You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. You only need to restart SAM CLI if you update your AWS SAM template 2021-11-03 02:09:49 * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit) 🌟1回目のリクエスト Invoking app.lambda_handler (python3.8) Reuse the created warm container for Lambda function 'HelloWorldFunction' Lambda function 'HelloWorldFunction' is already running END RequestId: cde0b3d7-122b-4d13-901f-67dc85ba916f REPORT RequestId: cde0b3d7-122b-4d13-901f-67dc85ba916f Init Duration: 0.21 ms Duration: 1030.60 ms Billed Duration: 1100 ms Memory Size: 128 MB Max Memory Used: 128 MB No Content-Type given. Defaulting to 'application/json'. 2021-11-03 02:09:56 127.0.0.1 - - [03/Nov/2021 02:09:56] "GET /hello HTTP/1.1" 200 - 🌟2回目のリクエスト Invoking app.lambda_handler (python3.8) Reuse the created warm container for Lambda function 'HelloWorldFunction' Lambda function 'HelloWorldFunction' is already running START RequestId: c43f5d2b-c311-4100-a113-03971a6be63a Version: $LATEST END RequestId: c43f5d2b-c311-4100-a113-03971a6be63a REPORT RequestId: c43f5d2b-c311-4100-a113-03971a6be63a Duration: 517.10 ms Billed Duration: 600 ms Memory Size: 128 MB Max Memory Used: 128 MB No Content-Type given. Defaulting to 'application/json'. 2021-11-03 02:09:59 127.0.0.1 - - [03/Nov/2021 02:09:59] "GET /hello HTTP/1.1" 200 -