AWS CloudFormation
コードでインフラ構築
YAML/JSON テンプレートでインフラを記述します。CloudFormation はテンプレート(stack)に従ってすべてのリソースを作成、更新、削除します。
- コスト$ 安い
- 難易度●●●●●
- 未使用でも課金?いいえ
- AWS マネージド
- 出題試験SAA · DVA · SOA
たとえるなら施工図面のようなものです。図面を渡せば正確に同じ建物が建ち、何度建て直しても同じになります。
CloudFormation はシステムのどこにある?
CloudFormation が呼び出す
具体例でステップごとに
- テスト環境を作るたびに、チームはコンソールで何十もの手順を手作業で行い、毎回何かが抜けてしまいます。
- VPC、ALB、Auto Scaling グループ、データベースを記述した YAML テンプレートを書きます。
- テンプレートからスタックを作成すると、CloudFormation が依存関係の順にリソースを作ります。
- インスタンスサイズを変えたいときは、テンプレートを編集し、変更セットで何が変わるかを確認してから適用します。
- 途中で失敗するとスタックは自動でロールバックし、スタックを削除するとすべて片付きます。DeletionPolicy Retain を付けたデータベースだけは残ります。
いつ使う?
✓ 使うべき場面
再現可能な環境(dev/staging/prod)を作成し、コードでインフラをレビューしたい場合。
✗ 避けるべき場面
プログラミング言語で書きたい → CDK(CloudFormation を生成します)。
よくある使い方
- まったく同じ dev / staging / prod 環境を作成
- コードによるインフラ、pull request でレビュー
- SAM または CDK による serverless のデプロイ
- 複数のアカウントと Region 向けの StackSets
CloudFormation・Systems Manager・Organizations の違い
| 役割 | テンプレートからインフラを作成・更新 | マシンの運用:コマンド実行、パッチ、アクセス | 多数の AWS アカウントの管理 |
|---|---|---|---|
| 対象 | AWS リソース(VPC、EC2、RDS など) | EC2 とオンプレミスのマシン | アカウントとポリシー |
| 再現性 | あり、同じテンプレートで同じインフラ | あり、ドキュメントとランブック | あり、OU と SCP |
| 料金 | AWS リソースについては無料 | ほとんど無料 | 無料 |
料金はいくら?
- 課金方式: AWS リソースでは無料です。サードパーティの extension を使う場合のみ課金されます(約 $0.0009/操作)。
- 無料枠: 無料です。
料金面の落とし穴はありません。リスクは stack を削除するとリソースも削除されることです(DeletionPolicy で保持できます)。
多数のアカウント・リージョンに同じインフラを展開するなら StackSets、変更の事前確認なら変更セット、スタック削除時にデータベースを残すなら DeletionPolicy: Retain、テンプレート外の手動変更の検出ならドリフト検出です。テンプレートで唯一必須のセクションは Resources です。
本番の試験問題に挑戦
1An administrator is using Amazon CloudFormation to deploy a three tier web application that consists of a web tier and application tier that will utilize Amazon DynamoDB for storage. When creating the CloudFormation template, which of the following would allow the application instance access to the DynamoDB tables without exposing API credentials?
- ACreate an Identity and Access Management Role that has the required permissions to read and write from the required DynamoDB table and associate the Role to the application instances by referencing an instance profile.
- BUse the Parameter section in the Cloud Formation template to have the user input Access and Secret Keys from an already created IAM user that has the permissions required to read and write from the required DynamoDB table.
- CCreate an Identity and Access Management Role that has the required permissions to read and write from the required DynamoDB table and reference the Role in the instance profile property of the application instance.
- DCreate an identity and Access Management user in the CloudFormation template that has permissions to read and write from the required DynamoDB table, use the GetAtt function to retrieve the Access and secret keys and pass them to the application instance through user-data.
答えを見る
正解: C
DynamoDB テーブルを読み書きできる IAM ロールを作り、テンプレート内のインスタンスのインスタンスプロファイルプロパティで参照します。アクセスキーをパラメータに入れたり、テンプレートで生成したりしてはいけません。
2You need to create a JSON-formatted text file for AWS CloudFormation. This is your first template and the only thing you know is that the templates include several major sections but there is only one that is required for it to work. What is the only section required?
- AMappings.
- BOutputs.
- CResources.
- DConditions.
答えを見る
正解: C
CloudFormation テンプレートで唯一必須のセクションは Resources で、少なくとも 1 つのリソースを宣言します。Mappings、Outputs、Conditions は任意です。
よくある質問
CloudFormation と CDK、Terraform の違いは何ですか?
CloudFormation は YAML または JSON のテンプレートでインフラを記述します。CDK は TypeScript、Python、Java などで書き、CloudFormation テンプレートを生成します。Terraform は HashiCorp のツールで、HCL という言語を使い、複数のクラウドに対応しています。
変更セットとドリフト検出は何に使いますか?
変更セットは、スタックを更新する前に、どのリソースが作成・変更・置き換えされるかを確認するためのものです。ドリフト検出は、CloudFormation の外で手動変更され、テンプレートと一致しなくなったリソースを見つけます。
CloudFormation に料金はかかりますか?
AWS のリソースについては料金はかからず、スタックが作成するリソースの料金のみがかかります。サードパーティのリソースやフックを無料枠を超えて使った場合にのみ料金が発生します。
AWS 認定試験では CloudFormation について何が問われますか?
複数のアカウントとリージョンへ展開する StackSets、変更セット、スタック削除時にデータベースを残す DeletionPolicy、失敗時のロールバック、スタック間で共有するパラメータと出力、EC2 を設定する cfn-init やユーザーデータなどです。