Pass complex variable into module with consul_key_prefix.shared_config

Hello,

I have an issue of passing list variable which should get his value from consul_key_prefix.shared_config.

Our concept is using modules, and for each new resource that we need to use, we are creating a new sub module for it and using the resource by calling it as module.

our main.tf has aws provider definitions and consul_key for shared_config and a list of module:

"module": {
   "devex_ttrt_aurora_mysql": {
      "source": "terraform-aws-modules/rds-aurora/aws",
      "version": "~> 2.0",
      "name": "devex-ttrt-aurora-mysql",
      "engine": "aurora-mysql",
      "engine_version": "5.7.mysql_aurora.2.10.0",
      "username": null,
      "password": null,
      ....
      ...
      "subnets": "${local.subnet_id_db}",     ------> should get a list
   ....
    .... 
}

This is how i define in variable.tf local.subnet_id_db:

locals {
  subnet_path = "eu-west-1/production/subnets"
  db_list = ["db-a", "db-b", "db-c"]
}

locals {
  subnet_id_db = [ data.consul_key_prefix.shared_config.subkeys["${local.subnet_path}/${local.db_list[0]}/id"],
      data.consul_key_prefix.shared_config.subkeys["${local.subnet_path}/${local.db_list[1]}/id"],
      data.consul_key_prefix.shared_config.subkeys["${local.subnet_path}/${local.db_list[2]}/id"]
    ]

 }

From some reason i always get an empty map result:

Error: Invalid index

  on variables.tf line 19, in locals:
  19:   subnet_id_db = [ data.consul_key_prefix.shared_config.subkeys["${local.subnet_path}/${local.db_list[0]}/id"],
    |----------------
    | data.consul_key_prefix.shared_config.subkeys is empty map of string
    | local.db_list[0] is "db-a"
    | local.subnet_path is "eu-west-1/production/subnets"

The given key does not identify an element in this collection value.

Dont sure what i missing here, please advise.

Thanks

Hi @eliorso,

I think the key part of that message the following statement:

data.consul_key_prefix.shared_config.subkeys is empty map of string

The index lookup failed because there are no keys in this map at all, and thus there is no value that would be a valid index for this map.

I think the next step here would be to understand why data.consul_key_prefix.shared_config isn’t finding any keys in the Consul key/value store under your given prefix. For example, maybe the prefix isn’t correct, or maybe you’re asking this question of the wrong Consul datacenter (since key/value store is a per-datacenter idea, IIRC).

Thanks for the replay.

I will check it, but i dont think this is the case, because i can verify and see data in the consul_key_prefix.

is my syntax correct ? i did not missed anything ?