Unable to retrieve subnet_Id – Terraform.

huangapple go评论71阅读模式
英文:

Unable to retrieve subnet_Id - Terraform

问题

I've created a subnet via Terraform

resource "azurerm_subnet" "subnet" {
  depends_on = [ azurerm_virtual_network.vnet ]
  name                 = lower("${var.application_name}-subnet-${var.location_short}-${var.environment}")
  resource_group_name  = azurerm_resource_group.networking_rg.name
  virtual_network_name = azurerm_virtual_network.vnet.name
  address_prefixes     = ["10.0.14.224/27"] # -> 10.0.14.255
  service_endpoints    = ["Microsoft.Storage"]
  delegation {
    name = "AFADelegation"
    service_delegation {
      name    = "Microsoft.DBforPostgreSQL/flexibleServers"
      actions = ["Microsoft.Network/virtualNetworks/subnets/join/action"]
    }
  }
}

I'm trying to output the subnetId of the above, so I can add my App Service into the private network, however, when outputting subnetId:

output "subnet_id" {
  value = module.private_network.subnet_id
}

I don't actually get an Id, instead I get

"/subscriptions/2XXXXX-XXXX-XXX-XXXX-XXXXXX/resourceGroups/rg-networking-ae-dev-rg/providers/Microsoft.Network/virtualNetworks/rg-vnet-ae-dev/subnets/pn-subnet-ae-dev"

Why can't I get the Id?

英文:

I've created a subnet via Terraform

resource "azurerm_subnet" "subnet" {
  depends_on = [ azurerm_virtual_network.vnet ]
  name                 = lower("${var.application_name}-subnet-${var.location_short}-${var.environment}")
  resource_group_name  = azurerm_resource_group.networking_rg.name
  virtual_network_name = azurerm_virtual_network.vnet.name
  address_prefixes     = ["10.0.14.224/27"] #-> 10.0.14.255
  service_endpoints    = ["Microsoft.Storage"]
  delegation {
    name = "AFADelegation"
    service_delegation {
      name    = "Microsoft.DBforPostgreSQL/flexibleServers"
      actions = ["Microsoft.Network/virtualNetworks/subnets/join/action"]
    }
  }
}

I'm trying to output the subnetId of the above, so I can add my App Service into the private network, however, when outputting subnetId:

output "subnet_id" {
  value = module.private_network.subnet_id
}

I don't actually get an Id, instead I get

> "/subscriptions/2XXXXX-XXXX-XXX-XXXX-XXXXXX/resourceGroups/rg-networking-ae-dev-rg/providers/Microsoft.Network/virtualNetworks/rg-vnet-ae-dev/subnets/pn-subnet-ae-dev"

Why can't I get the Id?

答案1

得分: 1

Sure, here is the translated content:

我实际上没有得到一个 Id,而是得到了

"/subscriptions/2XXXXX-XXXX-XXX-XXXX-XXXXXX/resourceGroups/rg-networking-ae-dev-rg/providers/Microsoft.Network/virtualNetworks/rg-vnet-ae-dev/subnets/pn-subnet-ae-dev"

为什么我不能获取到 Id?

当您使用 Terraform 检查 Subnet ID 时获得的值是 Azure 资源管理器 格式的资源标识符。它包括 订阅 ID资源组 名称、虚拟网络名称子网名称。它仍然是一个有效的标识符,可以在 Azure 中唯一标识子网资源。

输出:

Unable to retrieve subnet_Id – Terraform.

如果您特别想从资源标识符中提取出子网名称,可以使用 elementsplit 函数来提取子网基本名称。

现在,当您运行 terraform apply 时,输出的 subnet_id 将包含从完整资源标识符中提取的子网基本名称。

注意:子网 ID 是 Azure 资源管理器格式中的有效标识符。您也可以从门户中验证相同的值。如果您想在其他地方使用子网 ID,则需要传递整个值。

输出:

Unable to retrieve subnet_Id – Terraform.

参考链接: 使用输出变量查询数据

Please note that I have only translated the content and excluded the code parts, as per your request.

英文:

> I don't actually get an Id, instead I get
> > "/subscriptions/2XXXXX-XXXX-XXX-XXXX-XXXXXX/resourceGroups/rg-networking-ae-dev-rg/providers/Microsoft.Network/virtualNetworks/rg-vnet-ae-dev/subnets/pn-subnet-ae-dev"

> Why can't I get the Id?

The value you obtained when you check the Subnet ID using terraform for subnet_id is the resource identifier of the subnet in Azure Resource Manager format. It includes the subscription ID, resource group name, virtual network name, and subnet name. it is still a valid identifier that uniquely identifies the subnet resource within Azure.

Output:

Unable to retrieve subnet_Id – Terraform.

If you specifically want to extract only the subnet name from the resource identifier, you can use the element and split functions to extract the subnet base name.

Now, when you run terraform apply, the output subnet_id will contain the base name of the subnet extracted from the full resource identifier.

> Note: Subnet ID is a valid identifier in azure resource manager format. You can also verify the same value from the portal. If you want to use the Subnet ID elsewhere, you need to pass the entire value.

Output:

Unable to retrieve subnet_Id – Terraform.

Reference: Query Data with Output Variables

huangapple
  • 本文由 发表于 2023年7月18日 06:55:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708550.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定