Terrain interpolation for map creation


username

I'm struggling with interpolation, with some variables below.

primary = ["foo.dev","all.bar.com"]
secondary = ["module.foo.dev","*.foo.dev","all.bar.com"]

I want my output to be

{
     "foo.dev" = ["module.foo.dev","*foo.dev"]
     "all.bar.com" = ["all.bar.com"]
}

Using Terraform 0.12.20

I tried it in the terraform console but couldn't achieve the desired output. Is there any simple solution?

[for i in ["foo.dev","all.bar.com"]: map(i,[for j in ["module.foo.dev","*foo.dev","all.bar.com"]: replace(j,i,"")!=j == true ? j : 0])]
[
  {
    "foo.dev" = [
      "module.foo.dev",
      "*foo.dev",
      "0",
    ]
  },
  {
    "all.bar.com" = [
      "0",
      "0",
      "all.bar.com",
    ]
  },
]
Matt Schuchard

You can do this with the following expression:

{ for i in ["foo.dev", "all.bar.com"] : i => [for j in ["module.foo.dev", "*foo.dev", "all.bar.com"] : j if can(regex(i, j))] }

The result is the following values:

{
  "all.bar.com" = [
    "all.bar.com",
  ]
  "foo.dev" = [
    "module.foo.dev",
    "*foo.dev",
  ]
}

Exactly what you asked for.

Iteration is explained as follows:

First, specify the type as a map:

{}

Now, we should iterate over the elements in the first list:

{ for i in ["foo.dev", "all.bar.com"] : }

Next, we assign ivalues ​​to the map's keys, and initialize a list of that value:

{ for i in ["foo.dev", "all.bar.com"] : i => [] }

