Reading through Consul keys data in terraform

291 Views Asked by At

I have a problem where I am trying to read the values from terraform data Consul keys:

These are the two data path I need to read to get a specific value:

Path 1: private/plt-network/infrastructure/eu-west-1/vpc-layout where if env=="development" && name contains *mesh then dev_vpc_id == id

[
  {
    "cidr_block": "0.0.0.0/17",
    "environment": "development",
    "id": "vpc-xxx",
    "name": "development"
  },
  {
    "cidr_block": "10.113.0.0/19",
    "environment": "development",
    "id": "vpc-yyy",
    "name": "development-mesh",
  }
]

Path 2: private/plt-network/infrastructure/ap-south-1/global/vpc-layout where if env=="development" && mesh == false then dev_vpc_id == id

[
  {
    "environment": "acceptance",
    "id": "vpc-xyz",
    "mesh": false
  },
  {
    "environment": "development",
    "id": "vpc-abc",
    "mesh": true
  },
]

The data export looks like below:

data "consul_keys" "vpcs-id" {
#  for_each = toset(local.aws_regions)
  key {
    name = "vpcs-all"
    path = "private/plt-network/infrastructure/eu-west-1/vpc-layout"
#    path = each.value == "eu-west-1" ? "${local.consul_base_path}/plt-network/infrastructure/${each.value}/vpc-layout" : "private/plt-network/infrastructure/${each.value}/vpc-layout/global"
  }
}

locals.tf

locals {
  aws_regions = toset(["eu-west-1", "eu-central-1", "us-east-2", "us-west-2", "ap-south-1", "ap-southeast-1"])
  consul_base_path     = "private/plt-network/infrastructure"
  eu-west-1-json = jsondecode(data.consul_keys.vpcs-id.var.vpcs_all)
  ap-south-1-json = jsondecode(data.consul_keys.vpcs-id-ap-south.var.vpcs_all)
  eu-west-1-vpc-ids = local.eu-west-1-json.id
  ap-south-1-vpc-ids = [for user in local.ap-south-1-json : user.id]

outputs.tf

output "eu-west-1-id" {
  value = local.eu-west-1-vpc-ids
}

output "ap-south-1-id" {
  value = local.ap-south-1-vpc-ids
}

Error:

Error: Missing map element

  on locals.tf line 8, in locals:
   8:   eu-west-1-json = jsondecode(data.consul_keys.vpcs-id.var.vpcs_all)
    |----------------
    | data.consul_keys.vpcs-id.var is map of string with 1 element

This map does not have an element with the key "vpcs_all".


Error: Missing map element

  on locals.tf line 9, in locals:
   9:   ap-south-1-json = jsondecode(data.consul_keys.vpcs-id-ap-south.var.vpcs_all)
    |----------------
    | data.consul_keys.vpcs-id-ap-south.var is map of string with 1 element

This map does not have an element with the key "vpcs_all".

Can someone help please?

0

There are 0 best solutions below