2
votes

In its simplest form, main.tf is as below:

data "azurerm_resource_group" "tf-rg-external" {
  name = var.rg_name
}

# Reference existing Virtual Network
data "azurerm_virtual_network" "tf-vn" {
  name                = var.vnet_name
  resource_group_name = data.azurerm_resource_group.tf-rg-external.name
}

# Reference existing subnet
data "azurerm_subnet" "tf-sn" {
  name                 = var.subnet_name
  virtual_network_name = data.azurerm_virtual_network.tf-vn.name
  resource_group_name  = data.azurerm_resource_group.tf-rg-external.name
}

resource "azurerm_network_security_group" "tf-nsg" {
  name                = var.app_nsg
  location            = data.azurerm_virtual_network.tf-vn.location
  resource_group_name = data.azurerm_resource_group.tf-rg-external.name
}

resource "azurerm_network_security_rule" "tf-nsr-5986" {
  name                   = "Open Port 5986"
  priority               = 101
  direction              = "Inbound"
  access                 = "Allow"
  protocol               = "Tcp"
  source_port_range      = "*"
  destination_port_range = "5986"
  source_address_prefixes     = var.allowed_source_ips
  destination_address_prefix  = "VirtualNetwork"
  resource_group_name         = data.azurerm_resource_group.tf-rg-external.name
  network_security_group_name = azurerm_network_security_group.tf-nsg.name
}

resource "azurerm_network_security_rule" "tf-nsr-3389" {
  name                   = "Open Port 3389"
  priority               = 102
  direction              = "Inbound"
  access                 = "Allow"
  protocol               = "Tcp"
  source_port_range      = "*"
  destination_port_range = "3389"
  source_address_prefixes     = var.allowed_source_ips
  destination_address_prefix  = "VirtualNetwork"
  resource_group_name         = data.azurerm_resource_group.tf-rg-external.name
  network_security_group_name = azurerm_network_security_group.tf-nsg.name
}

# Assosciate NSG to subnet
resource "azurerm_subnet_network_security_group_association" "tf-snnsg" {
  subnet_id                 = data.azurerm_subnet.tf-sn.id
  network_security_group_id = azurerm_network_security_group.tf-nsg.id
}

# Network inteface for Interface
resource "azurerm_network_interface" "tf-ni" {
  count               = var.vm_count
  name                = "${var.base_hostname}${format("%02d", count.index + 1)}-nic01"
  location            = data.azurerm_virtual_network.tf-vn.location
  resource_group_name = data.azurerm_resource_group.tf-rg-external.name

  ip_configuration {
    name                          = "${var.base_hostname}${format("%02d", count.index)}-iip01"
    subnet_id                     = data.azurerm_subnet.tf-sn.id
    private_ip_address_allocation = "dynamic"
    public_ip_address_id          = element(azurerm_public_ip.tf-pip.*.id, count.index)
  }
}

resource "azurerm_public_ip" "tf-pip" {
  count               = var.vm_count
  location            = data.azurerm_virtual_network.tf-vn.location
  name                = "${var.base_hostname}${format("%02d", count.index + 1)}-pip01"
  resource_group_name = data.azurerm_resource_group.tf-rg-external.name
  allocation_method   = "Dynamic"
}

# Storage Account
resource "azurerm_storage_account" "tf-sa" {
  count                    = var.vm_count
  name                     = "${lower(var.base_hostname)}${format("%02d", count.index + 1)}${var.sto_acc_suffix}01"
  location                 = data.azurerm_virtual_network.tf-vn.location
  resource_group_name      = data.azurerm_resource_group.tf-rg-external.name
  account_tier             = var.sto_acc_tier_std
  account_replication_type = var.sto_acc_rep_type_lrs
}

