Alastair Montgomery
Alastair Montgomery

Reputation: 1856

Combine two lists into a formatted string in Terraform

I need to create a string parameter to pass to the aws-cli via local-exec so need to combine two lists from the remote state into the required format, cannot think of a good way to do this using the inbuilt interpolation functions.

Required string format

"SubnetId=subnet-x,Ip=ip_x SubnetId=subnet--y,Ip=ip_y SubnetId=subnet-z,Ip=ip_z"

We have the subnets and corresponding cidrs in two separate lists.

["subnet-x","subnet-y","subnet-z"]
["cidr-x","cidr-y","cidr-z"]

Was thinking I could use the cidrhost function to obtain the IPs but cannot see a way to format the two lists into the one string.

Upvotes: 1

Views: 4530

Answers (2)

rclement
rclement

Reputation: 1714

Try using formatlist followed by join.

locals {
   # this should give you 
   formatted_list = "${formatlist("SubnetId=%s,Ip=%s", var.subnet_list, var.cidrs_list}"

   # combine the formatted list of parameter together using join
   cli_parameter = "${join(" ", locals.formatted_list)}"
}

EDIT: You will need to use a null_resource to convert the CIDRs to IP Addresses like in the other answer. Then you can just build the formatted_list and cli_parameter similar to before.

locals {
   subnet_list = ["subnet-x","subnet-y","subnet-z"]
   cidr_list = ["cidr-x","cidr-y","cidr-z"]

   # this should give you 
   formatted_list = "${formatlist("SubnetId=%s,Ip=%s", var.subnet_list, null_resource.cidr_host_convert.*.triggers.value)}"

   # combine the formatted list of parameter together using join
   cli_parameter = "${join(" ", locals.formatted_list)}"
}

resource "null_resource" "cidr_host_convert" {
   count = "${length(locals.cidr_list}"

   trigger = {
      # for each CIDR, get the first IP Address in it. You may need to manage
      # the index value to prevent overlap
      desired_ips = "${cidrhost(locals.cidr_list[count.index], 1)}"
   }
}

Upvotes: 3

Alastair Montgomery
Alastair Montgomery

Reputation: 1856

One of the guys at work came up with this,

 variable "subnet_ids" {
   default = ["subnet-345325", "subnet-345243", "subnet-345234"]
 }

 variable "cidrs" {
   default = ["10.0.0.0/24", "10.0.1.0/24", "10.0.2.0/23"]
 }

 resource "null_resource" "subnet_strings_option_one" {
   count = "${length(var.subnet_ids)}"

   triggers {
     value = "SubnetId=${var.subnet_ids[count.index]},Ip=${cidrhost(var.cidrs[count.index],11)}"
   }
 }

 output "subnet_strings_option_one" {
   value = "${join("",null_resource.subnet_strings_option_one.*.triggers.value)}"
 }

This gives the following output

    null_resource.subnet_strings_option_one[1]: Creating...
      triggers.%:     "" => "1"
      triggers.value: "" => "SubnetId=subnet-345243,Ip=10.0.1.11"
    null_resource.subnet_strings_option_one[2]: Creating...
      triggers.%:     "" => "1"
      triggers.value: "" => "SubnetId=subnet-345234,Ip=10.0.2.11"
    null_resource.subnet_strings_option_one[0]: Creating...
      triggers.%:     "" => "1"
      triggers.value: "" => "SubnetId=subnet-345325,Ip=10.0.0.11"
    null_resource.subnet_strings_option_one[2]: Creation complete after 0s (ID: 852839482792384695)
    null_resource.subnet_strings_option_one[1]: Creation complete after 0s (ID: 5439264637705543321)
    null_resource.subnet_strings_option_one[0]: Creation complete after 0s (ID: 1054498808481879719)

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

    Outputs:

    subnet_strings_option_one = SubnetId=subnet-345325,Ip=10.0.0.11 SubnetId=subnet-345243,Ip=10.0.1.11 SubnetId=subnet-345234,Ip=10.0.2.11

Upvotes: 1

Related Questions