Eduard Ahmatgareev
Eduard Ahmatgareev

Reputation: 51

terraform and list, split returned the last element for unknown index

Faced with an issue during working on terraform code, wrote a little example and hope somebody of you will help me to resolve my issue.

Current code looks like:

variable "aws-subnets" {
  description = "Test rule"

  default = [
    "10.0.0.0/32:us-east-1-s3",
    "10.0.0.1/32:us-east-2-s3",
    "10.0.0.2/32",
    "",
  ]
}

data "template_file" "ipset-wrapper" {
  count = "${length(var.aws-subnets)}"

  template = <<TXT
$${SUBNET_RANGE} comment $${SUBNET_COMMENT}
TXT

  vars {
    SUBNET_RANGE = "${element(split(":",var.aws-subnets[count.index]),0) != "" ? "${element(split(":",var.aws-subnets[count.index]),0)}" : "no_ip" }"
    SUBNET_COMMENT = "${element(split(":",var.aws-subnets[count.index]),1) != "" ? "${element(split(":",var.aws-subnets[count.index]),1)}" : "no_comment_provided" }"
),2))}"
  }
}

output "ipset-rules" {
  value = "${data.template_file.ipset-wrapper.*.rendered}"
}

The output now:

Outputs:

ipset-rules = [
    10.0.0.0/32 comment us-east-1-s3
,
    10.0.0.1/32 comment us-east-2-s3
,
    10.0.0.2/32 comment 10.0.0.2/32
,
    no_ip comment 0      no_comment_provided

]

3d string, instead of return unknown element or element empty, returned the last element from split command, that's why condition:

${element(split(":",var.aws-subnets[count.index]),1) != ""}

doesn't work for me as expected

what is the best approach in that case to write a code, if second index wasn't defined?

I understand that code:

    SUBNET_COMMENT = "${element(split(":",var.aws-subnets[count.index]),1) != element(split(":",var.aws-subnets[count.index]),0) ? "${element(split(":",var.aws-subnets[count.index]),1)}" : "no_comment_provided" }"

works well in that case, but if won't work if I will have more than 2 indexes.

any help?

Upvotes: 2

Views: 8195

Answers (1)

DJAlPee
DJAlPee

Reputation: 962

How about this using new TF 0.12 syntax:

variable "aws-subnets" {
  description = "Test rule"
  type = map(string)
  default = {
    "10.0.0.0/32" = "us-east-1-s3",
    "10.0.0.1/32" = "us-east-2-s3",
    "10.0.0.2/32" = "",
    "" = "",
  }
}

data "template_file" "ipset-wrapper" {
  for_each = var.aws-subnets

  template = <<TXT
$${SUBNET_RANGE} comment $${SUBNET_COMMENT}
TXT

  vars = {
    SUBNET_RANGE   = each.key != "" ? each.key : "no_ip"
    SUBNET_COMMENT = each.value != "" ? each.value : "no_comment_provided"
  }
}

output "ipset-rules" {
  value = data.template_file.ipset-wrapper
}

Results in:

$ terraform apply
data.template_file.ipset-wrapper["10.0.0.0/32"]: Refreshing state...
data.template_file.ipset-wrapper["10.0.0.1/32"]: Refreshing state...
data.template_file.ipset-wrapper["10.0.0.2/32"]: Refreshing state...
data.template_file.ipset-wrapper[""]: Refreshing state...

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

ipset-rules = {
  "" = {
    "id" = "a30b544d33f455799f3b6d105d31eee10f326869f483ce22fd0300a56dd18c70"
    "rendered" = "no_ip comment no_comment_provided\n"
    "template" = "${SUBNET_RANGE} comment ${SUBNET_COMMENT}\n"
    "vars" = {
      "SUBNET_COMMENT" = "no_comment_provided"
      "SUBNET_RANGE" = "no_ip"
    }
  }
  "10.0.0.0/32" = {
    "id" = "21372ccabc2941e632a3e7fc8db7629c743689fb8318b8219135468366edd21b"
    "rendered" = "10.0.0.0/32 comment us-east-1-s3\n"
    "template" = "${SUBNET_RANGE} comment ${SUBNET_COMMENT}\n"
    "vars" = {
      "SUBNET_COMMENT" = "us-east-1-s3"
      "SUBNET_RANGE" = "10.0.0.0/32"
    }
  }
  "10.0.0.1/32" = {
    "id" = "87ab002bf580b46c48d580ed613f84c8730c6c238b087c59d6e876ff4838b6f8"
    "rendered" = "10.0.0.1/32 comment us-east-2-s3\n"
    "template" = "${SUBNET_RANGE} comment ${SUBNET_COMMENT}\n"
    "vars" = {
      "SUBNET_COMMENT" = "us-east-2-s3"
      "SUBNET_RANGE" = "10.0.0.1/32"
    }
  }
  "10.0.0.2/32" = {
    "id" = "eaf64bde14c2d9e6921f6cb8f8297cf3335779450cddc734e9cb7f84849d742a"
    "rendered" = "10.0.0.2/32 comment no_comment_provided\n"
    "template" = "${SUBNET_RANGE} comment ${SUBNET_COMMENT}\n"
    "vars" = {
      "SUBNET_COMMENT" = "no_comment_provided"
      "SUBNET_RANGE" = "10.0.0.2/32"
    }
  }
}

Upvotes: 1

Related Questions