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

More than 3 years have passed since last update.

terraformで2重ループする

1
Last updated at Posted at 2022-09-26

やりたいこと

このような感じで、各リポジトリがさらにユーザーごとに分かれて管理されているようなものを作りたい

vamdemic-k8s/dev/front/test01

サンプル

  • 配列を組み合わせて、その直積を求めることができるsetproductを使うとよい
  • この場合、repositoriy_namesとsubdomain_namesがそれぞれ3つずつなので、9この組み合わせが返される
    https://www.terraform.io/language/functions/setproduct
locals {
  system_name = "vamdemic-k8s"
  region      = "ap-northeast-1"

  repository_names = [
    "front",
    "app",
    "db",
  ]
  subdomain_names = [
    "test01",
    "test02",
    "test03",
  ]
}

for_eachでまわす

resource "aws_ecr_repository" "ecr" {
  for_each             = { for v in setproduct(local.repository_names, local.subdomain_names) : join(",", v) => v }
  name                 = "${local.system_name}/${terraform.workspace}/${each.value[0]}/${each.value[1]}"
  image_tag_mutability = "MUTABLE"
  force_delete         = true

  image_scanning_configuration {
    scan_on_push = true
  }
}

参考

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