0

We have developed terraform code for deploying Application Gateway few years ago. Around that time, it was working fine.

But now, it is showing error "Unresolved Reference redirect" in code

 dynamic "redirect_configuration" {

    for_each = [for x in var.request_routing_rule_set : x if x.**redirect**]
    
content {
      name                 = redirect_configuration.value["redirect_conf_name"]

      redirect_type        = "Permanent"

      target_listener_name = redirect_configuration.value["target_listener_name"]

      include_path         = true

      include_query_string = true

and Error "Unresolved Reference name" in this application gateway module

resource "azurerm_key_vault_secret" "backend_address_pool" {

  for_each     = { for idx, record in var.backend_address_pool_name_list : idx => record }

  name         = each.value.**name**

  value        = format("%s/backendAddressPools/%s", azurerm_application_gateway.cluster_agw.id, each.value.**name**)

  key_vault_id = var.key_vault_id

The var file was having before.

variable "request_routing_rule_set" {
  description = "request_routing_rule_set"

variable "backend_address_pool_name_list" {
  description = "backend_address_pool_list"
}

How to eliminate this error? I dont want to add backend address pools in var file. Because it is already mentioned in our TEST Env as

backend_address_pool_name_list = [
    {
      name = local.pool_name
    },
1
  • Try using try(x.redirect, false) in your for_each to safely handle missing redirect fields and avoid unresolved reference errors which you're facing @ARB Commented Apr 11 at 7:39

1 Answer 1

0

Provisioning Azure Application Gateway template while using for_each in terraform.

As per the error description, I can see two errors as of now: Unresolved Reference redirect & Unresolved Reference name, which are two different blockers for two different plugins.

In the dynamic configuration, the representation assumes that each item in request_routing_rule_set has a redirect property. So, any element in the dynamic configuration that doesn't possess this key will result in the blockers mentioned above.

So, try to include those elements in the representation:

for_each = [for x in var.request_routing_rule_set : x if try(x.redirect, false)]

Coming to the second blocker as per the plugin:

If local.pool_name is defined in locals {} somewhere like this, then:

locals {
  pool_name = "my-backend-pool”
}

Then your input is fine, and you don’t need to change anything in the var file.

Or, if you want to completely eliminate the var:

You can make use of the for-each as mentioned below:

    locals {
      backend_address_pool_name_list = [
        {
          name = local.pool_name
        }
      ]
    }

By the use of this, you can remove the variable defined for backend_address_pool_name_list.

Refer:

https://discuss.hashicorp.com/t/nested-dynamic-blocks-and-using-a-try-function-call-on-the-nested-dynamic-somehow/23207.

https://discuss.hashicorp.com/t/combining-local-variables-with-for-each/31092.

https://www.env0.com/blog/terraform-dynamic-blocks.

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.