Terraformとは
クラウドやオンプレなどのリソースをコードで効率的に構築、変更、状態管理できるツールです。コードはGitなどでバージョン管理できます。
これをローカルでやってみようと思います。
想定
- Macで作業
- Dockerを使わずローカルでTerraformの操作
Terraformをインストール
- インストール
brew install terraform
- バージョン確認
terafform --version
main.tf
terraform {
required_providers {
local = {
source = "hashicorp/local"//プロバイダーの場所
version = "~> 2.0"
}
random = {
source = "hashicorp/random"//プロバイダーの場所
version = "~> 3.0"
}
}
}
# ランダムな文字列を生成
resource "random_pet" "server_name" {
length = 2
separator = "-"
}
# テキストファイルを作成
resource "local_file" "example" {
filename = "${path.module}/outputs/hello.txt"
content = "Hello from Terraform!\nServer name: ${random_pet.server_name.id}\nCreated at: ${timestamp()}"
}
# JSON設定ファイルを作成
resource "local_file" "config" {
filename = "${path.module}/outputs/config.json"
content = jsonencode({
server_name = random_pet.server_name.id
environment = "development"
ports = [80, 443, 8080]
enabled = true
})
}
- 補足
1.terraform { } ブロック
2.hashicorpはTerraformを作っている会社のこと
3.\n = 改行
terraform init前の注意
.gitignoreにこれを設定しないとものすごいファイルの
.terraform や *.tfstateみたいな巨大・バイナリ系をそのまま
pushしてしまい、pushできなくなってしまいます。
数MB〜数百MBクラスのバイナリファイルが入るからです。💦
.terraform/
└── providers/
└── registry.terraform.io/
└── hashicorp/
└── aws/5.69.0/darwin_arm64/terraform-provider-aws_v5.69.0_x5
こんな巨大なバイナリファイルは流石にGitHubも拒否したりします。
なので事前に.gitignoreに設定しておきました。
.terraform/
*.tfstate
*.tfstate.backup
*.tfvars
*.tfvars.json
terraform init
Terraform構成を含む作業ディレクトリを初期化します。
terraform init
初期化すると、terraform planやterraform applyなどの
他のコマンドを実行できるようになります。
terraform plan
このコマンドは実行したらこうなるっていうのを教えてくれるコマンドです。
terraform plan
terraform plan
出力結果
Terraform used the selected providers to generate the following execution plan.
Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# local_file.example will be created
+ resource "local_file" "example" {
+ content = (known after apply)
+ content_base64sha256 = (known after apply)
+ content_base64sha512 = (known after apply)
+ content_md5 = (known after apply)
+ content_sha1 = (known after apply)
+ content_sha256 = (known after apply)
+ content_sha512 = (known after apply)
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "./outputs/hello.txt"
+ id = (known after apply)
}
# local_file.server_name will be created
+ resource "local_file" "server_name" {
+ content = (known after apply)
+ content_base64sha256 = (known after apply)
+ content_base64sha512 = (known after apply)
+ content_md5 = (known after apply)
+ content_sha1 = (known after apply)
+ content_sha256 = (known after apply)
+ content_sha512 = (known after apply)
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "./outputs/config.json"
+ id = (known after apply)
}
# random_pet.server_name will be created
+ resource "random_pet" "server_name" {
+ id = (known after apply)
+ length = 2
+ separator = "-"
}
Plan: 3 to add, 0 to change, 0 to destroy.
───────────────────────────────────────────────────────────────────────────────────
Note: You didn't use the -out option to save this plan, so Terraform can't
guarantee to take exactly these actions if you run "terraform apply" now.
- 補足
Plan: 3 to add, 0 to change, 0 to destroy.
3つのリソースを新規作成する予定で変更・削除するものはないということです。
作成される3つのリソースは
- random_pet.server_name
- local_file.server_name
- local_file.example
です。
terraform apply
terraform plan(シュミレーション)したのを実行するコマンドです。
ファイルやリソースが実際に作られます。
terraform apply
yesと入力すると、planで表示された通りに実行されます。
terraform apply
以下のファイルが作成されます。
Hello name from Terraform
Server name: beloved-hagfish
Created at:2025-11-09T09:51:20Z
{"enabled":true,"enviroment":"develop","ports":[80,443,8080],"server_name":"beloved-hagfish"}
#ここでは省略
状態管理用のファイルで今回は.gitignoreにしているので、Gitには入らない。
terraform get(おまけ)
外部モジュールを取得するコマンド見たいですが、
terraform initで自動的にやってくれるので、使う機会がなさそうです。
terraform validate
- 構文チェック
terraform validate
- コード整形
terraform fmt
terraform destroy
terraform destroy
その後以下のことが起きます。
ファイルが削除され、
- terraform.tfstateの中身が空になります。
{
"version": 4,
"terraform_version": "1.x.x",
"serial": 2,
"lineage": "...",
"outputs": {},
"resources": [] ← 空になる
}
- terraform.tfstate.backupが作成される
- random_petの名前が消える
-
terraform applyを再度実行すると新しいランダムな名前が生成される
前回がcute-dogでも、次はbrave-monkeyなどになる
Terafformのコマンド集
ここにいろんなコマンドが記載されているので、いろいろ見てみてください。
AIに聞くだけでなく公式ドキュメントが1番です。
例えばterraform testとかも使う機会が少なさそうです。
公式ドキュメント
参考
ClaudeCode