0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AWSのリソースをコード化する上で検討すべきTerraformのディレクトリ構造

Last updated at Posted at 2024-09-18

Terraformのベストなディレクトリ構造とは

これは非常に悩ましい内容だと思います。
まず私の場合は、Terraformの事を相談出来る相手がおらんとです・・・w
調べても調べてもどれがベストなのかは出てこず、
とりあえずシンプルな構成とモジュール構成の大きく分けて2通りだろうということは分かりました。

基礎中の基礎を学びたかったり、Terraformを触ってみたいといった方は
シンプル構成で良いのではないかと思います。
私もまずはコードの書き方を学ぶ必要があるのと、エンターキー1つでリソースが作成されていく気持ち良さを体験するためにシンプル構成でやりました。

下記に実際私が作ったものと類似した構成を記述します。

具体的なディレクトリ案

1.シンプルな構成 
簡単にかけるが流用性が低い

project/
├──envs
   ├──prd/
        ├── main.tf
        ├── variables.tf
        ├── outputs.tf
        └── provider.tf
   ├── dev/
        ├── main.tf
        ├── variables.tf
        ├── outputs.tf
        └── provider.tf

2.環境ごとにデプロイする構成

enviroment配下のmain.tfが大きくなりがちで、影響が出やすい

project/
├── main.tf
├── variables.tf
├── outputs.tf
├── provider.tf
└── enviroment
    ├── prd/
    ├── dev/
    ├── staging/
└── modules/
    ├── network/
    │   ├── main.tf
    │   ├── variables.tf
    │   └── outputs.tf
    ├── ec2/
    │   ├── main.tf
    │   ├── variables.tf
    │   └── outputs.tf
    └── security_group/
        ├── main.tf
        ├── variables.tf
        └── outputs.tf

3.リソースごとにデプロイする構成

最良か・・・?だが、環境によって違いが多ければproject配下のルートディレクトリにmain.tfを置かないでその配下に置く必要があるか

project/
├── provider.tf
└── resources
    ├── ec2/
    |    ├── prd/ 
    |    │   ├── main.tf
    │    ├── variables.tf
    |    │   └── outputs.tf
    |    │    ├── project1/
    |    │         ├── main.tf
    |    │         ├── variables.tf
    |    │         └── outputs.tf          
    |    │    ├── project2/
    |    │         ├── main.tf
    |    │         ├── variables.tf
    |    │         └── outputs.tf  
    |    ├── dev/
    |    │   ├── main.tf
    |    │   ├── variables.tf
    |    │   └── outputs.tf
    |    │    ├── project1/
    |    │         ├── main.tf
    |    │         ├── variables.tf
    |    │         └── outputs.tf      
    |    │    ├── project2/
    |    │         ├── main.tf
    |    │         ├── variables.tf
    |    │         └── outputs.tf  
    ├── network/
    |    ├── prd/ 
    |    │   ├── main.tf
    |    │   ├── variables.tf
    |    │   └── outputs.tf
    |    │    ├── project1/
    |    │         ├── main.tf
    |    │         ├── variables.tf
    |    │         └── outputs.tf          
    |    │    ├── project2/
    |    │         ├── main.tf
    |    │         ├── variables.tf
    |    │         └── outputs.tf          
    |    ├── dev/
    |    │   ├── main.tf
    |    │   ├── variables.tf
    |    │   └── outputs.tf
    |    │    ├── project1/
    |    │         ├── main.tf
    |    │         ├── variables.tf
    |    │         └── outputs.tf      
    |    │    ├── project2/
    |    │         ├── main.tf
    |    │         ├── variables.tf
    |    │         └── outputs.tf  

3のリソースごとの構成はあまり見かけないかも知れませんが、
main.tfに大量に書き込まないためデプロイがデストロが早いのと、
多人数で作業する時のstateファイルのデグレが起きないなど、メリットが多いとのこと。

感想

右も左も分からないので、まず出来るところから作っていって要件を意識して変更していくしかないと思ったんですよね。
会社によって規模も違いますし、どれくらい構成の変更やdestroyが必要かも分からないので、非常に難しいところです。

ではまた。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?