LoginSignup
5
7

More than 5 years have passed since last update.

aws-sam-cli 0.6.0でgenerate-eventがより多くのサービスに対応!!

Last updated at Posted at 2018-08-29

SAM CLI が 最新の0.6.0で Go言語で作成された関数のデバッグに対応し、
更に generate-eventコマンド のアップデートがありました。

generate-event については50以上のeventタイプに対応していますので、紹介します。

事前準備としてアップグレードまたはインストールします。

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

$ sam --version
SAM CLI, version 0.6.0

これまで generate-event に対応していたのは以下のサービスです。

  • API Gateway
  • DynamoDB
  • Kinesis
  • DynamoDB
  • S3
  • CloudWatch Schedule Event
  • SNS

確認すると以下のように対応サービスが増えています。
API Gatewayイベントについて、
コマンドが api から apigatewayに変わっているので注意が必要ですね。

$ sam local generate-event --help
Usage: sam local generate-event [OPTIONS] COMMAND [ARGS]...

~中略~

Commands:
  alexa-skills-kit
  alexa-smart-home
  apigateway
  batch
  cloudformation
  cloudfront
  cloudwatch
  codecommit
  codepipeline
  cognito
  config
  dynamodb
  kinesis
  lex
  rekognition
  s3
  ses
  sns
  sqs
  stepfunctions

かつ、各サービスで複数のイベントタイプをサポートするようになったので
コマンドの構文が変わっています。

$ sam local generate-event <service_name> <event_type>

例えば S3の場合、put イベントと delete イベントを作成できます。

$  sam local generate-event s3 --help
Usage: sam local generate-event s3 [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  delete  Generates an Amazon S3 Delete Event
  put     Generates an Amazon S3 Put Event

Alexa Skills Kitの場合、githubで公開されているサンプルスキルのインテントが
用意されていたりします。

$ sam local generate-event alexa-skills-kit --help
Usage: sam local generate-event alexa-skills-kit [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  end-session        Generates an Amazon Alexa End Session Event
  intent-answer      Generates an Amazon Alexa Intent Answer Event
  intent-getnewfact  Generates an Amazon Alexa Intent GetNewFact...
  intent-mycoloris   Generates an Amazon Alexa Intent MyColorIs...
  intent-recipe      Generates an Amazon Alexa Intent Recipe Event
  start-session      Generates an Amazon Alexa Start Session Event

以下の hello-world でやってみます。

alexa/skill-sample-nodejs-hello-world
https://github.com/alexa/skill-sample-nodejs-hello-world/blob/master/lambda/custom/index.js

$ sam local generate-event alexa-skills-kit start-session > event_file.json

$ sam local start-lambda
2018-08-29 11:54:03 Starting the Local Lambda Service. You can now invoke your Lambda Functions defined in your template through the endpoint.
2018-08-29 11:54:03  * Running on http://127.0.0.1:3001/ (Press CTRL+C to quit)

$ 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 | python -m json.tool
{
    "response": {
        "card": {
            "content": "Welcome to the Alexa Skills Kit, you can say hello!",
            "title": "Hello World",
            "type": "Simple"
        },
        "outputSpeech": {
            "ssml": "<speak>Welcome to the Alexa Skills Kit, you can say hello!</speak>",
            "type": "SSML"
        },
        "reprompt": {
            "outputSpeech": {
                "ssml": "<speak>Welcome to the Alexa Skills Kit, you can say hello!</speak>",
                "type": "SSML"
            }
        },
        "shouldEndSession": false
    },
    "sessionAttributes": {},
    "userAgent": "ask-node/2.0.7 Node/v8.10.0",
    "version": "1.0"
}

想定どおりLaunchRequestIntentの結果が返りました。
ますますSAM CLIがいい感じです。
SAM CLIについては以前投稿した記事も参照していただければと思います。

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

aws-sam-cliの新機能 Local Lambda Endpoint と sam logs を試す
https://qiita.com/hayao_k/items/244e74c6c0f8935c2f36

簡単ですが、以上です。
参考になれば幸いです。

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