【问题标题】:Accessing specific storage account id created using terraform module访问使用 terraform 模块创建的特定存储帐户 ID
【发布时间】:2021-12-05 03:31:52
【问题描述】:

我正在使用 terraform 模块创建基础架构。一些常见和重复的基础设施是使用模块创建的 和其他资源是在模块之外独立创建的。我的代码结构描述如下。

-terraform\module\storage.tf

-terraform\main.tf

-terraform\mlws.tf

这是我的 /module/storage.tf 代码,我在其中创建这样的存储帐户

  resource "azurerm_storage_account" "storage" {
  name                      = var.storage_account_name
  resource_group_name       = var.rg_name
  location                  = var.location
  account_tier              = "Standard"
  account_replication_type  = "GRS"
  min_tls_version           = "TLS1_2"
}

module "m1" {
  source                     = "./modules"

  storage_account_name       = "m1storage"
  rg_name                    = "rg1"
  location                   = "USCentral"

}

module "m2" {
  source                     = "./modules"

  storage_account_name       = "m2storage"
  rg_name                    = "rg2"
  location                   = "USCentral"
}

module "m3" {
  source                     = "./modules"

  storage_account_name       = "m3storage"
  rg_name                    = "rg3"
  location                   = "USCentral"
}

resource "azurerm_machine_learning_workspace" "mlws" {
  name                    = "mlws"
  location                = ""USCentral"
  resource_group_name     = "mlws-rg1"
  application_insights_id = azurerm_application_insights.mlops_appins.id
  key_vault_id            = data.azurerm_key_vault.kv.id
  storage_account_id      = **<Mandatory to be filled>**
  container_registry_id   = azurerm_container_registry.acr.id

  identity {
    type = "SystemAssigned"
  }

  depends_on = [
    module.m2
  ]
}

存储帐户的代码在\terraform\module\storage.tf下,调用模块的代码在\terraform\main.tf下,机器学习工作区的代码在\terraform\mlws.tf下。

由于我的 mlws.tf 代码在模块之外,但它需要与上述代码中在模块 m2 下创建的存储帐户 ID 相关联。 我正在努力获取“m2storage”存储帐户的 ID。您能否提供解决方案,说明如何访问通过模块创建的特定存储帐户的 ID 并将其附加到模块外部的代码中。

【问题讨论】:

    标签: azure terraform


    【解决方案1】:

    这是它通常的工作方式。你运行模块m2,它应该给出类似这样的输出(应该包括storage_account_id):

    output "storage_account_id" {
      description = "M2 storage account id."
      value       = m2.storage_account.storage_account_id
    }
    

    现在您有了输出并且想要使用它,您将其称为:

    resource "azurerm_machine_learning_workspace" "mlws" {
      name                    = "mlws"
      location                = ""USCentral"
      resource_group_name     = "mlws-rg1"
      application_insights_id = azurerm_application_insights.mlops_appins.id
      key_vault_id            = data.azurerm_key_vault.kv.id
      storage_account_id      = module.m2.storage_account_id
      container_registry_id   = azurerm_container_registry.acr.id
    
      identity {
        type = "SystemAssigned"
      }
    
      depends_on = [
        module.m2
      ]
    }
    

    如果您需要更多帮助,请告诉我。

    【讨论】:

    • 感谢您的回复,但这不起作用。我收到这样的错误“在module.m1中声明了没有名为“m2”的模块调用”。对于所有 3 个模块调用,相同的错误重复三次。我之前确实尝试过,但我不确定如何检索特定的存储帐户 ID。
    猜你喜欢
    • 2018-08-02
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 2021-04-06
    • 2019-07-28
    • 2021-10-29
    相关资源
    最近更新 更多