これは、なにをしたくて書いたもの?
最近、よくAWS SAMを使ってAWS Lambdaを使うアプリケーションの雛形を作っているのですが。
プロジェクト生成時のテンプレート名が知りたいなと思って、調べてみました。
環境
$ sam --version SAM CLI, version 1.36.0
困っていたこと
sam init
を対話的に指定するのが面倒になってきまして。オプションで指定することで、バッチ的に実行できるようです。
sam init
のヘルプを見ると
$ sam init --help
使い方が表示され、
Common usage: Starts an interactive prompt process to initialize a new project: $ sam init Initializes a new SAM project using project templates without an interactive workflow: $ sam init --name sam-app --runtime nodejs14.x --dependency-manager npm --app-template hello-world $ sam init --name sam-app --runtime nodejs14.x --architecture arm64 $ sam init --name sam-app --package-type image --base-image nodejs14.x-base
こういうのを使えばコマンド1回でプロジェクトが作れるわけですが。
$ sam init --name sam-app --runtime nodejs14.x --dependency-manager npm --app-template hello-world
--package-type
などと違って
-p, --package-type [Zip|Image] Package type for your app -i, --base-image [amazon/nodejs14.x-base|amazon/nodejs12.x-base|amazon/nodejs10.x-base|amazon/python3.9-base|amazon/python3.8-base|amazon/python3.7-base|amazon/python3.6-base|amazon/python2.7-base|amazon/ruby2.7-base|amazon/ruby2.5-base|amazon/go1.x-base|amazon/java11-base|amazon/java8.al2-base|amazon/java8-base|amazon/dotnet5.0-base|amazon/dotnetcore3.1-base|amazon/dotnetcore2.1-base]
--app-template
は内容が表示されません。
--app-template TEXT Identifier of the managed application template you want to use. If not sure, call 'sam init' without options for an interactive workflow.
これは、テンプレートが配置されている場所が決まらないと
Which template source would you like to use? 1 - AWS Quick Start Templates 2 - Custom Template Location Choice: 1 Cloning from https://github.com/aws/aws-sam-cli-app-templates
その中にある選択肢が挙げられないからだと思います。
AWS quick start application templates: 1 - Hello World Example 2 - Step Functions Sample App (Stock Trader) 3 - Quick Start: From Scratch 4 - Quick Start: Scheduled Events 5 - Quick Start: S3 6 - Quick Start: SNS 7 - Quick Start: SQS 8 - Quick Start: Web Backend Template selection:
AWS Quick Start Templates、つまりこちらを使う場合はその名前は知りたいものだな、と思います。
GitHub - aws/aws-sam-cli-app-templates
manifest.json
それで、どこを見たらテンプレートの名前がわかるかというと、manifest.json
というファイルみたいです。
https://github.com/aws/aws-sam-cli-app-templates/blob/master/manifest.json
ファイルの中を見ると、こんな内容が定義されています。
〜省略〜 "nodejs14.x": [ { "directory": "nodejs14.x/cookiecutter-aws-sam-hello-nodejs", "displayName": "Hello World Example", "dependencyManager": "npm", "appTemplate": "hello-world", "packageType": "Zip", "useCaseName": "Hello World Example" }, { "directory": "nodejs14.x/cookiecutter-aws-sam-step-functions-sample-app", "displayName": "Step Functions Sample App (Stock Trader)", "dependencyManager": "npm", "appTemplate": "step-functions-sample-app", "packageType": "Zip", "useCaseName": "Multi-step workflow" }, { "directory": "nodejs14.x/cookiecutter-quick-start-from-scratch", "displayName": "Quick Start: From Scratch", "dependencyManager": "npm", "appTemplate": "quick-start-from-scratch", "packageType": "Zip", "useCaseName": "Standalone function" }, { "directory": "nodejs14.x/cookiecutter-quick-start-cloudwatch-events", "displayName": "Quick Start: Scheduled Events", "dependencyManager": "npm", "appTemplate": "quick-start-cloudwatch-events", "packageType": "Zip", "useCaseName": "Scheduled task" }, { "directory": "nodejs14.x/cookiecutter-quick-start-s3", "displayName": "Quick Start: S3", "dependencyManager": "npm", "appTemplate": "quick-start-s3", "packageType": "Zip", "useCaseName": "Data processing" }, 〜省略〜
テンプレート名を知りたかったら、こんな感じでしょうか。
$ curl -s https://raw.githubusercontent.com/aws/aws-sam-cli-app-templates/master/manifest.json | jq 'to_entries[] | {(.key) : .value[].appTemplate}'
〜省略〜 { "nodejs14.x": "hello-world" } { "nodejs14.x": "step-functions-sample-app" } { "nodejs14.x": "quick-start-from-scratch" } { "nodejs14.x": "quick-start-cloudwatch-events" } { "nodejs14.x": "quick-start-s3" } { "nodejs14.x": "quick-start-sns" } { "nodejs14.x": "quick-start-sqs" } { "nodejs14.x": "quick-start-web" } 〜省略〜
hello-world
はもうわかっているので、試しにquick-start-sqs
を使ってみましょう
$ sam init --name sample-app --runtime nodejs14.x --app-template quick-start-sqs --package-type Zip
結果表示。
Cloning from https://github.com/aws/aws-sam-cli-app-templates ----------------------- Generating application: ----------------------- Name: sample-app Runtime: nodejs14.x Architectures: x86_64 Dependency Manager: npm Application Template: quick-start-sqs Output Directory: . Next application steps can be found in the README file at ./sample-app/README.md Commands you can use next ========================= [*] Create pipeline: cd sample-app && sam pipeline init --bootstrap [*] Test Function in the Cloud: sam sync --stack-name {stack-name} --watch
生成されたディレクトリを確認してみましょう。
$ tree sample-app sample-app ├── README.md ├── __tests__ │ └── unit │ └── handlers │ └── sqs-payload-logger.test.js ├── buildspec.yml ├── events │ └── event-sqs.json ├── package.json ├── src │ └── handlers │ └── sqs-payload-logger.js └── template.yaml 6 directories, 7 files
ソースコードも見てみます。
sample-app/src/handlers/sqs-payload-logger.js
/** * A Lambda function that logs the payload received from SQS. */ exports.sqsPayloadLoggerHandler = async (event, context) => { // All log statements are written to CloudWatch by default. For more information, see // https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-logging.html console.info(JSON.stringify(event)); }
OKそうですね。
これで、AWS SAMでプロジェクトを作成する時に、AWS Quick Start Templatesを使うのであれば対話形式で行わずに
済むようになります。