LoginSignup
7
4

More than 1 year has passed since last update.

AWS SAMのローカル開発でホットリロードを効かせる

Last updated at Posted at 2023-02-10

やりたいこと

sam local start-apiコマンドなどでAWS SAMをローカル環境で動かしている際、ソースコードを変更してもホットリロードで変更が反映されません。

変更を反映するにはsam buildを実行しなければいけません。

sam local start-apiを実行しているターミナルとは別のターミナルでsam buildを実行すればビルド後のファイルを自動で読み込んではくれます。

これを利用してVSCodeの拡張機能「Run on Save」を使ってファイルを保存したらsam buildが走るようにすることでホットリロードっぽい挙動を実現してみました。

やりかた

拡張機能をインストール後
例えばPythonのプロジェクトならこのように設定をしておきます。

.vscode/setting.json
{
    "emeraldwalk.runonsave": {
        "commands": [
            {
                "match": "template.yaml",
                "cmd": "sam build"
            },
            {
                "match": "requirements.txt",
                "cmd": "sam build"
            },
            {
                "match": ".*.py",
                "cmd": "sam build"
            }
        ]
    }
}

ファイルを保存してビルドコマンドが走ればOK。
コマンドの結果はここに出力されます。
{5DB2ECAB-E045-4E79-BEA6-90E21F38D16E}.tmp.png

ソースを変えながらホットリロードが走るか試してみます。

hello_world/app.py
import json

def lambda_handler(event, context):
    body = json.dumps({
        "message": "hello world",
    })
    print(body)
    return {
        "statusCode": 200,
        "body": body,
    }
Mounting C:\Users\danishi\sam-app\.aws-sam\build\HelloWorldFunction as /var/task:ro,delegated inside runtime container
START RequestId: ad2c7322-cc1e-4006-a837-ff7019e09d28 Version: $LATEST
{"message": "hello world"}
END RequestId: ad2c7322-cc1e-4006-a837-ff7019e09d28
REPORT RequestId: ad2c7322-cc1e-4006-a837-ff7019e09d28  Init Duration: 0.38 ms  Duration: 199.60 ms     Billed Duration: 200 ms Memory Size: 128 MB     Max Memory Used: 128 MB
No Content-Type given. Defaulting to 'application/json'.
2023-02-10 09:47:01 127.0.0.1 - - [10/Feb/2023 09:47:01] "GET /hello HTTP/1.1" 200 -
2023-02-10 09:47:01 127.0.0.1 - - [10/Feb/2023 09:47:01] "GET /favicon.ico HTTP/1.1" 403 -
Invoking app.lambda_handler (python3.9)
Using local image: public.ecr.aws/lambda/python:3.9-rapid-x86_64.

Mounting C:\Users\danishi\sam-app\.aws-sam\build\HelloWorldFunction as /var/task:ro,delegated inside runtime container
START RequestId: 3876c9bf-c560-47d1-985c-2b4940354661 Version: $LATEST
{"message": "hello world2"}
END RequestId: 3876c9bf-c560-47d1-985c-2b4940354661
REPORT RequestId: 3876c9bf-c560-47d1-985c-2b4940354661  Init Duration: 0.37 ms  Duration: 133.06 ms     Billed Duration: 134 ms Memory Size: 128 MB     Max Memory Used: 128 MB
No Content-Type given. Defaulting to 'application/json'.
2023-02-10 09:48:55 127.0.0.1 - - [10/Feb/2023 09:48:55] "GET /hello HTTP/1.1" 200 -
2023-02-10 09:48:55 127.0.0.1 - - [10/Feb/2023 09:48:55] "GET /favicon.ico HTTP/1.1" 403 -

手動でsam buildしなくてもホットリロードが実行されてるのが確認できます。

今回はサンプルなので軽量でビルド時間が短いから気になりませんが、デバッグしてないときに毎回ビルドが走るのが煩わしい場合はコマンドパレットからオフにしておくとよさそうです。

image.png

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