'string field contains invalid UTF-8' error

I have the following trivial example which keeps giving me an error about an invalid UTF-8 character. I’ve been able to reproduce it with a completely new file, I’ve tried overtyping the offending area, looking over it with a hex editor but I’ve got nothing. It passes validation but errors in plan, enabling debug doesn’t give any more information.

provider "azurerm" {
  features {}
# Adding this gives 'string field contains invalid UTF-8' error
resource "azurerm_resource_group" "rg" {
  name     = "rg"
  location = "uksouth"

Hi @TerraErr0r,

Could you share the full error message exactly as Terraform printed it? I’d like to see what exactly the error message was referring to here, since copy-pasting into the forum has probably changed the details of exactly how your file is encoded.

If you see something underlined in the source code snippet Terraform prints out then it would be helpful to mention which part is underlined too, since that won’t come across in copy-paste but it could help narrow down exactly what part of your input Terraform’s parser is objecting to.

I have exactly the same error. My setup: Windows 10 with terraform 1.2.4 and azurerm 3.13.0 (but a test with azurem 3.0.2 showed the same behaviour). The exact error message is:

Error: Plugin error

  with provider["registry.terraform.io/hashicorp/azurerm"],
  on test.tf line 10, in provider "azurerm":
  10: provider "azurerm" {

The plugin returned an unexpected error from plugin.(*GRPCProvider).ConfigureProvider: rpc error: code = Internal desc = grpc: error while marshaling: string field contains invalid UTF-8

Creating a new file with VSCode, notepad or notepad++ (and making sure the correct UTF-8 encoding is used) didn’t help.

Hi @xx364! Thanks for sharing the full error message.

This error message seems to be coming from the Azure provider rather than from Terraform Core itself, though it’s not an error I recognize seeing before so I’m not 100% sure what would cause it.

The message mentioned “marshalling” which is another way of saying “serializing” or “encoding” a value to send it over a wire protocol, so based only on the error text it seems like there are two possible sources for this error:

  • Terraform Core’s message to the provider includes a string that isn’t valid UTF-8, and so the provider protocol implementation inside Terraform Core is the one rejecting it, because there is no way to turn the request into a valid provider protocol message.
  • The Azure provider’s response, from provider to Terraform Core, includes an invalid string and so the plugin server embedded in the plugin (part of the plugin SDK) is rejecting it, but it’s sending the error about it back to Terraform Core as a protocol error. (“grpc” is the underlying RPC protocol Terraform uses to communicate with plugins).

This “rpc error” presentation of a message can in principle be returned by any part of the RPC process, so I can’t narrow down here which side of the protocol is incorrect. If it’s the first option above then that probably means there is something you could change in your configuration to avoid it, but since Terraform Core isn’t handling the situation well it’s hard to diagnose what exactly that root be. If it’s the second situation then it may need to be fixed in the provider code or provider SDK code.

To continue with this, I think the best option would be to open a bug report in the Azure provider repository, where I think the provider maintainers will be in a better position to debug this and determine what is causing it. If it turns out to be a problem on the client side if the protocol (in Terraform Core) the provider team can then file an issue in the main Terraform repository to report what they learned.

I’m sorry I don’t have a quick and easy answer for this case! :confounded:

Thank You, bug report created

Simple terraform script always causes grpc: error while marshaling: string field contains invalid UTF-8 · Issue #17563 · hashicorp/terraform-provider-azurerm (github.com)