You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Terraform多行引号字符串错误原因解析求助

Terraform报错「Quoted strings may not be split over multiple lines」解析与修复

一、错误含义

这个错误是Terraform的语法校验提示:普通双引号包裹的字符串不允许直接跨行拆分。如果需要定义包含换行的字符串,要么用\n转义字符表示换行,要么使用Heredoc多行模板语法(如<<EOT)。

二、问题原因

你复制的代码里存在两个关键问题:

  1. 引号配对语法错误:在定义azurerm_network_security_group资源时,资源类型的闭合引号前多了一个空格,导致Terraform无法正确识别语法边界,将后续代码误判为未闭合的跨行字符串。错误行如下:
    resource "azurerm_network_security_group "mtc-sg" {
    
  2. 资源引用错误:azurerm_network_security_rule资源中,network_security_group_name引用的是azurerm_network_security_group.mtc-rm.name,但你的安全组资源名称是mtc-sg,后续会出现资源找不到的问题。

三、修复方案

  1. 修正引号语法:删除安全组资源定义行的多余空格,确保引号正确配对:
    resource "azurerm_network_security_group" "mtc-sg" {
    }
    
  2. 修正资源引用:将安全组规则中的引用改为正确的资源名称:
    network_security_group_name = azurerm_network_security_group.mtc-sg.name
    
  3. 若确实需要定义多行字符串,可使用Heredoc语法,示例:
    variable "multiline_desc" {
      type = string
      default = <<EOT
    这是第一行描述内容
    这是第二行描述内容
    EOT
    }
    

四、完整修正后的代码

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=3.0.0"
    }
  }
}

# Configure the Microsoft Azure Provider
provider "azurerm" {
  features {}
}

# Create a resource group
resource "azurerm_resource_group" "mtc-rm" {
  name     = "mtc-rm"
  location = "East Us"
}

resource "azurerm_virtual_network" "mtc-network" {
  name                = "mtc-network"
  resource_group_name = azurerm_resource_group.mtc-rm.name
  location            = azurerm_resource_group.mtc-rm.location
  address_space       = ["10.123.0.0/16"]

  tags = {
    enviroment = "dev"
  }
}

resource "azurerm_virtual_network" "mtc-vn" {
  name                = "mtc-vn"
  resource_group_name = azurerm_resource_group.mtc-rm.name
  location            = azurerm_resource_group.mtc-rm.location
  address_space       = ["10.123.0.0/16"]

  tags = {
    enviroment = "dev"
  }
}

resource "azurerm_subnet" "mtc-subnet" {
  name                 = "mtc-subnet"
  resource_group_name  = azurerm_resource_group.mtc-rm.name
  virtual_network_name = azurerm_virtual_network.mtc-vn.name
  address_prefixes     = ["10.123.1.0/24"]
}

resource "azurerm_network_security_group" "mtc-sg" {
  name                = "mtc-sg" 
  location            = azurerm_resource_group.mtc-rm.location
  resource_group_name = azurerm_resource_group.mtc-rm.name

  tags = {
    environment = "Dev"
  }
}

resource "azurerm_network_security_rule" "mtc-dev-rule" {
  name                        = "mtc-dev-rule"
  priority                    = 100
  direction                   = "Outbound"
  access                      = "Allow"
  protocol                    = "Tcp"
  source_port_range           = "*"
  destination_port_range      = "*"
  source_address_prefix       = "*"
  destination_address_prefix  = "*"
  resource_group_name         = azurerm_resource_group.mtc-rm.name
  network_security_group_name = azurerm_network_security_group.mtc-sg.name
}

内容的提问来源于stack exchange,提问作者user2073048

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 19:07:39