備忘録
(まとめたら結構単純になったけど、実際に悩んでいたr時はもう少し複雑なケースだった)
以下のようなケースで、リソースの管理を行いたかったので、いろいろ試行錯誤した結果をまとめる。
- とあるリソース(ここの例では、aws_ecr_repository)を条件によって複数作成する
- 環境変数 var.environment = true の時は、api-v2のリポジトリを作成する
- 環境変数 var.environment = false の時は、api-v2のリポジトリは作成しない
コード
locals {
  repositories = {
    api1-v1 = {
      name                 = "api-v1"
    },
    api-v2 = var.environment ? {
      name                 = "api-v2"
    } : {},
  }
}
resource "aws_ecr_repository" "example" {
  for_each = local.repositories
  name                 = each.value.name
  image_tag_mutability = "IMMUTABLE"
  image_scanning_configuration {
    scan_on_push = true
  }
}
variable "environment" {
  description = "環境を指定します。"
  default     = false
}
コメント