chew224
chew224

Reputation: 501

How to use custom_data parameter in ARM template in Terraform?

I have an Azure ARM template that successfully bootstraps a VM from a file directory within an Azure Storage Account. I would like to get this working in Terraform, but I am really struggling getting it to work correctly.

Here is a working Azure ARM template that creates the VM and bootstraps it with files in an Azure storage account. The bootstrapping occurs by using the customData parameter.

"variables": {
  "uniqueId": "[uniqueString(resourceGroup().id)]",
  "customData": "[concat('storage-account=', parameters('STORAGE_ACCOUNT'), ',access-key=', parameters('ACCESS_KEY'), ',file-share=', parameters('FILE_SHARE'), ',share-directory=', parameters('SHARE_DIRECTORY'))]"
},
"resources": [
  {
    "apiVersion": "2016-04-30-preview",
    "type": "Microsoft.Compute/virtualMachines",
    "name": "MY-VM",
    "location": "[resourceGroup().location]",
    "properties": {
      "hardwareProfile": {
          "vmSize": "Standard_DS3_v2"
      },
      "osProfile": {
          "computerName": "My-Computer-Name",
          "adminUsername": "[parameters('Username')]",
          "adminPassword": "[parameters('Password')]",
          "customData": "[base64(variables('customData'))]"
      }
    }
  }

Here is my non-working Terraform script that does not work when I try to do the same type of Bootstrapping.

resource "azurerm_virtual_machine" "MY-VM" {
  name                         = "${var.vm_name}"
  location                     = "${var.location}"
  resource_group_name          = "${azurerm_resource_group.rg.name}"
  vm_size                      = "${var.vm_size}"
  primary_network_interface_id = "${azurerm_network_interface.nic0.id}"

  os_profile {
    computer_name     = "${var.vm_name}"
    admin_username    = "${var.adminuser}"
    admin_password    = "${var.adminuserpassword}"
    custom_data       = "${base64encode(join("", list("storage-account=", var.STORAGE_ACCOUNT, ",access-key=", var.ACCESS_KEY, ",file-share=", var.FILE_SHARE, ",share-directory=None")))}"
  }
}

This is the error that I receive when I run it. If I do not use the custom_data field, the machine launches fine, but is not bootstrapped. I am out of ideas here..

azurerm_virtual_machine.MY-VM: 
compute.VirtualMachinesClient#CreateOrUpdate: Failure sending 
request: StatusCode=0 -- Original Error: autorest/azure: Service 
returned an error. Status=400 Code="InvalidRequestFormat" 
Message="Cannot parse the request." Details=[]

Upvotes: 0

Views: 1314

Answers (1)

4c74356b41
4c74356b41

Reputation: 72151

i dont think join works for strings? for your case you can just do

"storage-account=${var.STORAGE_ACCCOUNT},access-key=${var.ACCESS_KEY},file-share=${var.FILE_SHARE},share-directory=None"

Upvotes: 1

Related Questions