ファイル構造

基本方針

environments/ディレクトリ配下に本番(prd)ステージング(stg)開発(dev)環境別のルートモジュールを配置し、各環境からmodules/ディレクトリ配下のVPC、SG、NACL、EC2等のリソース別の子モジュールを呼び出すことで、共通のインフラストラクチャコードを複数環境で再利用しながら、環境固有の設定値(terraform.tfvars)のみを変更することで環境差分を管理する、DRY原則に基づいたエンタープライズレベルのTerraformモジュール構成にする。

リソース管理の3原則

1. IAM権限の管理

各リソースに必要な実行ロールリソースベースポリシーは、各リソースモジュール内で管理します。これにより、リソースと権限の密結合を実現し、権限の過不足防止とモジュール単位での権限レビューが容易にする。

2. CloudWatch監視の管理

各リソースに必要なCloudWatch LogsCloudWatch Metricsは、各リソースモジュール内で管理します。これにより、リソースとログ・メトリクスの一元管理を実現し、監視設定の漏れを防止する。

3. セキュリティリソースの管理

SGNACLWAFは一括管理します。これらは複数のリソース間で共有・参照されるため、各module 配下で集中管理することで、セキュリティポリシーの統一性コンプライアンス要件への対応を容易にする。

  +- README.md
  |
  +- environments/                           # 環境別のルートモジュール
  |  |
  |  +- prd/                                 # 本番環境のルートモジュール
  |  |   |
  |  |   +- main.tf
  |  |   +- variables.tf
  |  |   +- backend.tf
  |  |   +- providers.tf
  |  |   +- terraform.tfvars
  |  |   |
  |  |   +templates/
  |  |     |
  |  |     +- user_data_xxxxxxxx.sh.tpl
  |  |
  |  +- stg/                                 # ステージング環境のルートモジュール
  |  |   |
  |  |   +- main.tf
  |  |   +- variables.tf
  |  |   +- backend.tf
  |  |   +- providers.tf
  |  |   +- terraform.tfvars
  |  |   |
  |  |   +templates/
  |  |     |
  |  |     +- user_data_xxxxxxxx.sh.tpl
  |  |
  |  +- dev/                                 # 開発環境のルートモジュール
  |      |
  |      +- main.tf
  |      +- variables.tf
  |      +- backend.tf
  |      +- providers.tf
  |      +- terraform.tfvars 
  |      +templates/
  |        |
  |        +- user_data_xxxxxxxx.sh.tpl
  |
  +- modules/                                # リソース別の子モジュール
      |
      +- vpc/                                # VPC、Subnet、RouteTable、IGW、NAT GW
      |   |
      |   +- main.tf
      |   +- variables.tf
      |   +- outputs.tf
      |   +- locals.tf
      |   +- README.md
      |
      +- nacl/                               # 全ての NACL を nacl モジュールで一括管理
      |   |
      |   +- main.tf
      |   +- variables.tf
      |   +- outputs.tf
      |   +- locals.tf
      |   +- README.md
      |
      +- sg/                                 # 全ての SG を sg モジュールで一括管理
      |   |
      |   +- main.tf
      |   +- variables.tf
      |   +- outputs.tf
      |   +- locals.tf
      |   +- README.md
      |
      +- ec2/                                # インスタンス
      |   |
      |   +- main.tf
      |   +- variables.tf
      |   +- outputs.tf
      |   +- locals.tf
      |   +- README.md
      |
      +- asg/                                # 起動テンプレート、Auto Scaling グループ
      |   |
      |   +- main.tf
      |   +- variables.tf
      |   +- outputs.tf
      |   +- locals.tf
      |   +- README.md
      |
      +- alb/                                # ターゲットグループ、リスナー
      |   |
      |   +- main.tf
      |   +- variables.tf
      |   +- outputs.tf
      |   +- locals.tf
      |   +- README.md
      |
      +- rds/                                # サブネットグループ、パラメータグループ
      |   |
      |   +- main.tf
      |   +- variables.tf
      |   +- outputs.tf
      |   +- locals.tf
      |   +- README.md
      |
      +- elasticache/                        # サブネットグループ、パラメータグループ
      |   |
      |   +- main.tf
      |   +- variables.tf
      |   +- outputs.tf
      |   +- locals.tf
      |   +- README.md
      |
      +- route53/                            # Route53 レコード定義、ACM
      |   |
      |   +- main.tf
      |   +- variables.tf
      |   +- outputs.tf
      |   +- locals.tf
      |   +- README.md
      |
      +- iam/                                # 管理者ユーザ、開発者ユーザ
      |   |                                  # クロスアカウント
      |   +- main.tf
      |   +- variables.tf
      |   +- outputs.tf
      |   +- locals.tf
      |   +- README.md
      |
      +- ecs/                                # クラスタ、タスク定義、サービス
      |   |
      |   +- main.tf
      |   +- variables.tf
      |   +- outputs.tf
      |   +- locals.tf
      |   +- README.md
      |
      +- eks/                                # クラスタ、ノードグループ、アドオン
      |   |
      |   +- main.tf
      |   +- variables.tf
      |   +- outputs.tf
      |   +- locals.tf
      |   +- README.md
      |
      +- lambda/                             # Lambda 関数
      |   |
      |   +- main.tf
      |   +- variables.tf
      |   +- outputs.tf
      |   +- locals.tf
      |   +- README.md
      |
      +- apigateway/                         # HTTP API、統合、ルート、ステージ
      |   |
      |   +- main.tf
      |   +- variables.tf
      |   +- outputs.tf
      |   +- locals.tf
      |   +- README.md
      |
      +- dynamodb/                           # DynamoDB テーブル 
      |   |
      |   +- main.tf
      |   +- variables.tf
      |   +- outputs.tf
      |   +- locals.tf
      |   +- README.md
      |
      +- s3/                                 # S3 バケット
      |   |
      |   +- main.tf
      |   +- variables.tf
      |   +- outputs.tf
      |   +- locals.tf
      |   +- README.md
      |
      +- cloudfront/                         # ディストリビューション
      |   |                                  # キャッシュポリシー、リクエストポリシー
      |   +- main.tf
      |   +- variables.tf
      |   +- outputs.tf
      |   +- locals.tf
      |   +- README.md
      |
      +- waf/                               # Web ACL、保護対象との紐づけ
      |   |
      |   +- main.tf
      |   +- variables.tf
      |   +- outputs.tf
      |   +- locals.tf
      |   +- README.md
      |
      +- cloudwatch/                        # 横断的なアラーム、ダッシュボード
          |
          +- main.tf
          +- variables.tf
          +- outputs.tf
          +- locals.tf
          +- README.md

ファイル名

役割ファイル名
状態管理terraform.tfstate
プロバイダーの定義providers.tf
バックエンドの定義backend.tf
変数のデフォルト値terraform.tfvars
リソースの定義main.tf
ローカル変数の定義locals.tf
変数の定義variables.tf
出力の定義outputs.tf