プロジェクトフォルダの作成

本章の概要で確認したとおり、モジュールごとにファイルを分割した構成でインフラを構築します。まず、その土台となるプロジェクトフォルダとディレクトリ構成を作成します。

任意の場所にterraform-noteappフォルダを作成し、Visual Studio Codeの「ファイル」→「フォルダーを開く」から、作成したterraform-noteappフォルダを開きます。

terraform-noteapp  ← このフォルダを作成

続いて、以下のディレクトリ構成を作成します。今回は必要なフォルダとファイルが多いため、コマンドで一括作成します。

terraform-noteapp/
├── modules/
│   ├── remote/
│   ├── base/
│   ├── api/
│   └── frontend/
├── environments/
│   └── test/
└── app/

Visual Studio Codeのターミナルを開き、terraform-noteappフォルダ内で以下のコマンドを実行してください。フォルダと空のファイルがすべて一括で作成されます。

Windowsの場合:

フォルダを作成します。

mkdir modules\remote modules\base modules\api modules\frontend environments\test app

空のファイルを作成します。

type nul > modules\base\main.tf
type nul > modules\base\variables.tf
type nul > modules\base\outputs.tf
type nul > modules\api\main.tf
type nul > modules\api\variables.tf
type nul > modules\api\outputs.tf
type nul > modules\frontend\main.tf
type nul > modules\frontend\variables.tf
type nul > modules\frontend\outputs.tf
type nul > environments\test\backend.tf
type nul > environments\test\main.tf
type nul > environments\test\variables.tf
type nul > environments\test\outputs.tf
type nul > environments\test\terraform.tfvars

macOSの場合:

フォルダを作成します。

mkdir -p modules/remote modules/base modules/api modules/frontend environments/test app

空のファイルを作成します。

touch modules/base/main.tf \
      modules/base/variables.tf \
      modules/base/outputs.tf \
      modules/api/main.tf \
      modules/api/variables.tf \
      modules/api/outputs.tf \
      modules/frontend/main.tf \
      modules/frontend/variables.tf \
      modules/frontend/outputs.tf \
      environments/test/backend.tf \
      environments/test/main.tf \
      environments/test/variables.tf \
      environments/test/outputs.tf \
      environments/test/terraform.tfvars

Visual Studio Codeのエクスプローラーで、フォルダと空のファイルがすべて作成されていることを確認してください。

💡 ポイント
modules/remote/にはファイルを作成していません。このモジュールでは、importブロックの-generate-config-outオプションでコードを自動生成するため、事前にファイルを用意する必要がありません。importブロック自体はルートモジュール(environments/test/main.tf)に記述します。