Error: Invalid character; This character is not used within the language

Error: Invalid character

│ On main.tf line 25: This character is not used within the language.

I copied the code from : Build Infrastructure | Terraform - HashiCorp Learn

Hi @jeffgo888,

Getting good answers from an Internet forum, relies on giving potential responders the information they need to understand your problem.

As you’ve not included the code that Terraform is complaining about, I’m not able to help you.

The link you have provided includes various different pieces of code, including a video containing images of some code - I don’t know which one you mean, and exactly what is on line 25 could vary considerably. If you were retyping code from a video, it might not even be the same as what is pictured there.

Please provide the exact code, and remember to include it within the appropriate formatting markers (a ``` on a line by itself before and after) so the forum software doesn’t try to treat special characters as formatting. Check how your post looks after posting (or the preview), and edit it if the formatting looks like it would make it difficult for people to help you.

sorry here is the code and I used wordpad:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }

  required_version = ">= 1.2.0"
}

provider "aws" {
  region  = "us-west-2"
}

resource "aws_instance" "app_server" {
  ami           = "ami-830c94e3"
  instance_type = "t2.micro"

  tags = {
    Name = "ExampleAppServerInstance"
  }
}

I am sorry, but there are still two problems with what you’ve posted:

  • There are only 23 lines, so it can’t be the same code as referenced in the error message pointing to line 25
  • You didn’t do this:

so I can’t tell whether all the angled quotation marks in your post are that way because the forum software is changing them to make the text look “nice” or whether they are that way in your actual Terraform source.

not sure what else to do:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }

  required_version = ">= 1.2.0"
}

provider "aws" {
  region  = "us-west-2"
}

resource "aws_instance" "app_server" {
  ami           = "ami-830c94e3"
  instance_type = "t2.micro"

  tags = {
    Name = "ExampleAppServerInstance"
  }
}

@jeffgo888

When i copied your code and tried formatting with terraform fmt it gives me below error which suggest that it is specific to your text editor which is converting is " to ” that is straight quote to curly quote .

“Curly quotes” are not valid here. These can sometimes be
│ inadvertently introduced when sharing code via documents or
│ discussion forums. It might help to replace the character with a
│ “straight quote”.

This can be one of the reason and when i paste your code into my code editor it is only 23 lines and as per your error it is on line 25 so either there are blank spaces or other characters there or you havent provided complete code block.

so what editor to use
nvm I used ATOM and it works now thanks guys

great @jeffgo888

I would suggest trying out visual studio as it supports variety of different tools and applications also very user friendly and works almost same way on all different OS.

But Atom is great choice as well :slight_smile:

Hi @jeffgo888,

It looks like the discussion here already reached this conclusion but I just wanted to post it explicitly in case someone else finds this topic in future and is unsure what the conclusion was:

WordPad is a minimal word processor rather than a text editor, and so it’s primarily designed for writing words for humans to read rather than for writing source code for computers to parse directly.

In particular, it seems that WordPad has a feature where it will automatically convert ASCII quotes " into the Unicode separate open and closing quotes / for better typography, but that makes the resulting file invalid for Terraform.

Although not discussed here, I think another potential problem with WordPad is that if you save a WordPad document as plain text it can potentially generate a document in the wrong character encoding: either UTF-16 (the native encoding used by Microsoft Windows) or UTF-8 with an errononeous byte order mark that Terraform cannot always accept gracefully.

When writing source code it’s better to use an editor designed for that purpose. Windows comes with a plain text editor called Notepad which can potentially create suitable plain-text documents as long as you’re careful to save with the character encoding UTF-8 without BOM, but if you will be writing a lot of source code as part of your job it’s often worth installing a third-party programmers’ text editor. Atom is one such editor that you’ve already found; Visual Studio Code is another, and HashiCorp maintains an official extension for Visual Studio Code that includes some helpful features for writing Terraform code, such as automatic error indications and code completion.

2 Likes