Terraform failing to render with invalid character

Viewed 1234

I have a terraform script that deploys a linux VM into azure, snippet below

data "template_file" "setup_script" {
template = file("myscript.sh")
}

resource "azurerm_linux_virtual_machine" "myterraformvm" {
    name                  = var.vmname
    location              = var.zone
    resource_group_name   = azurerm_resource_group.myresourcegroup.name
    network_interface_ids = [azurerm_network_interface.myterraformnic.id]
    size                  = "Standard_DS1_v2"

    os_disk {
        name              = "myOsDisk"
        caching           = "ReadWrite"
        storage_account_type = "Premium_LRS"
    }

    source_image_reference {
        publisher = "Canonical"
        offer     = "UbuntuServer"
        sku       = "16.04-LTS"
        version   = "latest"
    }

    computer_name  = var.vmname
    admin_username = "myusername"
    disable_password_authentication = true
    custom_data = base64encode(data.template_file.setup_script.rendered)

tags = {
    environment = var.envname
    }

}

On boot I want to run the script myscript.sh which derives from template_file. It looks like this

#!/bin/bash
REMOTEHOST=8.8.8.8
REMOTEPORT=(22 80 443)
TIMEOUT=1

for i in "${REMOTEPORT[@]}"; do
    if nc -w 1 -z 8.8.8.8 $i; then
        echo "I was able to connect via $i" >> /tmp/output.txt
    else
        echo "Connection failed on $i. Exit code from Netcat was ($?)." >> /tmp/output.txt
    fi
done

When i run terraform apply it get the following error

fatal: [localhost]: FAILED! => changed=false
  msg: |-
Terraform plan could not be created
STDOUT:

STDERR:
Error: failed to render : <template_file>:6,24-25: Invalid character; This character is not used within the language., and 1 other diagnostic(s)

  on main.tf line 134, in data "template_file" "setup_script":
 134: data "template_file" "setup_script" {

The bash script works fine if i run it locally and works in the terraform deployment if I remove the for loop/'@' character and just do a static run. Is there a way to loop over an array in a bash file and deploy it on azurerm_linux_virtual_machine?

0 Answers
Related