Help us understand the problem. What is going on with this article?

terraform で入れ子ループ

More than 1 year has passed since last update.

TL;DR

code

variable "asc" {
  type = "list"

  default = [
    "a",
    "b",
    "c",
    "d",
  ]
}

variable "desc" {
  type = "list"

  default = [
    "z",
    "y",
    "x",
  ]
}

// want
// a_z, a_y, a_x,
// b_z, b_y, b_x,
// c_z, c_y, c_x,
// d_z, d_y, d_x

locals {
  counter = {
    asc  = "${length(var.asc)}"
    desc = "${length(var.desc)}"
  }
}

// 要諦はasc(4)*desc(3)=count(12), index/asc(4)の余りとindex/asc(4)
resource "local_file" "foo" {
  count    = "${local.counter["asc"] * local.counter["desc"]}"
  content  = "ok"
  filename = "${path.module}/file_${var.asc[count.index % local.counter["asc"]]}-${var.desc[count.index / local.counter["asc"]]}.txt"
}

蛇足

  • 正確には terraform が順番どおりにやってくれないので入れ子ループとは呼びにくい
  • AWS で VPC Peering を考えた時にVPC間のルートテーブルとサブネットのIDをぶん回す時に使用した
  • もっとも、最終的にそれはVPC CIDRで接続することにしたのでお蔵入りになったのだけれど
  • 捨てるのももったいないのでサンプルにして残してみた
raki
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした