使用 Terraform 部署 Azure 资源时如何解决插件错误?

Mic*_*695 5 azure terraform-provider-azure

我收到以下错误

\n
\xe2\x94\x82 Error: Plugin error\n\xe2\x94\x82\n\xe2\x94\x82   with provider["registry.terraform.io/hashicorp/azurerm"],\n\xe2\x94\x82   on nsg.tf line 13, in provider "azurerm":\n\xe2\x94\x82   13: provider "azurerm" {\n\xe2\x94\x82\n\xe2\x94\x82 The plugin returned an unexpected error from plugin.(*GRPCProvider).ConfigureProvider: rpc error: code = Internal desc = grpc: error while marshaling: string field\n\xe2\x94\x82 contains invalid UTF-8\n
Run Code Online (Sandbox Code Playgroud)\n

当我尝试对以下代码执行 Terraform 计划时

\n
# Configure the Microsoft Azure Provider\nterraform {\n  required_providers {\n    azurerm = {\n      source  = "hashicorp/azurerm"\n      version = "=2.74.0"\n    }\n  }\n}\n\n# Configure the Microsoft Azure Provider\nprovider "azurerm" {\n  features {}\n}\nresource "azurerm_network_security_group" "nsg" {\n    name                                = "TestNSG"\n    location                            = "East US"\n    resource_group_name                 = "TFResourcegroup"\n}\n\nresource "azurerm_network_security_rule" "example1" {\n    name                                = "Web80"\n    priority                            = 1001\n    direction                           = "Inbound"\n    access                              = "Allow"\n    protocol                            = "Tcp"\n    source_port_range                   = "*"\n    destination_port_range              = "80"\n    source_address_prefix               = "*"\n    destination_address_prefix          = "*"\n    resource_group_name                 = "TFResourcegroup"\n    network_security_group_name         = azurerm_network_security_group.nsg.name          \n}\n\nresource "azurerm_network_security_rule" "example2" {\n    name                                = "Web8080"\n    priority                            = 1000\n    direction                           = "Inbound"\n    access                              = "Deny"\n    protocol                            = "Tcp"\n    source_port_range                   = "*"\n    destination_port_range              = "8080"\n    source_address_prefix               = "*"\n    destination_address_prefix          = "*"\n    resource_group_name                 = "TFResourcegroup"\n    network_security_group_name         = azurerm_network_security_group.nsg.name \n}\n\nresource "azurerm_network_security_rule" "example3" {\n    name                                = "WebOUT"\n    priority                            = 1000\n    direction                           = "Outbound"\n    access                              = "Deny"\n    protocol                            = "Tcp"\n    source_port_range                   = "*"\n    destination_port_range              = "80"\n    source_address_prefix               = "*"\n    destination_address_prefix          = "*"\n    resource_group_name                 = "TFResourcegroup"\n    network_security_group_name         = azurerm_network_security_group.nsg.name \n}\n
Run Code Online (Sandbox Code Playgroud)\n

这些是我的 terraform、provider 和 Azure 版本

\n
PS C:\\russ\\nsg> terraform --version\nTerraform v1.0.4\non windows_amd64\n+ provider registry.terraform.io/hashicorp/azurerm v2.74.0\nPS C:\\russ\\nsg> az --version\nazure-cli                         2.27.2\n
Run Code Online (Sandbox Code Playgroud)\n

我已经尝试过......

\n

在互联网上进行研究,它说这只发生在 Azure 门户中的 Azure CLI 中。我已经在我自己的计算机上的 Visual Studio Code 和 Powershell 上尝试过了。但我得到了相同的结果。我什至尝试过将https://registry.terraform.io/providers/hashicorp/azurerm/latest 作为源而不是通常的“hashicorp/azurerm”,但这也会引发错误

\n

我真的陷入困境...任何指导将不胜感激地收到..我一整天都愉快地使用相同的方法部署Azure资源...但出于某种原因..它只是不喜欢这个...

\n

小智 8

这可能是由于您的 Az CLI 会话出现问题,您可能需要使用 再次登录az login

\n

您可以通过运行需要与资源(不仅仅是az account show)交互的 Az CLI 命令来确认这是否是问题。这就是我识别问题的方法:

\n
\n

$ az vm list -g 我的资源组

\n

AADSTS700082:由于不活动,刷新令牌已过期。\xc2\xa0令牌于 2021-06-27T00:13:23.1948087Z 颁发,并且在 90.00:00:00 内处于非活动状态。

\n
\n