Azure监控服务部署Terraform配置错误及脚本编写问题咨询
问题排查与修正方案
配置存在如下问题。Terraform配置必须在初始化前保证合法,这样Terraform才能判定需要安装哪些模块和提供程序。
错误根因
1. 变量类型不匹配
- variables.tf中定义的
location默认值为数组类型["East US"],但azurerm_resource_group的location参数要求传入字符串类型,类型不兼容。
2. 资源引用语法错误
- 存储账户
location参数引用错误,漏写了资源别名.example,正确写法为azurerm_resource_group.example.location。 - 存储容器、存储Blob配置中引用存储账户时拼写错误,错写为
eample,正确拼写为example。 - 存储容器不需要
resource_group_name、account_key参数,需替换为合法参数storage_account_name。 - 存储Blob参数完全不符合规范,不需要
resource_group_name、account_key参数,需补充storage_account_name、storage_container_name、type参数,source参数需指定本地文件路径,不能直接引用存储容器名称。
3. 非法配置文件内容
- azvm.tf中写入的是Azure CLI命令,不是Terraform要求的HCL配置语法,会被Terraform识别为非法配置直接报错。
修正后配置文件
variables.tf
variable "resourcegroup" { default = "user-pbtwiiiuofyu" } variable "location" { default = "East US" }
azmonitor.tf
provider "azurerm" { features {} } # 如果目标资源组已预先创建,可删除本段资源配置 resource "azurerm_resource_group" "example" { name = var.resourcegroup location = var.location } resource "azurerm_storage_account" "example" { name = "sa123321123" resource_group_name = azurerm_resource_group.example.name location = azurerm_resource_group.example.location sku = "Standard_LRS" } resource "azurerm_storage_container" "example" { name = "sc123321123" storage_account_name = azurerm_storage_account.example.name public_access = "blob" } resource "azurerm_storage_blob" "example" { name = "sb123321123" storage_account_name = azurerm_storage_account.example.name storage_container_name = azurerm_storage_container.example.name type = "Block" # 如需上传本地日志文件可替换为source = "本地日志文件路径" content = "Log storage initialization completed" }
部署操作步骤
- 删除非法的azvm.tf文件,或将其中的CLI命令替换为合法的
azurerm_linux_virtual_machine资源HCL配置。 - 执行
terraform init初始化AzureRM提供程序。 - 执行
terraform plan验证配置合法性,确认无报错后执行terraform apply完成资源部署。
内容的提问来源于stack exchange,提问作者Ashish Jindal
相关产品推荐
相关产品推荐

