1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

sam local start-api で hot reload されない

Last updated at Posted at 2022-04-11

環境

OS: Windows10 Home
SAM Version: 1.43.0

現象

sam local start-api で API Geteway のローカルインスタンスを起動後、app.pyを修正してもホットリロードされない。

PS C:\xxxx> sam local start-api                                   
Mounting HelloWorldFunction at http://127.0.0.1:3000/hello1 [GET]
Mounting HelloEfsFunction 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

厳密にいうと、sam buildしなおさないとホットリロードされない。
調べると「そういうもんだ」って言ってる人が多く、当たり前の話なのかもしれないし、環境構築がミスってるのかもしれないが、仕方ないのでPowerShellでファイルの変更をトリガーにsam buildやっちゃう。
:warning: 私はsamもdockerも勉強し始めたてなので、何いってんだ・・・?というレベルの内容でしたらすみません。

PowerShell

    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "C:\xxxx"
    $watcher.Filter = "*.*"
    $watcher.IncludeSubdirectories = $true
    $watcher.EnableRaisingEvents = $true  

    $action = { sam build -t ./xxxx/template.yaml }    

    Register-ObjectEvent $watcher "Created" -Action $action
    Register-ObjectEvent $watcher "Changed" -Action $action
    Register-ObjectEvent $watcher "Deleted" -Action $action
    Register-ObjectEvent $watcher "Renamed" -Action $action
    while ($true) {sleep 5}

$watcher.Path = "C:\xxxx" ここにファイルの変更を監視する対象ディレクトリのパスを入れる。
$action = { sam build -t ./xxxx/template.yaml } ここにはps1ファイルからtemplate.yamlへの相対パスを入れる。

参考元

おわり

もっと、根本的な改善方法があれば教えてください・・・!

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?