これはなに?
AWS CloudFormationワークショップのパッケージ化とデプロイを学習したのでメモ。
AWS CloudFormationでつかえる便利なコマンド3つ
aws cloudformation package
LambdaコードなどをS3にアップロードし、その保存先をCloudFormationテンプレート内に書いてくれる機能
PythonFunction: Type: AWS::Lambda::Function Properties: (略) Code: lambda/ # 現ディレクトリ内の、Lambdaが格納されたディレクトリを記載
aws cloudformation package
コマンドを実行する
$ aws cloudformation package \ --template-file infrastructure.template \ --s3-bucket example-bucket-name \ --s3-prefix cfn-workshop-package-deploy \ --output-template-file infrastructure-packaged.template
Code:
に書かれたディレクトリをZIPで圧縮し、S3へ保存、保存先をCode:
のところに記載してくれる
aws cloudformation validate-template
CloudFormationテンプレートの検証を行うコマンド。
cfn-lint
でテンプレートの構文チェックはしてくれるが、それとどう違うのだろう?
$ aws cloudformation validate-template \ --template-body file://infrastructure-packaged.template (結果) { "Parameters": [], "Description": "AWS CloudFormation workshop - Package and deploy.", "Capabilities": [ "CAPABILITY_IAM" ], "CapabilitiesReason": "The following resource(s) require capabilities: [AWS::IAM::Role]"
aws cloudformation deploy
AWSのWebコンソールからデプロイしていたCloudFormationテンプレートですが、aws cliからデプロイすることもできます。
$ aws cloudformation deploy \ --template-file infrastructure-packaged.template \ --stack-name cfn-workshop-package-deploy-lambda \ --region ap-northeast-1 \ --capabilities CAPABILITY_IAM
※--capabilities CAPABILITY_IAM
IAMにリソースを作成するなどの可能性があることを認識していることを示すため、つける引数。
AWSのWebコンソールでCloudFormationテンプレートをデプロイするときにも聞かれ、チェックを入れているが、それを引数で指定している。