aws cloudformation packageコマンド
AWS SAMテンプレートを利用してローカルPCにあるzipファイルをS3にアップロードします。
ヘッダ
SAMテンプレートのヘッダは以下です。hogehogeは説明文ですので任意です。
AWSTemplateFormatVersion: 2010-09-09 Transform: AWS::Serverless-2016-10-31 Description: hogehoge
リソース
SAMテンプレートのリソースは5つあります。
| タイプ | デプロイ内容 |
|---|---|
| AWS::Serverless::Function | AWS Lambdaをデプロイ |
| AWS::Serverless::Api | API Gatewayをデプロイ |
| AWS::Serverless::SimpleTable | Amazon DynamoDBのテーブルをデプロイ |
| AWS::Serverless::LayerVersion | Lambda Layerをデプロイ |
| AWS::Serverless::Application | Serverless Application Repositoryに存在するアプリケーションをデプロイ |
aws cloudformation packageコマンドを使ってローカルPCにあるファイルをS3にアップロードすることが出来ます。
SAMテンプレートを作成します。ここでは「sam.yml」とします。Lambdaデプロイまでしてみます。
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Description: setumei
Resources:
TestFunc:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./src/index.zip
Handler: index.handler
Runtime: nodejs12.x
aws cloudformation packageコマンドを下記の通り実行します。
C:\tmp>aws cloudformation package --template-file sam.yml --s3-bucket バケット名 --output-template-file output.yml Uploading to 13a2e4d76e98b3988e602631e143a8cf 11985405 / 11985405.0 (100.00%) Successfully packaged artifacts and wrote output template to file output.yml. Execute the following command to deploy the packaged template aws cloudformation deploy --template-file c:\tmp\output.yml --stack-name <YOUR STACK NAME>
コマンドを実行するとuuidのような32桁の乱数が表示されています。ここでは「13a2e4d76e98b3988e602631e143a8cf」です。このファイル(中身は同じ)が指定バケットに配置されます。
メッセージにあるように、次にaws cloudformation deployコマンドを実行すればLambdaデプロイが完了します。
output.ymlは以下のようになります。
AWSTemplateFormatVersion: 2010-09-09
Description: setumei
Resources:
TestFunc:
Properties:
CodeUri: s3://バケット名/13a2e4d76e98b3988e602631e143a8cf
Handler: index.handler
Runtime: nodejs12.x
Type: AWS::Serverless::Function
Transform: AWS::Serverless-2016-10-31
自動生成されたこのファイルをテンプレートとしてaws cloudformation deployコマンドを実行します。
aws cloudformation deploy --template-file C:\zip\output.yml --stack-name stackname --capabilities CAPABILITY_IAM Waiting for changeset to be created.. Waiting for stack create/update to complete Successfully created/updated stack - stackname
これでstacknameというスタックが作成され、Lambdaが作成されていることが確認できます。
packageしてからdeployすれば、毎回S3にアップロードしたzipファイルの内容でデプロイされるようになります。
–parameter-overrides
aws cloudformation packageコマンドに--parameter-overridesオプションはありません。
詳細はaws cloudformation package helpで確認することが出来ます。
aws cloudformation deployコマンドで--parameter-overridesオプションが使えるので、AWS SAMテンプレートに以下のようにParametersをつけると使えます。
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Description: This CloudFormation template to create Lambda function
Parameters:
Stage:
Description: Stage name
Type: String
Resources:
MyLambdaFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName:
!Join
- '-'
- - !Ref Stage
- 'getData'
CodeUri: ../zip/index.zip
Handler: index.handler
Runtime: nodejs12.x
MemorySize: 128
Timeout: 15
!JoinでハイフンでStageとgetDataを連結しています。
上記ではLambda名はStage=devの場合、dev-getDataとなります。
package & deploy
「sam packageしてaws cloudformation deployでlambdaデプロイを行う」参照
sam deploy が sam package の機能を暗黙的に実行するようになりました。sam deploy コマンドを直接使用して、アプリケーションをパッケージ化およびデプロイできます。

KHI入社して退社。今はCONFRAGEで正社員です。関西で140-170/80~120万から受け付けております^^
得意技はJS(ES20xx),Java,AWSの大体のリソースです
コメントはやさしくお願いいたします^^
座右の銘は、「狭き門より入れ」「願わくは、我に七難八苦を与えたまえ」です^^


コメント