terraform,

'Invalid character' error in Terraform

Jun 01, 2022 · 2 mins read · Post a comment

Yet another Terraform error-related post. For any other Terraform-related issues, browse by category here.

Prerequisites

  • Terraform

The error

╷
│ Error: Invalid character
│
│   on providers.tf line 1:
│    1: ��provider "azurerm" {
│
│ This character is not used within the language.
╵

Solution

There are three things that comes to my mind you could try.

UTF-8 encoding

Make sure that the encoding is set to UTF-8. Just google how to set the encoding on your IDE tool of choice. This works 100% though if you receive the following errors as well:

╷
│ Error: Invalid character encoding
│
│   on providers.tf line 1:
│    1: ��provider "azurerm" {
│
│ All input files must be UTF-8 encoded. Ensure that UTF-8 encoding is selected in your editor.
╵

╷
│ Error: Argument or block definition required
│
│   on providers.tf line 1:
│    1: ��provider "azurerm" {
│
│ An argument or block definition is required here.
╵

Related posts:

ASCII

Next thing you might want to do is check for non-ASCII characters. ASCII simply put is a subset of the UTF-8 encoding. There are a dozen of ASCII validation tools, utilities and scripts available online tho but, if you need a one-liner:

plan.tf

What also could happen is, running terraform plan -out=plan.tf. Since the output is generated as a binary file, instead of saving the plan to a file ending with tf extension, e.g. plan.tf save it as tfplan, tfplan.out or even plan.tfplan. For instance, the command would be: terraform plan -out=tfplan.

Related post: terraform plan the right way.

Note(s): If terraform plan -out=tfplan throws Error: Too many command line arguments, try replacing the equal sign (=) with a space e.g. terraform plan -out tfplan.

Conclusion

If you have any questions, thoughts or opinions, feel free to leave a comment below. On a side note, follow our official channel on Telegram.