LoginSignup
12
7

More than 3 years have passed since last update.

AWS SAM を使用してローカル環境でLambdaを動作させる

Posted at

(AWS Serverless Application Model (SAM) コマンドラインインターフェイス。サーバレスアプリケーションをローカルで構築、テスト、デバッグする)
ほぼここから引用ですが個人的備忘録として

やりたいこと

AWS SAMを使用して、ローカル環境でLambdaの構築をする

環境

Windows 10 64bit

インストール

Docker Desktop for Windows
(sam local コマンドはDockerを使用しているため必須)

・Python 3.6
・VS Code(これは好みで他のEditorでもOK)
・AWS CLI(AWSにデプロイするのなら)
AWS SAM CLI 64bit
 参考

インストール方法

1.Docker Desktop for Window のインストール(MSIなので割愛)
2.python3.6のインストール
3.AWS CLI のインストール
pip install awscli --upgrade --user
4.VS Code のインストール
5.VS Code にPython Extensionをインストールする
6.AWS SAM CLI のインストール(MSIなので割愛)

作成方法

PS C:\User\staff\> mkdir sam_app 
PS C:\User\staff\> cd .\sam_app\
PS C:\User\staff\sam_apps\> sam init --runtime python
(ここでファイルが生成される)
PS C:\User\staff\sam_apps\> cd .\sam-app\
PS C:\User\staff\sam_apps\sam-app> ls
    ディレクトリ: C:\Users\staff\sam_apps\sam-app


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2019/05/16     10:08                hello_world
d-----       2019/05/16     10:08                tests
-a----       2019/05/16     10:08           3973 .gitignore
-a----       2019/05/16     10:08           2059 event.json
-a----       2019/05/16     10:08           7541 README.md
-a----       2019/05/16     10:08           1662 template.yaml

上記ファイルが生成されている。

PS C:\Users\staff\sam_apps\sam-app> md hello_world\build

bilidフォルダの生成

PS C:\Users\staff\sam_apps\sam-app> pip install -r .\requirements.txt -t hello_world\build

requirements.txtを先に作成して、
必要なモジュールはrequirements.txtに記述すること

PS C:\Users\staff\sam_apps\sam-app> cp .\hello_world\*.py .\hello_world\build\
PS C:\Users\staff\sam_apps\sam-app> sam local start-api
2019-05-16 10:18:06 Found credentials in shared credentials file: ~/.aws/credentials
2019-05-16 10:18:06 Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET]
2019-05-16 10:18:06 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

記載されているURLを実行します。

image.png

ローカル環境でLambdaの値を取得できている。

これでコード変更すれば、デバッグも用意にできるようになります。

最低限の動作できるまでのフローはここまで。
あとは元の記事を参考にやっていこう。

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