Servisphere
AWS CloudFormation のアイコン(AWS Architecture Icons)

AWS CloudFormation

コードでインフラ構築

YAML/JSON テンプレートでインフラを記述します。CloudFormation はテンプレート(stack)に従ってすべてのリソースを作成、更新、削除します。

  • コスト$ 安い
  • 難易度●●●●●
  • 未使用でも課金?いいえ
  • AWS マネージド
  • 出題試験SAA · DVA · SOA

範囲: Regional

CloudFormation をマップで開く →
たとえるなら施工図面のようなものです。図面を渡せば正確に同じ建物が建ち、何度建て直しても同じになります。

CloudFormation はシステムのどこにある?

マップで全体を見る →

具体例でステップごとに

  1. テスト環境を作るたびに、チームはコンソールで何十もの手順を手作業で行い、毎回何かが抜けてしまいます。
  2. VPC、ALB、Auto Scaling グループ、データベースを記述した YAML テンプレートを書きます。
  3. テンプレートからスタックを作成すると、CloudFormation が依存関係の順にリソースを作ります。
  4. インスタンスサイズを変えたいときは、テンプレートを編集し、変更セットで何が変わるかを確認してから適用します。
  5. 途中で失敗するとスタックは自動でロールバックし、スタックを削除するとすべて片付きます。DeletionPolicy Retain を付けたデータベースだけは残ります。

いつ使う?

✓ 使うべき場面

再現可能な環境(dev/staging/prod)を作成し、コードでインフラをレビューしたい場合。

✗ 避けるべき場面

プログラミング言語で書きたい → CDK(CloudFormation を生成します)。

よくある使い方

  • まったく同じ dev / staging / prod 環境を作成
  • コードによるインフラ、pull request でレビュー
  • SAM または CDK による serverless のデプロイ
  • 複数のアカウントと Region 向けの StackSets

CloudFormation・Systems Manager・Organizations の違い

CloudFormationSystems ManagerOrganizations
役割テンプレートからインフラを作成・更新マシンの運用:コマンド実行、パッチ、アクセス多数の AWS アカウントの管理
対象AWS リソース(VPC、EC2、RDS など)EC2 とオンプレミスのマシンアカウントとポリシー
再現性あり、同じテンプレートで同じインフラあり、ドキュメントとランブックあり、OU と SCP
料金AWS リソースについては無料ほとんど無料無料

料金はいくら?

料金の例$0(stack が作成したリソースの料金を支払います)。
  • 課金方式: AWS リソースでは無料です。サードパーティの extension を使う場合のみ課金されます(約 $0.0009/操作)。
  • 無料枠: 無料です。
⚠️ 料金の落とし穴

料金面の落とし穴はありません。リスクは stack を削除するとリソースも削除されることです(DeletionPolicy で保持できます)。

料金は us-east-1 の参考値(2026年10月14日 更新)で、変更されている可能性があります。必ず AWS の料金ページでご確認ください。 AWS 公式の料金ページ ↗

💡 試験のコツ

多数のアカウント・リージョンに同じインフラを展開するなら StackSets、変更の事前確認なら変更セット、スタック削除時にデータベースを残すなら DeletionPolicy: Retain、テンプレート外の手動変更の検出ならドリフト検出です。テンプレートで唯一必須のセクションは Resources です。

本番の試験問題に挑戦

AWS 認定試験は英語でも受験できるため、問題文は英語のまま掲載しています。

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?

  1. 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.
  2. 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.
  3. 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.
  4. 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?

  1. AMappings.
  2. BOutputs.
  3. CResources.
  4. DConditions.
答えを見る

正解: C

CloudFormation テンプレートで唯一必須のセクションは Resources で、少なくとも 1 つのリソースを宣言します。Mappings、Outputs、Conditions は任意です。

Servisphere で 700 問以上を練習する →

よくある質問

CloudFormation と CDK、Terraform の違いは何ですか?

CloudFormation は YAML または JSON のテンプレートでインフラを記述します。CDK は TypeScript、Python、Java などで書き、CloudFormation テンプレートを生成します。Terraform は HashiCorp のツールで、HCL という言語を使い、複数のクラウドに対応しています。

変更セットとドリフト検出は何に使いますか?

変更セットは、スタックを更新する前に、どのリソースが作成・変更・置き換えされるかを確認するためのものです。ドリフト検出は、CloudFormation の外で手動変更され、テンプレートと一致しなくなったリソースを見つけます。

CloudFormation に料金はかかりますか?

AWS のリソースについては料金はかからず、スタックが作成するリソースの料金のみがかかります。サードパーティのリソースやフックを無料枠を超えて使った場合にのみ料金が発生します。

AWS 認定試験では CloudFormation について何が問われますか?

複数のアカウントとリージョンへ展開する StackSets、変更セット、スタック削除時にデータベースを残す DeletionPolicy、失敗時のロールバック、スタック間で共有するパラメータと出力、EC2 を設定する cfn-init やユーザーデータなどです。

関連サービス

CloudFormation をマップで開く →