resource "azurerm_virtual_machine" "tf-vm" {
  count                 = var.vm_count
  name                  = "${var.base_hostname}${format("%02d", count.index + 1)}"
  location              = data.azurerm_virtual_network.tf-vn.location
  resource_group_name   = data.azurerm_resource_group.tf-rg-external.name
  network_interface_ids = [element(azurerm_network_interface.tf-ni.*.id, count.index)]
  vm_size               = var.vm_size

  delete_os_disk_on_termination = true

  delete_data_disks_on_termination = true

  storage_image_reference {
    publisher = var.vm_publisher
    offer     = var.vm_offer
    sku       = var.vm_sku
    version   = var.vm_img_version
  }

  storage_os_disk {
    name              = "${var.base_hostname}${format("%02d", count.index + 1)}-wosdsk01"
    caching           = var.caching_option
    create_option     = var.create_option
    managed_disk_type = var.managed_disk_std_lrs
  }

  os_profile {
    computer_name  = "${var.base_hostname}${format("%02d", count.index + 1)}"
    admin_username = var.username
    admin_password = var.password
  }


  os_profile_windows_config {
    enable_automatic_upgrades = false
    provision_vm_agent        = "true"
  }
}

variables.tf is below:

# Declare env variable
variable "rg_name" {
  type = string
}

variable "vnet_name" {
  type = string
}

variable "subnet_name" {
  type = string
}

variable "app_nsg" {
  type = string
}

variable "vm_count" {
  type = number
}

variable "base_hostname" {
  type = string
}

variable "sto_acc_suffix" {
  type = string
}

variable "sto_acc_tier_std" {
  type    = string
  default = "Standard"
}

variable "sto_acc_rep_type_lrs" {
  type    = string
  default = "LRS"
}

variable "vm_size" {
  type = string
}

variable "vm_publisher" {
  type = string
}

variable "vm_offer" {
  type = string
}

variable "vm_sku" {
  type = string
}

variable "vm_img_version" {
  type = string
}

variable "username" {
  type = string
}

variable "password" {
  type = string
}

variable "caching_option" {
  type    = string
  default = "ReadWrite"
}

variable "create_option" {
  type    = string
  default = "FromImage"
}

variable "managed_disk_std_lrs" {
  type    = string
  default = "Standard_LRS"
}

variable "managed_disk_prem_lrs" {
  type    = string
  default = "Premium_LRS"
}

variable "allowed_source_ips" {
  description = "List of ips from which inbound connection to VMs is allowed"
  type        = list(string)
}

I run below command for upgrading terraform config to 0.12 and above

terraform 0.12upgrade

Error:

Error: Syntax error in configuration file

  on main.tf line 22, in data "azurerm_resource_group" "tf-rg-external":
  22:   name = var.rg_name

Error while parsing: At 22:10: Unknown token: 22:10 IDENT var.rg_name


Error: Syntax error in configuration file

  on variable.tf line 3, in variable "rg_name":
   3:   type = string

Error while parsing: At 3:10: Unknown token: 3:10 IDENT string

Any idea, what is the problem? this would work if I don't run terrafom 0.12upgrade command. It is intriguing me why it is not working. I did same upgrade command in another terraform config and I get similar error there.

One observation. This IDENT error comes for first variable in main.tf and variable.tf file. Unable to correlate this error.

1
I've just went through the same struggle as terraform 0.12.15 has introduced warnings about string interpolation. One can remove all interpolation using the command 0.12upgrade. However the upgrade fails on the same lines where VS code linter would show warnings. I didn't find an easy way out it and I've simply upgraded the string interpolation manually.pijemcolu
I'm wondering however, why are you upgrading to 0.12, if you're obviously running 0.12 already, as we can see in the syntax?pijemcolu
We are using it as this "${var.CLUSTER}"libik
I don't udnerstand why are you running terraform 0.12upgrade. What's the output of terraform --version within your project ?pijemcolu
Actually the issue is with 0.12upgrade command. That was producing the error in the question. Also, VS Code gives error but in reality terraform plan and apply works fine.learner

1 Answers

2
votes

You're already using 0.12 syntax, the 0.12upgrade command expects to find 0.11 syntax and will attempt to automatically update it.

e.g. name = var.rg_name - note the lack of ${}

See https://www.terraform.io/docs/commands/0.12upgrade.html

The terraform 0.12upgrade command applies several automatic upgrade rules to help prepare a module that was written for Terraform v0.11 to be used with Terraform v0.12.

(Emphasis mine)