はじめに
AWS Toolkit for Visual Studio Codeがリリースされたので試してみます。
SAM localと連携していて、VSCodeからローカルテストやAWS環境へのデプロイで実施することが可能になります。
https://www.publickey1.jp/blog/19/aws_toolkit_for_visual_studio_codeaws_lmabda.html
https://docs.aws.amazon.com/ja_jp/toolkit-for-vscode/latest/userguide/welcome.html
前提
MACでの作業を想定
awscliがインストールされていること
aws-sam-cliがインストールされていること※pythonは3.6.4以上が必要
pyenv local 3.6.4
pip install aws-sam-cli
手順
AWS Toolkit for Visual Studio Codeのインストール
AWSクレデンシャル設定
AWSに接続
アプリケーション作成
ローカルテスト
デプロイ
やってみる
AWS Toolkit for Visual Studio Codeのインストール
左ペインでExtentionsをクリックする
「AWS Toolkit for Visual Studio Code」を検索してInstallをクリックする
AWSクレデンシャル設定
CMD+Shift+PでExplorerを開いて、AWS: Create Credentials Profileを実行する。
~/.aws/credentialsファイルが開くので、アクセスキー・シークレットアクセスキーを入力する。
[vscode]
aws_access_key_id = XXXXXXX
aws_secret_access_key = XXXXXXXXXXXXXX
AWSに接続
左ペインでAWSアイコンをクリックする。
Connect to AWSをクリックする
既存のCloudformationとLambdaが表示される
アプリケーション作成
Create new SAM Applicationをクリック
SAMのテンプレートファイルなどが作成される
$ tree My1stApp/
My1stApp/
├── README.md
├── event.json
├── hello-world
│ ├── app.js
│ ├── package.json
│ └── tests
│ └── unit
│ └── test-handler.js
└── template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
My1stApp
Sample SAM Template for My1stApp
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 3
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: hello-world/
Handler: app.lambdaHandler
Runtime: nodejs10.x
Events:
HelloWorld:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /hello
Method: get
Outputs:
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
# Find out more about other implicit resources you can reference within SAM
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
HelloWorldApi:
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
HelloWorldFunction:
Description: "Hello World Lambda Function ARN"
Value: !GetAtt HelloWorldFunction.Arn
HelloWorldFunctionIamRole:
Description: "Implicit IAM Role created for Hello World function"
Value: !GetAtt HelloWorldFunctionRole.Arn
ローカルテスト
作成されたapp.jsを開くと、「Run Locally」「Debug Locally」「Configure」が表示されており、それぞれをクリックすることで実行できる。
Run Locallyをクリックする。
sam localが実行され、テスト用のDockerイメージがダウンロードされて、テストが実行される。
Preparing to run app.lambdaHandler locally...
Building SAM Application...
Build complete.
Starting the SAM Application locally (see Terminal for output)
Running command: sam local invoke awsToolkitSamLocalResource --template /tmp/aws-toolkit-vscode/vsctk7VFCcJ/output/template.yaml --event /tmp/aws-toolkit-vscode/vsctk7VFCcJ/event.json --env-vars /tmp/aws-toolkit-vscode/vsctk7VFCcJ/env-vars.json
2019-07-17 12:25:29 Invoking app.lambdaHandler (nodejs10.x)
Fetching lambci/lambda:nodejs10.x Docker container image......
2019-07-17 12:25:33 Mounting /tmp/aws-toolkit-vscode/vsctk7VFCcJ/output/awsToolkitSamLocalResource as /var/task:ro,delegated inside runtime container
-略-
[32mREPORT RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72 Duration: 51.24 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 44 MB [0m
{"statusCode":200,"body":"{\"message\":\"hello world\"}"}
Local invoke of SAM Application has ended.
デプロイ
CMD+Shift+PでExplorerを開いて、AWS: Deploy SAM Applicationを実行する。
Starting SAM Application deployment...
Building SAM Application...
Packaging SAM Application to S3 Bucket: XXXX with profile: vscode
Deploying SAM Application to CloudFormation Stack: myapp with profile: vscode
Successfully deployed SAM Application to CloudFormation Stack: myapp with profile: vscode
デプロイしたLambda関数を右クリックして、Invoke on AWSをクリックする
localにあるevnet.jsonを指定してInvokeを実行
成功することを確認する。
Loading response...
Invocation result for arn:aws:lambda:ap-northeast-1:XXXXXX:function:myapp-HelloWorldFunction-EXCZ2RTGP7SE
Logs:
START RequestId: 1f4736fc-960b-4b98-9183-da8e978ebcf8 Version: $LATEST
END RequestId: 1f4736fc-960b-4b98-9183-da8e978ebcf8
REPORT RequestId: 1f4736fc-960b-4b98-9183-da8e978ebcf8 Duration: 106.54 ms Billed Duration: 200 ms Memory Size: 128 MB Max Memory Used: 62 MB
Payload:
{"statusCode":200,"body":"{\"message\":\"hello world\"}"}
Loading response...
Invocation result for arn:aws:lambda:ap-northeast-1:XXXXXX:function:myapp-HelloWorldFunction-EXCZ2RTGP7SE
Logs:
START RequestId: b4a3420d-2ba8-4bdc-b21b-6b0f20b35252 Version: $LATEST
END RequestId: b4a3420d-2ba8-4bdc-b21b-6b0f20b35252
REPORT RequestId: b4a3420d-2ba8-4bdc-b21b-6b0f20b35252 Duration: 27.32 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 62 MB
Payload:
{"statusCode":200,"body":"{\"message\":\"hello world\"}"}