1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Terraformでリソース作成を制御するパターン

Last updated at Posted at 2025-07-12

はじめに

Terraformでリソースを作成する際に、環境や条件に応じて作成するかどうかの制御が必要な場合があります。本記事では、実際の使用場面に応じた複数のパターンをご紹介します。

必ずしも特定の場面で決まった使い方をする必要はありませんが、開発時の参考にしていただければ幸いです。

以下のバージョンを基準にサンプルコードを作成しています。

  • terraform: 1.9.8
  • hashicorp/aws: 5.83.1

環境識別子による制御

使用場面

特定の環境(本番環境など)にのみリソースを作成する場合に使用できます。

サンプルコード

variables.tf
variable "env" {
  type        = string
  description = "Environment identifier"
}

environments/prd.tfvars
env = "prd"

environments/dev.tfvars
env = "dev"

main.tf
resource "aws_instance" "example" {
  count = var.env == "prd" ? 1 : 0

  ami           = "ami-xxxxxx"
  instance_type = "t2.micro"
}

フラグ変数による制御

使用場面

以下のようなケースで使用できます。

  • 現時点では特定の環境にのみ作成するが、将来的に他の環境でも作成する可能性がある
  • 特定の環境で作成したリソースが不要になり、削除する可能性がある

サンプルコード

variables.tf
variable "create_aws_instance" {
  type        = bool
  description = "Flag to create aws_instance"
}

environments/prd.tfvars
create_aws_instance = true

environments/dev.tfvars
create_aws_instance = false

main.tf
resource "aws_instance" "example" {
  count = var.create_aws_instance ? 1 : 0

  ami           = "ami-xxxxxx"
  instance_type = "t2.micro"
}

最初はprd環境にのみaws_instanceを作成していますが、dev環境にも作成する必要が出てきた場合、リソースブロック内の条件分岐を修正するのではなく、フラグ変数を変更します。

environments/dev.tfvars
# falseからtrueに変更
create_aws_instance = true

もう1つのパターンとしては、変数を1か0として定義し、count文で条件分岐を記述せずに変数をそのまま参照する方法です。

environments/dev.tfvars
create_aws_instance = 1

main.tf
resource "aws_instance" "example" {
  count = var.create_aws_instance

  ami           = "ami-xxxxxx"
  instance_type = "t2.micro"
}

ループ処理での制御

使用場面

1つのリソースブロック内でfor_eachなどを使用して複数のリソースを作成している場合、直接count文による制御はできません。その代わりに、for_eachに条件分岐を記述し、作成しない場合は空のオブジェクトを渡します。

サンプルコード

variables.tf
variable "create_aws_instance" {
  type        = bool
  description = "Flag to create aws_instance"
}

variable "aws_instances" {
  type = map(object{
    ami           = string
    instance_type = string
  })
  description = "Configs of aws_instances"
  default     = {}
}

environments/dev.tfvars
create_aws_instance = false

environments/prd.tfvars
create_aws_instance = true

aws_instances = {
  web_server = {
    ami           = "ami-xxxx"
    instance_type = "t2.micro"
  }

  ap_server = {
    ami           = "ami-oooo"
    instance_type = "t2.small"
  }
}
main.tf
resource "aws_instance" "example" {
  for_each = var.create_aws_instance ? var.aws_instances : {}

  ami           = each.value.ami
  instance_type = each.value.instance_type
}

複数のフラグ変数による制御

使用場面

複数のリソースがあり、それぞれのフラグ変数で作成するかどうかを制御している場合に使用できます。
これらのリソースの中で、1つでも作成されたら、それらとは別のリソースを作成したい場合、local変数を活用できます。

サンプルコード

variables.tf
variable "create_web_server" {
  type        = bool
  description = "Flag to create web server"
}

variable "create_app_server" {
  type        = bool
  description = "Flag to create application server"
}

variable "create_db_server" {
  type        = bool
  description = "Flag to create database server"
}

locals.tf
locals {
  # いずれかのサーバーが作成される場合にtrueになる
  create_monitoring_server = (
    var.create_web_server ||
    var.create_app_server ||
    var.create_db_server
  ) ? true : false
}
environments/prd.tfvars
create_web_server = true
create_app_server = true
create_db_server  = true

environments/dev.tfvars
create_web_server = true
create_app_server = false
create_db_server  = false

main.tf
resource "aws_instance" "web_server" {
  count         = var.create_web_server ? 1 : 0
  ami           = "ami-xxxx"
  instance_type = "t2.micro"
  
  tags = {
    Name = "web-server"
  }
}

resource "aws_instance" "app_server" {
  count         = var.create_app_server ? 1 : 0
  ami           = "ami-oooo"
  instance_type = "t2.small"
  
  tags = {
    Name = "app-server"
  }
}

resource "aws_instance" "db_server" {
  count         = var.create_db_server ? 1 : 0
  ami           = "ami-iiii"
  instance_type = "t2.medium"
  
  tags = {
    Name = "db-server"
  }
}

# いずれかのサーバーが作成される場合にのみ監視サーバーを作成
# 今回の変数の値の設定ではdev環境もprd環境もmonitoring_serverが作成される
resource "aws_instance" "monitoring_server" {
  count         = local.create_monitoring_server ? 1 : 0
  ami           = "ami-jjjj"
  instance_type = "t2.micro"
  
  tags = {
    Name = "monitoring-server"
  }
}

まとめ

今回はTerraformでリソースの作成を制御する方法として、以下のパターンを紹介しました。

  • 環境識別子による制御: 特定の環境にのみリソースを作成
  • フラグ変数による制御: 柔軟な作成制御と将来的な拡張性を考慮
  • ループ処理での制御: for_eachを使用した複数リソースの一括制御
  • 複数のフラグ変数による制御: 複数の条件を組み合わせた制御

本記事では全てのパターンをまとめているわけではありません。また、プロジェクトの要件や運用方針に応じて、適切なパターンを選択して実装していただければと思います。

参考URL

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?