<p>It seems like the intent of this expression is to take the result of encoding that data structure which contains numbers and boolean values in quotes and produce a new version with those values unquoted.</p>

<p>For example, anywhere the result contains the sequence <code>"true"</code> it would be replaced with <code>true</code>, quotes removed to produce a real JSON boolean value rather than a string.</p>

<p>A more direct way to achieve that result would be to set the values in <code>cadvisor_container_definition</code> to be of the intended type immediately:</p>

<pre><code>locals {
  cadvisor_container_definition = {
    name         = local.cadvisor_container
    image        = "google/cadvisor:latest"
    portMappings = [
      {
        containerPort = 8080
        hostPort      = 8080
      },
    ]
    mountPoints = [
      {
        sourceVolume  = "root"
        containerPath = "/rootfs"
        readOnly      = true
      },
      {
        sourceVolume  = "var_run"
        containerPath = "/var/run"
        readOnly      = false
      },
      {
        sourceVolume  = "sys"
        containerPath = "/sys"
        readOnly      = true
      },
      {
        sourceVolume  = "var_lib_docker"
        containerPath = "/var/lib/docker"
        readOnly      = true
      },
      {
        sourceVolume  = "cgroup",
        containerPath = "/sys/fs/cgroup"
        readOnly      = true
      }
    ]
    memory            = var.cadvisor_memory[var.env]
    memoryReservation = var.cadvisor_memory_reservation[var.env]
  }
}
</code></pre>

<p>I changed the literal values in the above to their numeric or boolean equivalents. I can't see from what you shared whether <code>memory</code> and <code>memoryReservation</code> are also supposed to be numbers, but if they are then that can be arranged by writing <code>tonumber(var.cadvisor_memory[var.env])</code> and <code>tonumber(var.cadvisor_memory_reservation[var.env])</code>.</p>

<p>The above examples use Terraform 0.12 syntax. It's possible that the configuration you are looking at here is written for Terraform 0.11 instead, which would explain this unusual string substitution technique because the Terraform 0.11 version of <code>jsonencode</code> suffered from Terraform 0.11's tendency to convert all primitive-typed values to strings. If you are still using Terraform 0.11 then the strange expression you found is likely still needed, but if you are using Terraform 0.12 then a change like I described above should work to simplify the confusing expression to just a straightforward <code>jsonencode</code>:</p>

<pre><code>jsonencode([local.cadvisor_container_definition])
</code></pre>

CC BY-SA 4.0
2019-11-22T17:45:31.267Z
2019-11-22T17:45:31.267Z
Martin Atkins
<p>So i was going through a code of some one else and just could not make sense of it,, if may be some can of this,</p>

<p><code>"${replace(jsonencode(list(local.cadvisor_container_definition)), "/\"(\\d{2,5}|true|false)\"/", "$1")}"</code></p>

<pre><code>locals {
  cadvisor_container_definition = {
    name         = "${local.cadvisor_container}"
    image        = "google/cadvisor:latest"
    portMappings = [
      {
        containerPort = "8080",
        hostPort = "8080"
      }
    ]
    mountPoints = [
      {
        sourceVolume  = "root",
        containerPath = "/rootfs",
        readOnly      = "true"
      },
      {
        sourceVolume  = "var_run",
        containerPath = "/var/run",
        readOnly      = "false"
      },
      {
        sourceVolume  = "sys",
        containerPath = "/sys",
        readOnly      = "true"
      },
      {
        sourceVolume  = "var_lib_docker",
        containerPath = "/var/lib/docker",
        readOnly      = "true"
      },
      {
        sourceVolume  = "cgroup",
        containerPath = "/sys/fs/cgroup",
        readOnly      = "true"
      }
    ]
    memory            = "${lookup(var.cadvisor_memory, var.env)}"
    memoryReservation = "${lookup(var.cadvisor_memory_reservation, var.env)}"
  }
}```
</code></pre>

<terraform><terraform-module>
Unable to understand the logic behind this terraform function
Mohammed Ali
# Unable to understand the logic behind this terraform function

(Asked by: username_0 on 2019-11-20)

So i was going through a code of some one else and just could not make sense of it,, if may be some can of this,
"${replace(jsonencode(list(local.cadvisor_container_definition)), "/\"(\\d{2,5}|true|false)\"/", "$1")}"
locals {
  cadvisor_container_definition = {
    name         = "${local.cadvisor_container}"
    image        = "google/cadvisor:latest"
    portMappings = [
      {
        containerPort = "8080",
        hostPort = "8080"
      }
    ]
    mountPoints = [
      {
        sourceVolume  = "root",
        containerPath = "/rootfs",
        readOnly      = "true"
      },
      {
        sourceVolume  = "var_run",
        containerPath = "/var/run",
        readOnly      = "false"
      },
      {
        sourceVolume  = "sys",
        containerPath = "/sys",
        readOnly      = "true"
      },
      {
        sourceVolume  = "var_lib_docker",
        containerPath = "/var/lib/docker",
        readOnly      = "true"
      },
      {
        sourceVolume  = "cgroup",
        containerPath = "/sys/fs/cgroup",
        readOnly      = "true"
      }
    ]
    memory            = "${lookup(var.cadvisor_memory, var.env)}"
    memoryReservation = "${lookup(var.cadvisor_memory_reservation, var.env)}"
  }
}```

**Tags:** terraform, terraform-module


---

## 1 answers

### Answer by username_1 on 2019-11-22. Score: 1

It seems like the intent of this expression is to take the result of encoding that data structure which contains numbers and boolean values in quotes and produce a new version with those values unquoted.
For example, anywhere the result contains the sequence "true" it would be replaced with true, quotes removed to produce a real JSON boolean value rather than a string.
A more direct way to achieve that result would be to set the values in cadvisor_container_definition to be of the intended type immediately:
locals {
  cadvisor_container_definition = {
    name         = local.cadvisor_container
    image        = "google/cadvisor:latest"
    portMappings = [
      {
        containerPort = 8080
        hostPort      = 8080
      },
    ]
    mountPoints = [
      {
        sourceVolume  = "root"
        containerPath = "/rootfs"
        readOnly      = true
      },
      {
        sourceVolume  = "var_run"
        containerPath = "/var/run"
        readOnly      = false
      },
      {
        sourceVolume  = "sys"
        containerPath = "/sys"
        readOnly      = true
      },
      {
        sourceVolume  = "var_lib_docker"
        containerPath = "/var/lib/docker"
        readOnly      = true
      },
      {
        sourceVolume  = "cgroup",
        containerPath = "/sys/fs/cgroup"
        readOnly      = true
      }
    ]
    memory            = var.cadvisor_memory[var.env]
    memoryReservation = var.cadvisor_memory_reservation[var.env]
  }
}

I changed the literal values in the above to their numeric or boolean equivalents. I can't see from what you shared whether memory and memoryReservation are also supposed to be numbers, but if they are then that can be arranged by writing tonumber(var.cadvisor_memory[var.env]) and tonumber(var.cadvisor_memory_reservation[var.env]).
The above examples use Terraform 0.12 syntax. It's possible that the configuration you are looking at here is written for Terraform 0.11 instead, which would explain this unusual string substitution technique because the Terraform 0.11 version of jsonencode suffered from Terraform 0.11's tendency to convert all primitive-typed values to strings. If you are still using Terraform 0.11 then the strange expression you found is likely still needed, but if you are using Terraform 0.12 then a change like I described above should work to simplify the confusing expression to just a straightforward jsonencode:
jsonencode([local.cadvisor_container_definition])


---