However, we need to assign the values ​​of the elements in the second list to the list of keys of the resulting map (the following example is sub-optimal because we don't need foran expression, but can be used for the next step):

{ for i in ["foo.dev", "all.bar.com"] : i => [for j in ["module.foo.dev", "*foo.dev", "all.bar.com"] : j] }

Finally, we want to filter the elements in the list value based on the key. We can use a canfunction outside the regexfunction to return a boolean based on whether there is a match. We use this as a condition to determine whether the elements of the second list should be added to the list of values:

{ for i in ["foo.dev", "all.bar.com"] : i => [for j in ["module.foo.dev", "*foo.dev", "all.bar.com"] : j if can(regex(i, j))] }

We have found a solution.

Related


Terrain Condition Creation

David Sumner I have a Terraform file that creates resource groups, storage accounts, storage shares, databases and VMs on Azure. My suggested use case is that in production, once resource groups, storage accounts, storage shares and databases are created, they

Terrain Condition Creation

David Sumner I have a Terraform file that creates resource groups, storage accounts, storage shares, databases and VMs on Azure. My suggested use case is that in production, once resource groups, storage accounts, storage shares and databases are created, they

Terrain cycle in the map list

Nibals I have a Terraform module that produces a list of map outputs: object_ids = [ { "object_id" = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx" "upn" = "[email protected]" "user" = "john" }, { "object_id" = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx" "u

Terrain interpolation in marker keys

username I use Terraform to create a name based VM count.index. I want to create labels which are dynamically based on count.index. I just can't get it to work. For VM01 the tag should be "PatchCycle01=centos" and for VM02 it should be "PatchCycle02=centos".

Terrain interpolation for map creation

username I'm struggling with interpolation, with some variables below. primary = ["foo.dev","all.bar.com"] secondary = ["module.foo.dev","*.foo.dev","all.bar.com"] I want my output to be { "foo.dev" = ["module.foo.dev","*foo.dev"] "all.bar.com" = ["

Terrain Multilevel Map

7 Powell 79 When trying to use a multi-level map (3+ levels), I'm getting an error in the Terrain Plan that doesn't seem to solve the problem completely. Error: "The given value is invalid for variable "secgroups": element "bastion": attribute "direction" is r

Terrain - Inverted Map

mccleton I'm stuck trying to write a terraform expression that does this: subnets = { my_subnet_1 = { nsg = "my_nsg_1", addresses = "my_addresses_1" } my_subnet_2 = { nsg = "my_nsg_2", addresses = "my_addresses_2" }

Controlling Dynamic Terrain Creation in Unity

Wojtek Wencel I'm creating a game where the player always moves forward along a path. The terrain consists of line segments (there are always 9), and I have a script that creates a line segment relative to another line segment. Now, I want to create a script t

Terrain Condition Creation

David Sumner I have a Terraform file that creates resource groups, storage accounts, storage shares, databases and VMs on Azure. My suggested use case is that in production, once resource groups, storage accounts, storage shares and databases are created, they

Terrain interpolation in marker keys

username I use Terraform to create a name based VM count.index. I want to create labels which are dynamically based on count.index. I just can't get it to work. For VM01 the tag should be "PatchCycle01=centos" and for VM02 it should be "PatchCycle02=centos".

Terrain interpolation for map creation

username I'm struggling with interpolation, with some variables below. primary = ["foo.dev","all.bar.com"] secondary = ["module.foo.dev","*.foo.dev","all.bar.com"] I want my output to be { "foo.dev" = ["module.foo.dev","*foo.dev"] "all.bar.com" = ["

Terrain - Inverted Map

mccleton I'm stuck trying to write a terraform expression that does this: subnets = { my_subnet_1 = { nsg = "my_nsg_1", addresses = "my_addresses_1" } my_subnet_2 = { nsg = "my_nsg_2", addresses = "my_addresses_2" }

Terrain - Inverted Map

mccleton I'm stuck trying to write a terraform expression that does this: subnets = { my_subnet_1 = { nsg = "my_nsg_1", addresses = "my_addresses_1" } my_subnet_2 = { nsg = "my_nsg_2", addresses = "my_addresses_2" }

Terrain Multilevel Map

7 Powell 79 When trying to use a multi-level map (3+ levels), I'm getting an error in the Terrain Plan that doesn't seem to solve the problem completely. Error: "The given value is invalid for variable "secgroups": element "bastion": attribute "direction" is r

Terrain Condition Creation

David Sumner I have a Terraform file that creates resource groups, storage accounts, storage shares, databases and VMs on Azure. My suggested use case is that in production, once resource groups, storage accounts, storage shares and databases are created, they

Terrain Condition Creation

David Sumner I have a Terraform file that creates resource groups, storage accounts, storage shares, databases and VMs on Azure. My suggested use case is that in production, once resource groups, storage accounts, storage shares and databases are created, they

Terrain cycle in the map list

Nibals I have a Terraform module that produces a list of map outputs: object_ids = [ { "object_id" = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx" "upn" = "[email protected]" "user" = "john" }, { "object_id" = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx" "u

Terrain interpolation for map creation

username I'm struggling with interpolation, with some variables below. primary = ["foo.dev","all.bar.com"] secondary = ["module.foo.dev","*.foo.dev","all.bar.com"] I want my output to be { "foo.dev" = ["module.foo.dev","*foo.dev"] "all.bar.com" = ["

Terrain cycle in the map list

Nibals I have a Terraform module that produces a list of map outputs: object_ids = [ { "object_id" = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx" "upn" = "[email protected]" "user" = "john" }, { "object_id" = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx" "u

Terrain interpolation in marker keys

username I use Terraform to create a name based VM count.index. I want to create labels which are dynamically based on count.index. I just can't get it to work. For VM01 the tag should be "PatchCycle01=centos" and for VM02 it should be "PatchCycle02=centos".

Terrain interpolation in marker keys

username I use Terraform to create a name based VM count.index. I want to create labels which are dynamically based on count.index. I just can't get it to work. For VM01 the tag should be "PatchCycle01=centos" and for VM02 it should be "PatchCycle02=centos".

Terrain interpolation for map creation

username I'm struggling with interpolation, with some variables below. primary = ["foo.dev","all.bar.com"] secondary = ["module.foo.dev","*.foo.dev","all.bar.com"] I want my output to be { "foo.dev" = ["module.foo.dev","*foo.dev"] "all.bar.com" = ["

Terrain Multilevel Map

7 Powell 79 When trying to use a multi-level map (3+ levels), I'm getting an error in the Terrain Plan that doesn't seem to solve the problem completely. Error: "The given value is invalid for variable "secgroups": element "bastion": attribute "direction" is r

Terrain - Inverted Map

mccleton I'm stuck trying to write a terraform expression that does this: subnets = { my_subnet_1 = { nsg = "my_nsg_1", addresses = "my_addresses_1" } my_subnet_2 = { nsg = "my_nsg_2", addresses = "my_addresses_2" }

Terrain Condition Creation

David Sumner I have a Terraform file that creates resource groups, storage accounts, storage shares, databases and VMs on Azure. My suggested use case is that in production, once resource groups, storage accounts, storage shares and databases are created, they

Terrain Condition Creation

David Sumner I have a Terraform file that creates resource groups, storage accounts, storage shares, databases and VMs on Azure. My suggested use case is that in production, once resource groups, storage accounts, storage shares and databases are created, they

Terrain interpolation in marker keys

username I use Terraform to create a name based VM count.index. I want to create labels which are dynamically based on count.index. I just can't get it to work. For VM01 the tag should be "PatchCycle01=centos" and for VM02 it should be "PatchCycle02=centos".

Terrain interpolation in marker keys

username I use Terraform to create a name based VM count.index. I want to create labels which are dynamically based on count.index. I just can't get it to work. For VM01 the tag should be "PatchCycle01=centos" and for VM02 it should be "PatchCycle02=centos".

Terrain interpolation for map creation

username I'm struggling with interpolation, with some variables below. primary = ["foo.dev","all.bar.com"] secondary = ["module.foo.dev","*.foo.dev","all.bar.com"] I want my output to be { "foo.dev" = ["module.foo.dev","*foo.dev"] "all.bar.com" = ["