1
votes

I'm running Terraform using VScode editor which uses PowerShell as the default shell and getting the same error when I try to validate it or to run terraform init/plan/apply through VScode, external PowerShell or CMD.

The code was running without any issues until I added Virtual Machine creation code. I have clubbed the variables.tf, terraform.tfvars and the main Terraform code below.

terraform.tfvars

web_server_location       = "West US 2"
resource_prefix           = "web-server"
web_server_address_space  = "1.0.0.0/22"
web_server_address_prefix = "1.0.1.0/24"
Environment               = "Test"

variables.tf

variable "web_server_location" {
  type = string
}

variable "resource_prefix" {
  type = string
}

variable "web_server_address_space" {
  type = string
}

#variable for network range

variable "web_server_address_prefix" {
  type = string
}

#variable for Environment
variable "Environment" {
  type = string
}

terraform_example.tf

# Configure the Azure Provider
provider "azurerm" {
  # whilst the `version` attribute is optional, we recommend pinning to a given version of the Provider
  version = "=2.0.0"
  features {}
}

# Create a resource group
resource "azurerm_resource_group" "example_rg" {
  name     = "${var.resource_prefix}-RG"
  location = var.web_server_location
}

# Create a virtual network within the resource group
resource "azurerm_virtual_network" "example_vnet" {
  name                = "${var.resource_prefix}-vnet"
  resource_group_name = azurerm_resource_group.example_rg.name
  location            = var.web_server_location
  address_space       = [var.web_server_address_space]
}

# Create a subnet within the virtual network
resource "azurerm_subnet" "example_subnet" {
  name                  = "${var.resource_prefix}-subnet"
  resource_group_name   = azurerm_resource_group.example_rg.name
  virtual_network_name  = azurerm_virtual_network.example_vnet.name
  address_prefix        = var.web_server_address_prefix
}

# Create a Network Interface
resource "azurerm_network_interface" "example_nic" {
  name                = "${var.resource_prefix}-NIC"
  location            = azurerm_resource_group.example_rg.location
  resource_group_name = azurerm_resource_group.example_rg.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.example_subnet.id
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id          = azurerm_public_ip.example_public_ip.id
  }  
}

# Create a Public IP 
resource "azurerm_public_ip" "example_public_ip" {
  name = "${var.resource_prefix}-PublicIP"
  location = azurerm_resource_group.example_rg.location
  resource_group_name = azurerm_resource_group.example_rg.name
  allocation_method = var.Environment == "Test" ? "Static" : "Dynamic"

  tags = {
    environment = "Test"
  }
}

# Creating resource NSG
resource "azurerm_network_security_group" "example_nsg" {
  name = "${var.resource_prefix}-NSG"
  location = azurerm_resource_group.example_rg.location
  resource_group_name = azurerm_resource_group.example_rg.name

  # Security rule can also be defined with resource azurerm_network_security_rule, here just defining it inline. 
  security_rule {
    name       = "RDPInbound"
    priority   = 100
    direction  = "Inbound"
    access     = "Allow"
    protocol   = "Tcp"
    source_port_range = "*"
    destination_port_range = "3389"
    source_address_prefix  = "*"
    destination_address_prefix = "*"
  }
  tags = {
    environment = "Test"
  }
}

# NIC and NSG association 
resource "azurerm_network_interface_security_group_association" "example_nsg_association" {
   network_interface_id      = azurerm_network_interface.example_nic.id
   network_security_group_id = azurerm_network_security_group.example_nsg.id

}

# Creating Windows Virtual Machine
resource "azurerm_virtual_machine" "example_windows_vm" {
  name  = "${var.resource_prefix}-VM"
  location = azurerm_resource_group.example_rg.location
  resource_group_name = azurerm_resource_group.example_rg.name
  network_interface_ids = [azurerm_network_interface.example_nic.id]
  vm_size = "Standard_B1s"
  delete_os_disk_on_termination = true

  storage_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServerSemiAnnual"
    sku       = "Datacenter-Core-1709-smalldisk"
    version   = "latest"  
  }

  storage_os_disk  {
    name                 = "myosdisk1"
    caching              = "ReadWrite"
    create_option        = "FromImage"
    storage_account_type = "Standard_LRS"
  }

  os_profile {
    computer_name  = "hostname"
    admin_username = "adminuser"
    admin_password = "Password1234!"
  }

  os_profile_windows_config {
    disable_password_authentication = false
  }

  tags = {
    environment = "Test"
  }
}

