【问题标题】:How to use Terraform output as input for Azure ARM template如何使用 Terraform 输出作为 Azure ARM 模板的输入
【发布时间】:2020-09-14 22:11:59
【问题描述】:

使用 terraform 和 Azure ARM 模板,想要使用 Terraform 输出作为 Azure ARM 模板的输入。需要帮助来决定执行此操作的最佳方式。

  1. 如何将 terraform 输出存储在 KeyVault/Storage/Tables 中
  2. 将输出用于 ARM 模板输入

提前致谢。

【问题讨论】:

    标签: azure terraform azure-resource-manager terraform-provider-azure


    【解决方案1】:

    如何将 terraform 输出存储在 KeyVault/Storage/Tables 中

    Terraform 的输出由您自己定义。所以你可以决定输出什么。并将它们存储在 KeyVault/Storage/Tables 中,只需创建它们的资源。例如,您想将 VM 资源 Id 存储为机密,则可以这样做:

    resource "azurerm_virtual_machine" "main" {
      name                  = "${var.prefix}-vm"
      location              = azurerm_resource_group.main.location
      resource_group_name   = azurerm_resource_group.main.name
      ...
    }
    
    ...
    
    resource "azurerm_key_vault_secret" "example" {
      name         = "secret-sauce"
      value        = azurerm_virtual_machine.main.id
      key_vault_id = azurerm_key_vault.example.id
    
      tags = {
        environment = "Production"
      }
    }
    

    将输出用于 ARM 模板输入

    对于 Terraform 中的 ARM 模板,您可以查看example。你可以在其中有一个属性parameters。 ARM模板中的所有参数,您可以在参数中添加输入进行设置。例如:

    resource "azurerm_template_deployment" "example" {
      name                = "acctesttemplate-01"
      resource_group_name = azurerm_resource_group.example.name
    
      template_body = <<DEPLOY
    {
      "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "storageAccountType": {
          "type": "string",
          "defaultValue": "Standard_LRS",
          "allowedValues": [
            "Standard_LRS",
            "Standard_GRS",
            "Standard_ZRS"
          ],
          "metadata": {
            "description": "Storage Account type"
          }
        }
      },
      ...
    }
    DEPLOY
    
    
      # these key-value pairs are passed into the ARM Template's `parameters` block
      parameters = {
        "storageAccountType" = "Standard_GRS"
      }
    
      deployment_mode = "Incremental"
    }
    

    您可以在资源azurerm_template_deployment 和ARM 模板的参数属性中看到参数storageAccountType。此外,您要使用 Terraform 的输出为 ARM 模板中的参数设置 VM 资源 Id 的示例,您可以这样做将 Terraform 输出传递到 ARM 模板:

    resource "azurerm_template_deployment" "example" {
          name                = "acctesttemplate-01"
          resource_group_name = azurerm_resource_group.example.name
    
          template_body = <<DEPLOY
        {
          "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "parameters": {
            "vm_resource_id": {
              "type": "string"
            }
          },
          ...
        }
        DEPLOY
    
    
          # these key-value pairs are passed into the ARM Template's `parameters` block
          parameters = {
            "vm_resource_id" = azurerm_virtual_machine.main.id
          }
    
          deployment_mode = "Incremental"
        }
    

    【讨论】:

    • 是的,但我想将terraform output 存储到机密,然后在ARM 模板部署中使用机密。可能吗。因为我有多个输出值。
    • @user47 据我所知,似乎不可能一次实现。这次您可以使用执行 Terraform 之前存在的输出文件。而且我认为输出文件只是存储您可以在 Terraform 代码中获取它们的消息。为什么要通过输出文件来做呢?
    • 我需要这个 terraform 输出文件以键值的形式存储在某处,并使用 ARM 模板将其用于另一个部署
    • 它可以是任何 azure 资源或使用 azure-devops
    • @user47 如果您的意思是来自terraform output 的输出。您需要了解输出是什么。看看here。那你为什么要在你想要执行的 Terraform 中引用它们呢?
    猜你喜欢
    • 2019-12-12
    • 2017-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    相关资源
    最近更新 更多