LoginSignup
10
8

More than 5 years have passed since last update.

AWS SAMを使ってLambdaをデプロイする

Posted at

SAMとは

サーバーレスアプリケーションモデル(SAM)。AWSが提供するオープンソースのフレームワーク。

インストール

ドキュメントの通り。SAMの他にdocker for MacとAWS CLIを入れましょう。

  • brew tapを追加
brew tap aws/tap
  • aws-sam-cliをインストール
brew install aws-sam-cli
  • 確認
sam --version

プロジェクトを作成

sam init -n sample_app

-nオプションで名前を指定できます。未指定の場合はsam-app
hello worldのサンプルが作成されます。

作成されるもの

├── README.md
├── event.json
├── hello-world
│   ├── app.js
│   ├── package.json
│   └── tests
│       └── unit
│           └── test-handler.js
└── template.yaml
  • template.yaml
    AWSリソースの定義ファイル。LambdaやAPI Gateway等の定義を記述します。

  • hello_world/
    Lambdaのコードが入っているディレクトリ。

  • event.json
    Lambda関数にわたすイベントデータを含むjsonファイル。

デプロイまでの流れ

ビルド

Lambda関数をビルドします。

sam build

ローカルテスト

dockerコンテナを起動しローカル環境でLambda関数をテストします。

sam local invoke -e event.json

パッケージング

sam package --s3-bucket sam-test --output-template-file packaged.yaml

デプロイ

デプロイするとCloudformationを利用して各リソースが作成されます。

sam deploy --template-file packaged.yaml --stack-name sam-test --capabilities CAPABILITY_IAM

CloudFormation_-_スタック_sam-test-2.png

参考

AWS サーバーレスアプリケーションモデル (AWS SAM) の使用 - AWS Lambda
Installing the AWS SAM CLI on macOS - AWS Serverless Application Model
brew tapとは - Qiita

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