LoginSignup
2
1

個人的に管理しやすいterraformディレクトリ設計

Last updated at Posted at 2023-01-25

個人的に管理しやすいterraformのディレクトリを考えてみました。
3レイヤードアーキテクチャとかありますが、最小限の個人的に使ってるディレクトリの構造を紹介します。

ディレクトリ構造

.
├── entrypoint #entry point。基本ここをapplyすればok
│   └── <プロジェクトの名前> #プロジェクトの名前を記述し、必要な環境分ディレクトリを作成する
│       ├── dev
│       │   └── main.tf #環境別にterraform applyする
│       ├── prod
│       │   ├── main.tf
│       │   ├── <credential>.json #credentialはここに置く
│       └── stg
│           └── main.tf
├── modules //各モジュールを配置する
│   ├── aws
│   └── gcp
│       ├── gcr
│       ├── gcs
│       ├── gke
│       ├── networking
│       └── redis
└── readme.md //ドキュメント

entrypoint//<環境名>/main.tfの中のtemplate

# 環境別の変数を入れる
locals {
  gcp_project = "gcpプロジェクトID"
  env         = "dev"
  project     = "プロジェクト名"
  region      = "asia-northeast1"
  zone        = "asia-northeast1-a"
}

terraform {
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "4.50.0"
    }
  }
}

provider "google" {
  credentials = file("<credentialを入れる>.json")
  project     = local.gcp_project
  region      = local.region
  zone        = local.zone
}


#####ここからmoduleを呼び出す。
module "networking" {
  source  = "../../../modules/networking" #相対パス注意
  project = local.project
  env     = local.env
}

Terraform Cloudを利用した記事

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1