LoginSignup
8
8

More than 5 years have passed since last update.

aws-sam-cliの新機能 Local Lambda Endpoint と sam logs を試す

Posted at

はじめに

aws-sam-cli の 0.5.0 で2つの機能が追加されていました。

  • sam local start-lambda
  • sam logs

どのような機能か試してみました。
aws-sam-cli の基本的な使い方については以前投稿した記事も参照いただければと思います。

aws-sam-local 改め aws-sam-cli の新機能 sam init を試す
https://qiita.com/hayao_k/items/841026f9675d163b58d5

事前に最新版にアップグレードし、

$ sam --version
SAM CLI, version 0.4.0

$ pip install --user --upgrade aws-sam-cli

$ sam --version
SAM CLI, version 0.5.0

sam init でサンプルアプリケーションを作成しておきます。

$ sam init --runtime nodejs
[+] Initializing project structure...
[SUCCESS] - Read sam-app/README.md for further instructions on how to proceed
[*] Project initialization is now complete

$ cd sam-app/hello_wolrd

$ npm install
npm notice created a lockfile as package-lock.json. You should commit this file.
added 34 packages in 1.611s

$ cd ../
$ sam local generate-event api > event_file.json

sam local start-lambda

これまでも sam local invoke コマンドで Lambda 関数をローカル実行できていましたが
sam local start-lambda は Lambda のエンドポイントをローカルでエミュレートできます。
sam local start-api や DynamoDB Local の Lambda 版といって良さそうです。

早速実行してみます。
エンドポイントを起動する場合も、前提条件として Docker の起動が必要です。

$ sam local start-lambda
2018-07-18 16:17:54 Starting the Local Lambda Service. You can now invoke your Lambda Functions defined in your templatethrough the endpoint.
2018-07-18 16:17:54  * Running on http://127.0.0.1:3001/ (Press CTRL+C to quit)

Docker のインストール、起動を行っていない場合は以下のようなエラーが表示されます。

$ sam local start-lambda
Error: Running AWS SAM projects locally requires Docker. Have you got it installed?

ローカルで起動したので、ここでは AWS CLI で エンドポイントを指定し、関数を起動してみます。

$ aws lambda invoke --function-name HelloWorldFunction --endpoint-url http://127.0.0.1:3001/ --payload file://event_file.json output.txt
{
    "StatusCode": 200
}

$ less output.txt
{"statusCode":200,"body":"{\"message\":\"hello world\",\"location\":\"xxx.xxx.xxx.xxx\"}"}

成功しました。
確かにAWS SDK から Lambda を Invoke している場合やCIでテストを回している場合など、
リリース前に統合してテストを行う場面で役立ちそうです。

ログも出力されます。

$ sam local start-lambda
2018-07-20 06:11:35 Starting the Local Lambda Service. You can now invoke your Lambda Functions defined in your templatethrough the endpoint.
2018-07-20 06:11:35  * Running on http://127.0.0.1:3001/ (Press CTRL+C to quit)
2018-07-20 06:13:47 Invoking app.lambda_handler (nodejs8.10)
2018-07-20 06:13:47 Starting new HTTP connection (1): 169.254.169.254
2018-07-20 06:13:47 Starting new HTTP connection (1): 169.254.169.254

Fetching lambci/lambda:nodejs8.10 Docker container image......
2018-07-20 06:13:50 Mounting /home/ec2-user/node/sam-app/hello_world as /var/task:ro inside runtime container
START RequestId: 8f593acd-0a18-17ce-c010-e086a7b43256 Version: $LATEST
END RequestId: 8f593acd-0a18-17ce-c010-e086a7b43256
REPORT RequestId: 8f593acd-0a18-17ce-c010-e086a7b43256  Duration: 499.30 ms     Billed Duration: 500 ms Memory Size: 128 MB     Max Memory Used: 34 MB
2018-07-20 06:13:51 127.0.0.1 - - [20/Jul/2018 06:13:51] "POST /2015-03-31/functions/HelloWorldFunction/invocations HTTP/1.1" 200 -

sam logs

デプロイ済みの Lambda 関数のログを取得できます。
--tail でログをたれ流したり、--filter "文字列" でフィルタリングできるのがいい感じです。

CloudFormation のスタック名と作成されたLambda関数の論理IDを指定します。

$ sam logs -n HelloWorldFunction --stack-name mystack
2018-07-20 07:24:43 Starting new HTTP connection (1): 169.254.169.254
2018-07-20 07:24:43 Starting new HTTP connection (1): 169.254.169.254
2018-07-20 07:24:43 Starting new HTTPS connection (1): cloudformation.ap-northeast-1.amazonaws.com
2018-07-20 07:24:44 Starting new HTTPS connection (1): logs.ap-northeast-1.amazonaws.com
2018/07/20/[$LATEST]c1e4c40fbbf2495aa91ecff5b21ce24d 2018-07-20T07:24:37.835000 START RequestId: f5664465-8bed-11e8-994b-8d4b4bdcc800 Version: $LATEST
2018/07/20/[$LATEST]c1e4c40fbbf2495aa91ecff5b21ce24d 2018-07-20T07:24:38.395000 END RequestId: f5664465-8bed-11e8-994b-8d4b4bdcc800
2018/07/20/[$LATEST]c1e4c40fbbf2495aa91ecff5b21ce24d 2018-07-20T07:24:38.395000 REPORT RequestId: f5664465-8bed-11e8-994b-8d4b4bdcc800  Duration: 542.93 ms     Billed Duration: 600 ms        Memory Size: 128 MB     Max Memory Used: 22 MB

もしくは関数名を直接指定してもよいので、aws-sam-cli や CloudFormation で
デプロイされたかどうかは関係なく取得できます。
また フィルタリングした結果を tail するなどの組み合わせもできるので、
トラブルシューティングに役立ちそうです。

$ sam logs -n awshealth-to-slack --tail --filter "Error"
2018-07-20 07:33:32 Starting new HTTP connection (1): 169.254.169.254
2018-07-20 07:33:32 Starting new HTTP connection (1): 169.254.169.254
2018-07-20 07:33:33 Starting new HTTPS connection (1): logs.ap-northeast-1.amazonaws.com
2018/07/20/[$LATEST]a7c1db8930824b979e2ad84948bbceda 2018-07-20T07:31:21.307000 'detail': KeyError
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 23, in lambda_handler
    'title': event['detail']['eventTypeCode'],
KeyError: 'detail'

2018-07-20 07:34:10 Resetting dropped connection: logs.ap-northeast-1.amazonaws.com
2018/07/20/[$LATEST]a7c1db8930824b979e2ad84948bbceda 2018-07-20T07:34:02.855000 'detail': KeyError
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 23, in lambda_handler
    'title': event['detail']['eventTypeCode'],
KeyError: 'detail'

以上です。
参考になれば幸いです。

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