Error:

PS C:\Users\e5605266\Documents\MyFiles\Devops\Terraform> terraform init

There are some problems with the configuration, described below.

The Terraform configuration must be valid before initialization so that
Terraform can determine which modules and providers need to be installed.

Error: Invalid character

  on terraform_example.tf line 89, in resource "azurerm_virtual_machine" "example_windows_vm":
  89:   location                      = azurerm_resource_group.example_rg.location

This character is not used within the language.


Error: Invalid expression

  on terraform_example.tf line 89, in resource "azurerm_virtual_machine" "example_windows_vm":
  89:   location                      = azurerm_resource_group.example_rg.location

Expected the start of an expression, but found an invalid expression token.


Error: Argument or block definition required

  on terraform_example.tf line 90, in resource "azurerm_virtual_machine" "example_windows_vm":
  90:   resource_group_name           = azurerm_resource_group.example_rg.name

An argument or block definition is required here. To set an argument, use the
equals sign "=" to introduce the argument value.


Error: Invalid character

  on terraform_example.tf line 90, in resource "azurerm_virtual_machine" "example_windows_vm":
  90:   resource_group_name           = azurerm_resource_group.example_rg.name

This character is not used within the language.
*
2
@dmkvl - please see where i have created the RGShantanu Mukherjee
If you've copy pasted the snippet somewhere from the internet then there are chances that some special characters can be present. If this is not the case then please ignore my comment.Oli
How about the issue? Does the answer below resolved your question? If not, please let us know if you would like further assistance.Joy
@Joy : thanks.. but I have resolved the issue.Shantanu Mukherjee

2 Answers

4
votes

I've encountered this problem myself in several different contexts, and it does have a common solution which is no fun at all: manually typing the code back in...

This resource block seems to be where it runs into problems:

resource "azurerm_virtual_machine" "example_windows_vm" {
  name  = "${var.resource_prefix}-VM"
  location = azurerm_resource_group.example_rg.location
  resource_group_name = azurerm_resource_group.example_rg.name
  network_interface_ids = [azurerm_network_interface.example_nic.id]
  vm_size = "Standard_B1s"
  delete_os_disk_on_termination = true

  storage_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServerSemiAnnual"
    sku       = "Datacenter-Core-1709-smalldisk"
    version   = "latest"  
  }

  storage_os_disk  {
    name                 = "myosdisk1"
    caching              = "ReadWrite"
    create_option        = "FromImage"
    storage_account_type = "Standard_LRS"
  }

  os_profile {
    computer_name  = "hostname"
    admin_username = "adminuser"
    admin_password = "Password1234!"
  }

  os_profile_windows_config {
    disable_password_authentication = false
  }

  tags = {
    environment = "Test"
  }
}

Try copying that back into your editor as is. I cannot see any problematic characters in it, and ironically StackOverflow may have done you a solid and filtered them out. Literally copy/pasting it over the existing block may remedy the situation.

I have seen Terraform examples online with stylish double quotes (which aren't ASCII double quotes and won't work) many times. That may be what you are seeing.

Beyond that, you'd need to push your code to GitHub or similar so I can see the raw bytes for myself.

2
votes

In the off-chance this helps someone who runs into this error and comes across it on Google, I just thought I would post my situation and how I fixed it.

I have an old demo Terraform infrastructure that I revisited after months and, long story short, I issued this command two days ago and forgot about it:

terraform plan -out=plan.tf

This creates a zip archive of the plan. Upon coming back two days later and running a terraform init, my terminal scrolled garbage and "This character is not used within the language." for about 7 seconds. Due to the .tf extension, terraform was looking at the zip data and promptly pooping its pants.

Through moving individual tf files to a temp directory and checking their validity with terraform init, I found the culprit, deleted it, and functionality was restored.

Be careful when exporting your plan files, folks!