Home > Back-end >  Accessing list element in terraform output
Accessing list element in terraform output

Time:09-23

I have following code in terraform state file( pasted just part of state file which I consider relevant for this question), which is result of running terraform code pasted below as well:

  "mode": "managed",
      "type": "azurerm_vpn_gateway",
      "name": "azure_vpngw",
      "provider": "provider[\"registry.terraform.io/hashicorp/azurerm\"].azure_clusters",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "bgp_settings": [
              {
                "asn": 65515,
                "bgp_peering_address": "",
                "instance_0_bgp_peering_address": [
                  {
                    "custom_ips": [
                      "169.254.21.1"
                    ],
                    "default_ips": [
                      "10.255.176.12"
                    ],
                    "ip_configuration_id": "Instance0",
                    "tunnel_ips": [
                      "10.255.176.4",
                      "20.184.79.231"
                    ]

Relevant terraform code:

resource "azurerm_vpn_gateway" "azure_vpngw" {
  provider            = azurerm.azure_clusters
  name                = "azure_vpngw"
  location            = azurerm_resource_group.azure_networking.location
  resource_group_name = azurerm_resource_group.azure_networking.name
  virtual_hub_id      = azurerm_virtual_hub.azure_hub.id
  bgp_settings {
    asn         = 65515
    peer_weight = 50
    instance_0_bgp_peering_address {
      custom_ips = ["169.254.21.1"]
    }
    instance_1_bgp_peering_address {
      custom_ips = ["169.254.22.1"]
    }
  }
}

And I need to accesss last element in tunnel_ips list, so 20.184.79.231. I have been trying diff things like

azurerm_vpn_gateway.azure_vpngw.bgp_settings[0].instance_0_bgp_peering_address[0].tunnel_ips[0][1]

but it did not work. So, issue is in last part - tunnel_ips[0][1] I believe. If anybody has idea, it would be very wellcome.

CodePudding user response:

Since your code is not a valid TF code, I modified it to actually be such, and this is how you can access your ip:

locals {

    t = <<EOL
 {
  "mode": "managed",
  "type": "azurerm_vpn_gateway",
  "name": "azure_vpngw",
  "provider": "provider[azure_clusters",
  "instances": [{
    "schema_version": 0,
    "attributes": {
      "bgp_settings": [{
        "asn": 65515,
        "bgp_peering_address": "",
        "instance_0_bgp_peering_address": [{
          "custom_ips": [
            "169.254.21.1"
          ],
          "default_ips": [
            "10.255.176.12"
          ],
          "ip_configuration_id": "Instance0",
          "tunnel_ips": [
            "10.255.176.4",
            "20.184.79.231"
          ]
        }]
      }]
    }
  }]
 } 
EOL

    v = jsondecode(local.t)

}

output "test" {
  value = local.v["instances"][0]["attributes"]["bgp_settings"][0]["instance_0_bgp_peering_address"][0]["tunnel_ips"][1]
}

gives:

test = "20.184.79.231"

CodePudding user response:

I figure it out. So, to fetch IP and use it in another resource definition I did:

ip_address = sort(azurerm_vpn_gateway.azure_vpngw.bgp_settings[0].instance_0_bgp_peering_address[0].tunnel_ips)[1]
  • Related