如何使用Terraform输出作为Azure ARM模板的输入

问题描述 投票:0回答:1

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

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

提前感谢。

azure terraform azure-resource-manager terraform-provider-azure
1个回答
0
投票

如何将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"
}

您可以在资源storageAccountType和ARM模板的parameters属性中看到参数azurerm_template_deployment。另外,您想使用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"
    }
© www.soinside.com 2019 - 2024. All rights reserved.