SAM(Serverless Application Model)を試す
概要
LambdaとAPI Gatewayと使って何か作るのであれば、SAM(Serverless Application Model)を使うと楽にできるらしいので試してみた。
準備
SAM CLIをインストールする。 MacなのでHomebrewを使用する。
brew tap aws/tap brew install aws-sam-cli
以下のコマンドを実行してバージョン情報が表示されればOK
sam --version
samで使用できるコマンドは以下の通り
Usage: sam [OPTIONS] COMMAND [ARGS]... AWS Serverless Application Model (SAM) CLI The AWS Serverless Application Model extends AWS CloudFormation to provide a simplified way of defining the Amazon API Gateway APIs, AWS Lambda functions, and Amazon DynamoDB tables needed by your serverless application. You can find more in-depth guide about the SAM specification here: https://github.com/awslabs/serverless-application-model. Options: --debug Turn on debug logging to print debug message generated by SAM CLI. --version Show the version and exit. --info --help Show this message and exit. Commands: init Init an AWS SAM application. validate Validate an AWS SAM template. build Build your Lambda function code local Run your Serverless application locally for quick development &... package Package an AWS SAM application. deploy Deploy an AWS SAM application. logs Fetch logs for a function publish Publish a packaged AWS SAM template to the AWS Serverless Application Repository.
アプリケーション作成
アプリケーションを作成する。
sam init --name start-sam --runtime go1.x
treeコマンドを実行すると、以下が確認できる。
. ├── Makefile ├── README.md ├── hello-world │ ├── main.go │ └── main_test.go └── template.yaml 1 directory, 5 files
READMEが作成されるので、それを参照しながら進める。
開発
以下のコマンドを実行するとlocalhostで確認が可能になる。
sam local start-api
デプロイ
※事前にAWS CLIのインストールとクレデンシャルの設定が必要
CloudFormationを実行してデプロイできる。
sam deploy --guided
Prod/Stageの2つのステージが作成され、それぞれで実行できるところまで確認した。