LoginSignup
14

More than 3 years have passed since last update.

[AWS]VSCodeでSAM(Lambda)のHelloWorldを5分で行う

Last updated at Posted at 2019-07-16

AWS Toolkit for Visual Studio Code の正式版がリリースされたということで、早速HelloWorldしてみました。

今回はPythonを使います。node.jsもほぼ同じでできます。

0. 環境準備

VSCode

肝心のVSCode以下からインストールします。
https://code.visualstudio.com/

Docker

DockerはLambdaローカル実行時に使用するので必須です。
インストールしてない方はここからインストールします。
https://hub.docker.com

Python

awscliをインストールする際に使います。
インストールされてない場合は、インストールしてください。

$ python --version
Python 3.7.0
$ pip --version
pip 19.1.1

AWS Cli

以下コマンドでawsとsamをインストールしましょう。

$ pip install awscli
$ pip aws-sam-cli

インストール済みの場合は、awscliのバージョンを上げておきましょう。

$ pip install -U aws-sam-cli

aws configureでaws のアクセスキーとシークレットキーを設定しましょう。

$ aws configure

AWS Toolkit for Visual Studio Code

VSCodeの拡張機能で「AWS Toolkit」と検索して、インストールします。

1. HelloWorldローカル実行

1-1. テンプレ作成

コマンドパレット(Macなら、Shift + ⌘ + P) を開き、
「aws create new Sam Application」を選択します。

以下の順に聞かれるので入力します。

  • 開発する言語(今回はPython3.7を選びました)
  • ディレクトリ(今回はtest)
  • プロジェクト名(今回はhello)

test/helloが作られていればOKです。

1-2. ローカル実行

hello_world/app.pyを開きます。

以下の上に 「Run Locally | Debug Locally | Configure」と表示されているので、
「Run Locally」をクリックしていきなり実行します。

def lambda_handler(event, context):
{"statusCode":200,"body":"{\"message\": \"hello world\"}"}
Local invoke of SAM Application has ended.

こんな感じで出力されればOKです(最初はコンテナが起動するので時間がかかります)。
これでローカルでLambdaが実行できました。

2. デプロイ

  1. デプロイ用のS3バケットを作成($ aws s3 mb s3://BUCKET_NAME)
  2. コマンドパレットを開いて、「aws Deploy SAM Application」を選択
  3. デプロイするテンプレートの選択。今回は「hello/template.yaml」を選択
  4. リージョンの選択。今回は「Asia Pacific (Tokyo)」を選択
  5. 1で作成したS3バケット名を指定
  6. cloudformationのstack名を入力(プロジェクト名をいれておけばまずOK)
Successfully deployed SAM Application to CloudFormation Stack: hello with profile: ****

上記出力されれば無事にデプロイされました。

VSCodeの左メニューのAWSアイコンを選択すると、
CloudFormationおよびLambdaの所に、デプロイしたhelloが表示されていると思います。

AWSコンソール「API GateWay」>「hello」>「ステージ」>「Prod」>「URL の呼び出し」
のURLに/helloをつけてブラウザ等でアクセスすると「hello_word」のメッセージが表示されます。

URLは以下の感じ。
https://*********.execute-api.ap-northeast-1.amazonaws.com/Prod/hello

これで、Lambdaのデプロイができました(APIが作れました)。

削除する際は、VSCodeからCloudFormationのhelloを選択して、「Delete CloudFormation Stack」をしてください。
これで関連するサービスがすべて消えます。

番外編

AWS Toolkit for Visual Studio Code がなかった頃はコマンドでやってましたので、コマンドでもやってみます。

1. HelloWorldローカル実行

0.環境準備 は済んでいるものとします。

1-1. テンプレ作成

$ cd test
$ sam init -r python3.7 -n hello

testディレクトリにhelloプロジェクトをPython3.7で作成します。

以下の通り、作成されます。

$ cd hello
$ tree
.
|-- README.md
|-- event.json
|-- hello_world
|   |-- __init__.py
|   |-- __pycache__
|   |   |-- __init__.cpython-37.pyc
|   |   `-- app.cpython-37.pyc
|   |-- app.py
|   `-- requirements.txt
|-- template.yaml
`-- tests
    `-- unit
        |-- __init__.py
        |-- __pycache__
        |   |-- __init__.cpython-37.pyc
        |   `-- test_handler.cpython-37.pyc
        `-- test_handler.py

1-2. ローカル実行

以下のコマンドで実行します。
hello worldが出力されればOK

$ cd hello
$ sam local invoke -e event.json
〜(){"statusCode": 200, "body": "{\"message\": \"hello world\"}"}

2. デプロイ

デプロイ用のS3バケットを作ります。作ってあればOKです

$ aws s3 mb s3://BUCKET_NAME

以下コマンドでパッケージ化したものをS3に設置

$ sam package --template-file template.yaml --output-template-file packaged.yaml --s3-bucket BUCKET_NAME
$ sam deploy --template-file packaged.yaml --stack-name hello --capabilities CAPABILITY_IAM

Waiting for changeset to be created..
Waiting for stack create/update to complete
Successfully created/updated stack - hello

AWSコンソールから,CloudFormation, Lambda, APIGateWay を確認してみてください。
作成されているはずです。

削除する際は、CloudFormationから削除すれば関連するサービスまとめて削除してくれます。

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
14