【问题标题】:Terraform Azure Container Instance Dynamic Volume - share_name loop over azurerm_storage_shareTerraform Azure 容器实例动态卷 - azurerm_storage_share 上的 share_name 循环
【发布时间】:2021-06-02 23:55:52
【问题描述】:

我有以下 terraform 代码创建 azure 存储文件共享。

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.

有什么想法吗?

【问题讨论】:

    标签: terraform terraform-provider-azure


    【解决方案1】:

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

    azurerm_storage_share.jms-sftp-share 是具有 3 个属性的对象

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

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

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

    【讨论】:

    • 我知道那是我所缺少的,但我终其一生都想不出如何引用它。你把我带到了我需要去的地方。这最终成为我的线,它似乎工作。 azurerm_storage_share.jms-sftp-share[volume.value.name].name
    猜你喜欢
    • 2021-09-25
    • 1970-01-01
    • 2021-01-01
    • 2021-05-26
    • 2021-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    相关资源
    最近更新 更多