0

We have a module that creates a redis instance in GCP. I'm calling that from the example folder where I define the variables. This works fine. This redis instance will output a tuple of IPs that I want to use to create consul service via a submodule. The submodule requires the IP as a string, so I'd need to somehow iterate through either with the output from the module / terraform state or data resource that grabs the info with a for each to the submodule but I'm a bit lost!

main.tf
output.tf
variables.tf
submodule.tf
└── example
   ├── main.tf
   └── output.tf

example/main.tf

module "create_redis_instance_example" {
  source  = "../."
  stage   = "eng"
  redis_instances = {
    test1 = {
      memory_size_gb = 1
      tier           = "BASIC"
      redis_config = {
        maxmemory-policy = "allkeys-lru"
      }
    }
  }
  register_consul = true
  consul_servicenames = [
    "test1",
  ]
}

example/output.tf

output "redis_instances" {
  value     = module.create_redis_instance_example.redis_instances
}

main.tf

resource "google_redis_instance" "cache" {
  provider = google-beta
  for_each = var.redis_instances

  name               = "${var.stage}-${each.key}"
  display_name       = "${var.stage}-${each.key}"
  memory_size_gb     = each.value["memory_size_gb"]
  auth_enabled       = true
  authorized_network = test
  connect_mode       = "PRIVATE_SERVICE_ACCESS"
  redis_configs           = each.value["redis_config"]
  tier                    = each.value["tier"]
}

output.tf

output "redis_instances" {
  value = toset([
    for i in google_redis_instance.cache : i.host
  ])
}

variables.tf

variable "stage" {
  description = "Stage of the redis instance"
  type        = string
}

variable "redis_instances" {
  description = "A list of bucket-suffixes with optional information"
  type = map(object({
    memory_size_gb = number
    tier           = string
    redis_config   = map(any)
  }))
}

etc. etc.

submodule.tf

module "terraform_consul_dns" {
  for_each = var.redis_instances
  depends_on = [google_redis_instance.cache]
 
  #ipaddress = "${google_redis_instance.cache[host]}"
  #ipaddress = "${google_redis_instance.cache[each.value].host}"
  #ipaddress = "${var.google_redis_instances[each.value.host]}"
  ipaddress = "1.1.1.1"
  nodename  = "test1-${var.stage}-${each.key}"
  services_with_ports = [
    {
      servicename = "test1-${var.stage}-${each.key}"
      port        = "1234"
    },
  ]
}

So when we specify the IP:

ipaddress = "1.1.1.1"

This works fine but as mentioned we'd like to create this either with the output, terraform state or data source dynamically. Tried a few things as you can see above but nothing really works :-( any hints, much welcomed!

1 Answer 1

1

There is no need to gather this from the output/tf state as I can directly get this from the module that is creating the resource and than call the submodule, so all that needed to be changed was:

submodule.tf

module "terraform_consul_dns" {
  for_each = google_redis_instance.cache
  depends_on = [google_redis_instance.cache]

  ipaddress = google_redis_instance.cache[each.KEY].host
}

So we made the mistake to grab value instead of key. As value had no host attribute but key has.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.