Terraform Azure 容器实例动态卷 - share_name 循环 azurerm_storage_share

Terraform Azure Container Instance Dynamic Volume - share_name loop over azurerm_storage_share

我有以下创建 Azure 存储文件共享的 Terraform 代码。

resource "azurerm_storage_share" "jms-sftp-share" {
  for_each             = toset(["one", "two", "three"])
  name                 = each.key
  quota                = 5120
  storage_account_name = azurerm_storage_account.working-storage_account.name

  acl {
    id = "${each.key}_this_is_my_id"

    access_policy {
      permissions = "rwl"
    }
  }
}

然后我尝试创建一个 azurerm_container_instance 和一个动态卷块,它通过遍历 azurerm_storage_share.jms-sftp-share 获取它的共享名称。

resource "azurerm_container_group" "jms-sftp" {
  dns_name_label = "doccji-dts-dev-jms-sftp"
  exposed_port = [
    {
      port     = 22
      protocol = "TCP"
    },
  ]
  location            = var.resource-location
  name                = "${local.resource-name-prefix}-sftp-1"
  os_type             = "Linux"
  resource_group_name = local.resource-group-name
  restart_policy      = "Always"
  tags                = merge(local.common_tags, tomap({ "type" = "docker-sftp-server" }))

  container {
    commands = []
    cpu      = 1
    image    = "atmoz/sftp:latest"
    memory   = 1.5
    name     = "jms-sftp-1"

    ports {
      port     = 22
      protocol = "TCP"
    }
    dynamic "volume" {
      for_each = [for v in azurerm_storage_share.jms-sftp-share : {
        name = v.name
      }]
      content {
        empty_dir            = false
        mount_path           = "/home/${volume.value.name}"
        name                 = "${volume.value.name}-home-folder"
        read_only            = false
        share_name           = azurerm_storage_share.jms-sftp-share[volume.value.name]
        storage_account_key  = azurerm_storage_account.working-storage_account.primary_access_key
        storage_account_name = azurerm_storage_account.working-storage_account.name
      }

    }
    volume {
      empty_dir            = false
      mount_path           = "/etc/sftp"
      name                 = "sftp-users-conf"
      read_only            = true
      share_name           = azurerm_storage_share.jms-sftp-users-share.name
      storage_account_key  = azurerm_storage_account.working-storage_account.primary_access_key
      storage_account_name = azurerm_storage_account.working-storage_account.name
    }
  }

  depends_on = [
    azurerm_storage_share.jms-sftp-share,
    azurerm_storage_share.jms-sftp-users-share
  ]
}

我遇到了以下错误:

Error: Incorrect attribute value type

  on container-instance.tf line 36, in resource "azurerm_container_group" "jms-sftp":
  36:         share_name           = azurerm_storage_share.jms-sftp-share[volume.value.name]
    |----------------
    | azurerm_storage_share.jms-sftp-share is object with 3 attributes

Inappropriate value for attribute "share_name": string required.


Error: Incorrect attribute value type

  on container-instance.tf line 36, in resource "azurerm_container_group" "jms-sftp":
  36:         share_name           = azurerm_storage_share.jms-sftp-share[volume.value.name]
    |----------------
    | azurerm_storage_share.jms-sftp-share is object with 3 attributes

Inappropriate value for attribute "share_name": string required.


Error: Incorrect attribute value type

  on container-instance.tf line 36, in resource "azurerm_container_group" "jms-sftp":
  36:         share_name           = azurerm_storage_share.jms-sftp-share[volume.value.name]
    |----------------
    | azurerm_storage_share.jms-sftp-share is object with 3 attributes

Inappropriate value for attribute "share_name": string required.

我相信我明白它告诉我的,但我不知道如何设置

share_name           = azurerm_storage_share.jms-sftp-share[volume.value.name]

正确引用关联的共享。

如果我将 share_name 更改为

share_name           = azurerm_storage_share.jms-sftp-share[volume.value.name.name]

我得到的输出表明我之前的符号是正确的,但我不确定去哪里。

Error: Unsupported attribute

  on container-instance.tf line 36, in resource "azurerm_container_group" "jms-sftp":
  36:         share_name           = azurerm_storage_share.jms-sftp-share[volume.value.name.name]
    |----------------
    | volume.value.name is "one"

This value does not have any attributes.


Error: Unsupported attribute

  on container-instance.tf line 36, in resource "azurerm_container_group" "jms-sftp":
  36:         share_name           = azurerm_storage_share.jms-sftp-share[volume.value.name.name]
    |----------------
    | volume.value.name is "two"

This value does not have any attributes.


Error: Unsupported attribute

  on container-instance.tf line 36, in resource "azurerm_container_group" "jms-sftp":
  36:         share_name           = azurerm_storage_share.jms-sftp-share[volume.value.name.name]
    |----------------
    | volume.value.name is "three"

This value does not have any attributes.

有什么想法吗?

您需要在导出的资源属性对象中引用具体的值。错误消息指出:

azurerm_storage_share.jms-sftp-share is object with 3 attributes

表明您需要引用对象中的特定元素。这三个属性用 one twothree 字符串表示,您用作在问题中迭代的键。然后您访问特定元素,例如:

share_name = azurerm_storage_share.jms-sftp-share["one"].name

从导出的资源属性访问 azurerm_storage_share.jms-sftp-users-share 对象的 one 元素。