如何使用 Terraform 部署此 ARM 模板?请指教

Posted

技术标签:

【中文标题】如何使用 Terraform 部署此 ARM 模板?请指教【英文标题】:How do I deploy this ARM template with Terraform? Please Advise 【发布时间】:2022-01-16 09:26:17 【问题描述】:

当我通过 Terraform 和 Azure Devops 部署此模板时,我收到一个 Invalid template 错误,而模板在门户上正常部署。这是错误:

'microsoft.insights/workbooks' 类型的模板资源'' “1”行和“1512”列无效。名称属性不能 为空或为空。请参阅https://aka.ms/arm-template/#resources 使用详情。'。” AdditionalInfo=["info":"lineNumber":1,"linePosition":1512,"path":"properties.template.resources[0]","type":"TemplateViolation"]

我应该进行哪些修改才能通过 Terraform 进行部署?


    "contentVersion": "1.0.0.0",
    "parameters": 
      "workbookDisplayName": 
        "type": "string",
        "defaultValue": "Azure Firewall Workbook",
        "metadata": 
          "description": "The friendly name for the workbook that is used in the Gallery or Saved List.  This name must be unique within a resource group."
        
      ,
      "workbookType": 
        "type": "string",
        "allowedValues": [
            "workbook",
            "sentinel"
            
          ],
          "defaultValue": "workbook",
        "metadata": 
          "description": "The gallery that the workbook will been shown under. Supported values include workbook, tsg, etc. Usually, this is 'workbook'"
        
      ,
      "DiagnosticsWorkspaceName": 
        "type": "string",
        "defaultValue": "WorkspaceName",
        "metadata": 
          "description": "Provide the workspace name for your Network Diagnostic logs"
        
      ,
      "DiagnosticsWorkspaceSubscription": 
        "type": "string",
        "defaultValue": "WorkspaceSubscriptionID",
        "metadata": 
          "description": "Provide the workspace subscription GUID for your Network Diagnostic logs"
        
      ,
      "DiagnosticsWorkspaceResourceGroup": 
        "type": "string",
        "defaultValue": "ResourceGroupName",
        "metadata": 
          "description": "Provide the workspace resourcegroupname for your Network Diagnostic logs"
        
      ,
      "workbookId": 
        "type": "string",
        "defaultValue": "[newGuid()]",
        "metadata": 
          "description": "The unique guid for this workbook instance"
        
      
    ,
    "variables": 
            "workbookSourceId": "[concat('/subscriptions/',parameters('DiagnosticsWorkspaceSubscription'),'/resourcegroups/', parameters('DiagnosticsWorkspaceResourceGroup'), '/providers/Microsoft.OperationalInsights/workspaces/',parameters('DiagnosticsWorkspaceName'))]"
  ,
    "resources": [
      
        "name": "[parameters('workbookId')]",
        "type": "microsoft.insights/workbooks",
        "location": "[resourceGroup().location]",
        "apiVersion": "2018-06-17-preview",
        "dependsOn": [],
        "kind": "shared",
        "properties": 
          "displayName": "[parameters('workbookDisplayName')]"",
          "version": "1.0",
          "sourceId": "[variables('workbookSourceId')]",
          "category": "[parameters('workbookType')]"
        
      
    ],
    "outputs": 
      "workbookId": 
        "type": "string",
        "value": "[resourceId( 'microsoft.insights/workbooks', parameters('workbookId'))]"
      
    ,
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#"
  

【问题讨论】:

请注意,arm 标签适用于 ARM(高级 RISC 机器)CPU 架构,不适用于 Azure 资源管理器。请为此使用azure-resource-manager。在使用之前检查每个标签的描述是个好主意。 【参考方案1】:

我不了解 ARM 模板,但我在 Terraform AWS 和 Terraform Azure 提供商方面拥有非常好的经验。

首先,您最好看看 here. 的 Terraform 资源页面,这将有助于了解资源需求和结果。

我可能错了,因为您的 Terraform 脚本在问题部分不可见。不过,我猜您可能在 Terraform 方面遇到了问题。据我了解,您从 ARM 模板中收到错误消息。它抱怨缺少强制性的名称参数。您可能会忘记将参数名称从 Terraform 传递到 ARM 模板。我可能错了,这只是一个建议,正确的方法是查看 Terraform azurerm_template_deployment 资源。

// ARM Template part

      "$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"
          
        
      ,
// Terraform resource provisioning
      parameters = 
        "storageAccountType" = "Standard_GRS"
      

【讨论】:

以上是关于如何使用 Terraform 部署此 ARM 模板?请指教的主要内容,如果未能解决你的问题,请参考以下文章

将 ARM 模板转换为 Terraform [关闭]

使用 terraform 输出恢复 Azure ARm 模板的输出值

Terraform适用于现有的ARM资源。需要国家吗?

如何使用 Terraform 为 Azure 资源创建警报

Terraform - 将类型对象作为参数传递给 Azure 模板部署

如何使用 arm 模板更新 azure appsettings