首页 > 解决方案 > parameters_content 值无法反序列化

问题描述

我正在为 azurerm_resource_group_template_deployment 使用内联部署,并且我正在传递一个使用另一个 terraform 资源名称的参数。

代码:

resource "azurerm_app_service" "webapi" {
  name                = "${lower(var.deploymentEnvironment)}-webapi"
  location            = azurerm_resource_group.frontendResourceGroup.location
  resource_group_name = azurerm_resource_group.frontendResourceGroup.name
  app_service_plan_id = azurerm_app_service_plan.appSvcPlan.id
}

resource "azurerm_resource_group_template_deployment" "webapi_virtual_directory" {
  name                = "webapi_virtual_directory"
  resource_group_name = azurerm_resource_group.frontendResourceGroup.name
  deployment_mode     = "Incremental"
  template_content       = <<TEMPLATE
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
        "webAppName": {
            "type": "String"
        },
        "virtualApplications":{
        "type": "array",
        "defaultValue":[
            {
            "virtualPath": "/",
            "physicalPath": "site\\wwwroot",
            "preloadEnabled": false,
            "virtualDirectories": null
            }
        ]
        }
  },
  "variables": {},
  "resources": [
      {
          "type": "Microsoft.Web/sites/config",
          "name": "[concat(parameters('webAppName'), '/web')]",
          "apiVersion": "2020-06-01",
          "properties": {
              "virtualApplications": "[parameters('virtualApplications')]"
          },
          "dependsOn": []
      }
  ]
}
  TEMPLATE
  parameters_content  = jsonencode({webAppName = azurerm_app_service.webapi.name})
  depends_on          = [azurerm_app_service.webapi]
}

当我运行 terraform apply 命令时,出现错误:

错误:验证模板部署“webapi_virtual_directory”(资源组“frontendapps-rg”):请求验证:resources.DeploymentsClient#Validate:发送请求失败:StatusCode=400 - 原始错误:Code="InvalidRequestContent" Message="请求内容无效且无法反序列化:“将值“ci-webapi”转换为类型“Azure.ResourceManager.Deployments.Templates.Definitions.DeploymentParameterDefinition”时出错。路径“properties.parameters.webAppName”,第 1 行,位置 861。 "

在 main.tf 第 134 行,在资源“azurerm_resource_group_template_deployment”“webapi_virtual_directory”中:134:资源“azurerm_resource_group_template_deployment”“webapi_virtual_directory”{

标签: terraformterraform-provider-azure

解决方案


我没有使用该parameters_content属性,而是添加defaultValuetemplate_content这样的内容:

resource "azurerm_resource_group_template_deployment" "webapi_virtual_directory" {
  name                = "webapi_virtual_directory"
  resource_group_name = azurerm_resource_group.frontendResourceGroup.name
  deployment_mode     = "Incremental"
  template_content    = <<TEMPLATE
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "webAppName": {
        "type": "String",
        "defaultValue": "${azurerm_app_service.webapi.name}"
    },
    "virtualApplications":{
      "type": "array",
      "defaultValue":[
        {
        "virtualPath": "/",
        "physicalPath": "site\\wwwroot",
        "preloadEnabled": false,
        "virtualDirectories": null
        }
      ]
    }
  },
  "resources": [
    {
      "type": "Microsoft.Web/sites/config",
      "name": "[concat(parameters('webAppName'), '/web')]",
      "apiVersion": "2020-06-01",
      "properties": {
          "virtualApplications": "[parameters('virtualApplications')]"
      }
    }
  ]
}
TEMPLATE
  depends_on          = [azurerm_app_service.webapi]
}

现在我没有收到验证模板部署的错误。


推荐阅读