Skip to content

Terraform with azure

wordpress meta

title: 'Terraform with Azure'
date: '2018-09-15T11:54:39-05:00'
status: publish
permalink: /terraform-with-azure
author: admin
excerpt: ''
type: post
id: 1264
category:
    - Azure
    - Terraform
tag: []
post_format: []

Azure provides a cloud shell with Terraform already installed and even an editor aware of Terraform source. I tested creating a network and VM using the cloud shell in the browser.

REF: https://docs.microsoft.com/en-us/azure/virtual-machines/linux/terraform-install-configure

Note: I am not showing dry run (terraform plan) but you should always be in the habit of plan before apply.

###################################################################
Using cloud shell prompts for a storage account tied to your shell.
###################################################################

Your cloud drive has been created in:

Subscription Id: b[..]b
Resource group:  cloud-shell-storage-southcentralus
Storage account: c[..]4
File share:      cs-[..]2

Initializing your account for Cloud Shell...\
Requesting a Cloud Shell.Succeeded.
Connecting terminal...

Welcome to Azure Cloud Shell

Type "az" to use Azure CLI 2.0
Type "help" to learn about Cloud Shell

#######
Test az
#######

riaan@Azure:~$ az vm list
[
  {
    "availabilitySet": null,
    "diagnosticsProfile": {
      "bootDiagnostics": {
        "enabled": true,
        "storageUri": "https://sitesdiag.blob.core.windows.net/"
      }
    },
    "hardwareProfile": {
      "vmSize": "Standard_B1s"
    },
 [..]
  }
]

###################################################################################################################
Check folder and space available
###################################################################################################################

riaan@Azure:~$ ls
clouddrive
riaan@Azure:~$ df -h .
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop0      5.0G   11M  4.7G   1% /home/riaan

riaan@Azure:~$ az account show --query "{subscriptionId:id, tenantId:tenantId}"
{
  "subscriptionId": "b[..]",
  "tenantId": "f[..]"
}

###################################################################################################################
Simple terraform test add a resource group
###################################################################################################################

riaan@Azure:~$ mkdir sites
riaan@Azure:~$ cd sites
riaan@Azure:~/sites$ vi test.tf
riaan@Azure:~/sites$ terraform init

Initializing provider plugins...
- Checking for available provider plugins on https://releases.hashicorp.com...
- Downloading plugin for provider "azurerm" (1.15.0)...
[..]
* provider.azurerm: version = "~> 1.15"
Terraform has been successfully initialized!

riaan@Azure:~/sites$ terraform apply
[..]
  + create

Terraform will perform the following actions:

  + azurerm_resource_group.rg
      id:       <computed>
      location: "centralus"
      name:     "testResourceGroup"
      tags.%:   <computed>

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

azurerm_resource_group.rg: Creating...
  location: "" => "centralus"
  name:     "" => "testResourceGroup"
  tags.%:   "" => "<computed>"
azurerm_resource_group.rg: Creation complete after 0s (ID: /subscriptions/b[..]/resourceGroups/testResourceGroup)

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

#######################################################################
Simple terraform test. Destroy a resource group. I just comment it out.
#######################################################################

riaan@Azure:~/sites$ vi test.tf
riaan@Azure:~/sites$ terraform apply
azurerm_resource_group.rg: Refreshing state... (ID: /subscriptions/b[..]/resourceGroups/testResourceGroup)

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  - azurerm_resource_group.rg

Plan: 0 to add, 0 to change, 1 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

azurerm_resource_group.rg: Destroying... (ID: /subscriptions/b[..]/resourceGroups/testResourceGroup)
azurerm_resource_group.rg: Still destroying... (ID: /subscriptions/b[..]/resourceGroups/testResourceGroup, 10s elapsed)
[..]
azurerm_resource_group.rg: Destruction complete after 45s

Apply complete! Resources: 0 added, 0 changed, 1 destroyed.

###################################################################################################################
Create VM plus network
https://docs.microsoft.com/en-us/azure/virtual-machines/linux/terraform-create-complete-vm
###################################################################################################################

riaan@Azure:~/sites$ rm test.tf
riaan@Azure:~/sites$ vi main.tf
riaan@Azure:~/sites$ terraform apply

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + azurerm_network_interface.myterraformnic
      id:                                                                    <computed>
      applied_dns_servers.#:                                                 <computed>
      dns_servers.#:                                                         <computed>
      enable_accelerated_networking:                                         "false"
      enable_ip_forwarding:                                                  "false"
      internal_dns_name_label:                                               <computed>
      internal_fqdn:                                                         <computed>
      ip_configuration.#:                                                    "1"
      ip_configuration.0.application_gateway_backend_address_pools_ids.#:    <computed>
      ip_configuration.0.application_security_group_ids.#:                   <computed>
      ip_configuration.0.load_balancer_backend_address_pools_ids.#:          <computed>
      ip_configuration.0.load_balancer_inbound_nat_rules_ids.#:              <computed>
      ip_configuration.0.name:                                               "myNicConfiguration"
      ip_configuration.0.primary:                                            <computed>
      ip_configuration.0.private_ip_address_allocation:                      "dynamic"
      ip_configuration.0.public_ip_address_id:                               "${azurerm_public_ip.myterraformpublicip.id}"
      ip_configuration.0.subnet_id:                                          "${azurerm_subnet.myterraformsubnet.id}"
      location:                                                              "eastus"
      mac_address:                                                           <computed>
      name:                                                                  "myNIC"
      network_security_group_id:                                             "${azurerm_network_security_group.myterraformnsg.id}"
      private_ip_address:                                                    <computed>
      private_ip_addresses.#:                                                <computed>
      resource_group_name:                                                   "myResourceGroup"
      tags.%:                                                                "1"
      tags.environment:                                                      "Terraform Demo"
      virtual_machine_id:                                                    <computed>

  + azurerm_network_security_group.myterraformnsg
      id:                                                                    <computed>
      location:                                                              "eastus"
      name:                                                                  "myNetworkSecurityGroup"
      resource_group_name:                                                   "myResourceGroup"
      security_rule.#:                                                       "1"
      security_rule.2090309246.access:                                       "Allow"
      security_rule.2090309246.description:                                  ""
      security_rule.2090309246.destination_address_prefix:                   "*"
      security_rule.2090309246.destination_address_prefixes.#:               "0"
      security_rule.2090309246.destination_application_security_group_ids.#: "0"
      security_rule.2090309246.destination_port_range:                       "22"
      security_rule.2090309246.destination_port_ranges.#:                    "0"
      security_rule.2090309246.direction:                                    "Inbound"
      security_rule.2090309246.name:                                         "SSH"
      security_rule.2090309246.priority:                                     "1001"
      security_rule.2090309246.protocol:                                     "Tcp"
      security_rule.2090309246.source_address_prefix:                        "*"
      security_rule.2090309246.source_address_prefixes.#:                    "0"
      security_rule.2090309246.source_application_security_group_ids.#:      "0"
      security_rule.2090309246.source_port_range:                            "*"
      security_rule.2090309246.source_port_ranges.#:                         "0"
      tags.%:                                                                "1"
      tags.environment:                                                      "Terraform Demo"

  + azurerm_public_ip.myterraformpublicip
      id:                                                                    <computed>
      fqdn:                                                                  <computed>
      ip_address:                                                            <computed>
      location:                                                              "eastus"
      name:                                                                  "myPublicIP"
      public_ip_address_allocation:                                          "dynamic"
      resource_group_name:                                                   "myResourceGroup"
      sku:                                                                   "Basic"
      tags.%:                                                                "1"
      tags.environment:                                                      "Terraform Demo"

  + azurerm_resource_group.myterraformgroup
      id:                                                                    <computed>
      location:                                                              "eastus"
      name:                                                                  "myResourceGroup"
      tags.%:                                                                "1"
      tags.environment:                                                      "Terraform Demo"

  + azurerm_storage_account.mystorageaccount
      id:                                                                    <computed>
      access_tier:                                                           <computed>
      account_encryption_source:                                             "Microsoft.Storage"
      account_kind:                                                          "Storage"
      account_replication_type:                                              "LRS"
      account_tier:                                                          "Standard"
      enable_blob_encryption:                                                "true"
      enable_file_encryption:                                                "true"
      identity.#:                                                            <computed>
      location:                                                              "eastus"
      name:                                                                  "diag${random_id.randomId.hex}"
      primary_access_key:                                                    <computed>
      primary_blob_connection_string:                                        <computed>
      primary_blob_endpoint:                                                 <computed>
      primary_connection_string:                                             <computed>
      primary_file_endpoint:                                                 <computed>
      primary_location:                                                      <computed>
      primary_queue_endpoint:                                                <computed>
      primary_table_endpoint:                                                <computed>
      resource_group_name:                                                   "myResourceGroup"
      secondary_access_key:                                                  <computed>
      secondary_blob_connection_string:                                      <computed>
      secondary_blob_endpoint:                                               <computed>
      secondary_connection_string:                                           <computed>
      secondary_location:                                                    <computed>
      secondary_queue_endpoint:                                              <computed>
      secondary_table_endpoint:                                              <computed>
      tags.%:                                                                "1"
      tags.environment:                                                      "Terraform Demo"

  + azurerm_subnet.myterraformsubnet
      id:                                                                    <computed>
      address_prefix:                                                        "10.0.1.0/24"
      ip_configurations.#:                                                   <computed>
      name:                                                                  "mySubnet"
      resource_group_name:                                                   "myResourceGroup"
      virtual_network_name:                                                  "myVnet"

  + azurerm_virtual_machine.myterraformvm
      id:                                                                    <computed>
      availability_set_id:                                                   <computed>
      boot_diagnostics.#:                                                    "1"
      boot_diagnostics.0.enabled:                                            "true"
      boot_diagnostics.0.storage_uri:                                        "${azurerm_storage_account.mystorageaccount.primary_blob_endpoint}"
      delete_data_disks_on_termination:                                      "false"
      delete_os_disk_on_termination:                                         "false"
      identity.#:                                                            <computed>
      location:                                                              "eastus"
      name:                                                                  "myVM"
      network_interface_ids.#:                                               <computed>
      os_profile.#:                                                          "1"
      os_profile.1770182618.admin_password:                                  <sensitive>
      os_profile.1770182618.admin_username:                                  "azureuser"
      os_profile.1770182618.computer_name:                                   "myvm"
      os_profile.1770182618.custom_data:                                     <computed>
      os_profile_linux_config.#:                                             "1"
      os_profile_linux_config.69840937.disable_password_authentication:      "true"
      os_profile_linux_config.69840937.ssh_keys.#:                           "1"
      os_profile_linux_config.69840937.ssh_keys.0.key_data:                  "ssh-rsa AAAAB3Nz{snip}hwhqT9h"
      os_profile_linux_config.69840937.ssh_keys.0.path:                      "/home/azureuser/.ssh/authorized_keys"
      resource_group_name:                                                   "myResourceGroup"
      storage_data_disk.#:                                                   <computed>
      storage_image_reference.#:                                             "1"
      storage_image_reference.363552096.id:                                  ""
      storage_image_reference.363552096.offer:                               "UbuntuServer"
      storage_image_reference.363552096.publisher:                           "Canonical"
      storage_image_reference.363552096.sku:                                 "16.04.0-LTS"
      storage_image_reference.363552096.version:                             "latest"
      storage_os_disk.#:                                                     "1"
      storage_os_disk.0.caching:                                             "ReadWrite"
      storage_os_disk.0.create_option:                                       "FromImage"
      storage_os_disk.0.disk_size_gb:                                        <computed>
      storage_os_disk.0.managed_disk_id:                                     <computed>
      storage_os_disk.0.managed_disk_type:                                   "Premium_LRS"
      storage_os_disk.0.name:                                                "myOsDisk"
      storage_os_disk.0.write_accelerator_enabled:                           "false"
      tags.%:                                                                "1"
      tags.environment:                                                      "Terraform Demo"
      vm_size:                                                               "Standard_DS1_v2"

  + azurerm_virtual_network.myterraformnetwork
      id:                                                                    <computed>
      address_space.#:                                                       "1"
      address_space.0:                                                       "10.0.0.0/16"
      location:                                                              "eastus"
      name:                                                                  "myVnet"
      resource_group_name:                                                   "myResourceGroup"
      subnet.#:                                                              <computed>
      tags.%:                                                                "1"
      tags.environment:                                                      "Terraform Demo"

  + random_id.randomId
      id:                                                                    <computed>
      b64:                                                                   <computed>
      b64_std:                                                               <computed>
      b64_url:                                                               <computed>
      byte_length:                                                           "8"
      dec:                                                                   <computed>
      hex:                                                                   <computed>
      keepers.%:                                                             "1"
      keepers.resource_group:                                                "myResourceGroup"


Plan: 9 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

azurerm_resource_group.myterraformgroup: Creating...
  location:         "" => "eastus"
  name:             "" => "myResourceGroup"
  tags.%:           "" => "1"
  tags.environment: "" => "Terraform Demo"
azurerm_resource_group.myterraformgroup: Creation complete after 0s (ID: /subscriptions/b[..]/resourceGroups/myResourceGroup)
azurerm_virtual_network.myterraformnetwork: Creating...
  address_space.#:     "" => "1"
  address_space.0:     "" => "10.0.0.0/16"
  location:            "" => "eastus"
  name:                "" => "myVnet"
  resource_group_name: "" => "myResourceGroup"
  subnet.#:            "" => "<computed>"
  tags.%:              "" => "1"
  tags.environment:    "" => "Terraform Demo"
azurerm_public_ip.myterraformpublicip: Creating...
  fqdn:                         "" => "<computed>"
  ip_address:                   "" => "<computed>"
  location:                     "" => "eastus"
  name:                         "" => "myPublicIP"
  public_ip_address_allocation: "" => "dynamic"
  resource_group_name:          "" => "myResourceGroup"
  sku:                          "" => "Basic"
  tags.%:                       "" => "1"
  tags.environment:             "" => "Terraform Demo"
random_id.randomId: Creating...
  b64:                    "" => "<computed>"
  b64_std:                "" => "<computed>"
  b64_url:                "" => "<computed>"
  byte_length:            "" => "8"
  dec:                    "" => "<computed>"
  hex:                    "" => "<computed>"
  keepers.%:              "" => "1"
  keepers.resource_group: "" => "myResourceGroup"
random_id.randomId: Creation complete after 0s (ID: ZZic4bhOapM)
azurerm_network_security_group.myterraformnsg: Creating...
  location:                                                              "" => "eastus"
  name:                                                                  "" => "myNetworkSecurityGroup"
  resource_group_name:                                                   "" => "myResourceGroup"
  security_rule.#:                                                       "" => "1"
  security_rule.2090309246.access:                                       "" => "Allow"
  security_rule.2090309246.description:                                  "" => ""
  security_rule.2090309246.destination_address_prefix:                   "" => "*"
  security_rule.2090309246.destination_address_prefixes.#:               "" => "0"
  security_rule.2090309246.destination_application_security_group_ids.#: "" => "0"
  security_rule.2090309246.destination_port_range:                       "" => "22"
  security_rule.2090309246.destination_port_ranges.#:                    "" => "0"
  security_rule.2090309246.direction:                                    "" => "Inbound"
  security_rule.2090309246.name:                                         "" => "SSH"
  security_rule.2090309246.priority:                                     "" => "1001"
  security_rule.2090309246.protocol:                                     "" => "Tcp"
  security_rule.2090309246.source_address_prefix:                        "" => "*"
  security_rule.2090309246.source_address_prefixes.#:                    "" => "0"
  security_rule.2090309246.source_application_security_group_ids.#:      "" => "0"
  security_rule.2090309246.source_port_range:                            "" => "*"
  security_rule.2090309246.source_port_ranges.#:                         "" => "0"
  tags.%:                                                                "" => "1"
  tags.environment:                                                      "" => "Terraform Demo"
azurerm_storage_account.mystorageaccount: Creating...
  access_tier:                      "" => "<computed>"
  account_encryption_source:        "" => "Microsoft.Storage"
  account_kind:                     "" => "Storage"
  account_replication_type:         "" => "LRS"
  account_tier:                     "" => "Standard"
  enable_blob_encryption:           "" => "true"
  enable_file_encryption:           "" => "true"
  identity.#:                       "" => "<computed>"
  location:                         "" => "eastus"
  name:                             "" => "diag6[..]"
  primary_access_key:               "<sensitive>" => "<sensitive>"
  primary_blob_connection_string:   "<sensitive>" => "<sensitive>"
  primary_blob_endpoint:            "" => "<computed>"
  primary_connection_string:        "<sensitive>" => "<sensitive>"
  primary_file_endpoint:            "" => "<computed>"
  primary_location:                 "" => "<computed>"
  primary_queue_endpoint:           "" => "<computed>"
  primary_table_endpoint:           "" => "<computed>"
  resource_group_name:              "" => "myResourceGroup"
  secondary_access_key:             "<sensitive>" => "<sensitive>"
  secondary_blob_connection_string: "<sensitive>" => "<sensitive>"
  secondary_blob_endpoint:          "" => "<computed>"
  secondary_connection_string:      "<sensitive>" => "<sensitive>"
  secondary_location:               "" => "<computed>"
  secondary_queue_endpoint:         "" => "<computed>"
  secondary_table_endpoint:         "" => "<computed>"
  tags.%:                           "" => "1"
  tags.environment:                 "" => "Terraform Demo"
azurerm_public_ip.myterraformpublicip: Creation complete after 2s (ID: /subscriptions/b[..]-...t.Network/publicIPAddresses/myPublicIP)
azurerm_network_security_group.myterraformnsg: Creation complete after 2s (ID: /subscriptions/b[..]-...kSecurityGroups/myNetworkSecurityGroup)
azurerm_virtual_network.myterraformnetwork: Still creating... (10s elapsed)
azurerm_storage_account.mystorageaccount: Still creating... (10s elapsed)
azurerm_storage_account.mystorageaccount: Creation complete after 19s (ID: /subscriptions/b[..]-...e/storageAccounts/diag6[..])
azurerm_virtual_network.myterraformnetwork: Still creating... (20s elapsed)
azurerm_virtual_network.myterraformnetwork: Creation complete after 22s (ID: /subscriptions/b[..]-...crosoft.Network/virtualNetworks/myVnet)
azurerm_subnet.myterraformsubnet: Creating...
  address_prefix:       "" => "10.0.1.0/24"
  ip_configurations.#:  "" => "<computed>"
  name:                 "" => "mySubnet"
  resource_group_name:  "" => "myResourceGroup"
  virtual_network_name: "" => "myVnet"
azurerm_subnet.myterraformsubnet: Still creating... (10s elapsed)
azurerm_subnet.myterraformsubnet: Creation complete after 11s (ID: /subscriptions/b[..]-...irtualNetworks/myVnet/subnets/mySubnet)
azurerm_network_interface.myterraformnic: Creating...
  applied_dns_servers.#:                                              "" => "<computed>"
  dns_servers.#:                                                      "" => "<computed>"
  enable_accelerated_networking:                                      "" => "false"
  enable_ip_forwarding:                                               "" => "false"
  internal_dns_name_label:                                            "" => "<computed>"
  internal_fqdn:                                                      "" => "<computed>"
  ip_configuration.#:                                                 "" => "1"
  ip_configuration.0.application_gateway_backend_address_pools_ids.#: "" => "<computed>"
  ip_configuration.0.application_security_group_ids.#:                "" => "<computed>"
  ip_configuration.0.load_balancer_backend_address_pools_ids.#:       "" => "<computed>"
  ip_configuration.0.load_balancer_inbound_nat_rules_ids.#:           "" => "<computed>"
  ip_configuration.0.name:                                            "" => "myNicConfiguration"
  ip_configuration.0.primary:                                         "" => "<computed>"
  ip_configuration.0.private_ip_address_allocation:                   "" => "dynamic"
  ip_configuration.0.public_ip_address_id:                            "" => "/subscriptions/b[..]/resourceGroups/myResourceGroup/providers/Microsoft.Network/publicIPAddresses/myPublicIP"
  ip_configuration.0.subnet_id:                                       "" => "/subscriptions/b[..]/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet"
  location:                                                           "" => "eastus"
  mac_address:                                                        "" => "<computed>"
  name:                                                               "" => "myNIC"
  network_security_group_id:                                          "" => "/subscriptions/b[..]/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/myNetworkSecurityGroup"
  private_ip_address:                                                 "" => "<computed>"
  private_ip_addresses.#:                                             "" => "<computed>"
  resource_group_name:                                                "" => "myResourceGroup"
  tags.%:                                                             "" => "1"
  tags.environment:                                                   "" => "Terraform Demo"
  virtual_machine_id:                                                 "" => "<computed>"
azurerm_network_interface.myterraformnic: Creation complete after 1s (ID: /subscriptions/b[..]-...rosoft.Network/networkInterfaces/myNIC)
azurerm_virtual_machine.myterraformvm: Creating...
  availability_set_id:                                              "" => "<computed>"
  boot_diagnostics.#:                                               "" => "1"
  boot_diagnostics.0.enabled:                                       "" => "true"
  boot_diagnostics.0.storage_uri:                                   "" => "https://diag[..].blob.core.windows.net/"
  delete_data_disks_on_termination:                                 "" => "false"
  delete_os_disk_on_termination:                                    "" => "false"
  identity.#:                                                       "" => "<computed>"
  location:                                                         "" => "eastus"
  name:                                                             "" => "myVM"
  network_interface_ids.#:                                          "" => "1"
  network_interface_ids.0:                                          "" => "/subscriptions/b[..]/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkInterfaces/myNIC"
  os_profile.#:                                                     "" => "1"
  os_profile.1770182618.admin_password:                             "<sensitive>" => "<sensitive>"
  os_profile.1770182618.admin_username:                             "" => "azureuser"
  os_profile.1770182618.computer_name:                              "" => "myvm"
  os_profile.1770182618.custom_data:                                "" => "<computed>"
  os_profile_linux_config.#:                                        "" => "1"
  os_profile_linux_config.69840937.disable_password_authentication: "" => "true"
  os_profile_linux_config.69840937.ssh_keys.#:                      "" => "1"
  os_profile_linux_config.69840937.ssh_keys.0.key_data:             "" => "ssh-rsa AAAAB3Nz{snip}hwhqT9h"
  os_profile_linux_config.69840937.ssh_keys.0.path:                 "" => "/home/azureuser/.ssh/authorized_keys"
  resource_group_name:                                              "" => "myResourceGroup"
  storage_data_disk.#:                                              "" => "<computed>"
  storage_image_reference.#:                                        "" => "1"
  storage_image_reference.363552096.id:                             "" => ""
  storage_image_reference.363552096.offer:                          "" => "UbuntuServer"
  storage_image_reference.363552096.publisher:                      "" => "Canonical"
  storage_image_reference.363552096.sku:                            "" => "16.04.0-LTS"
  storage_image_reference.363552096.version:                        "" => "latest"
  storage_os_disk.#:                                                "" => "1"
  storage_os_disk.0.caching:                                        "" => "ReadWrite"
  storage_os_disk.0.create_option:                                  "" => "FromImage"
  storage_os_disk.0.disk_size_gb:                                   "" => "<computed>"
  storage_os_disk.0.managed_disk_id:                                "" => "<computed>"
  storage_os_disk.0.managed_disk_type:                              "" => "Premium_LRS"
  storage_os_disk.0.name:                                           "" => "myOsDisk"
  storage_os_disk.0.write_accelerator_enabled:                      "" => "false"
  tags.%:                                                           "" => "1"
  tags.environment:                                                 "" => "Terraform Demo"
  vm_size:                                                          "" => "Standard_DS1_v2"

Error: Error applying plan:

1 error(s) occurred:

* azurerm_virtual_machine.myterraformvm: 1 error(s) occurred:

* azurerm_virtual_machine.myterraformvm: compute.VirtualMachinesClient#CreateOrUpdate: Failure sending request: StatusCode=0 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="InvalidParameter" Message="The value of parameter linuxConfiguration.ssh.publicKeys.keyData is invalid." Target="linuxConfiguration.ssh.publicKeys.keyData"

Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with
any resources that successfully completed. Please address the error
above and apply again to incrementally change your infrastructure.

######################################################
Add a valid ssh public key to continue creating the VM
######################################################

riaan@Azure:~/sites$ vi main.tf
riaan@Azure:~/sites$ terraform apply
azurerm_resource_group.myterraformgroup: Refreshing state... (ID: /subscriptions/b[..]/resourceGroups/myResourceGroup)
azurerm_network_security_group.myterraformnsg: Refreshing state... (ID: /subscriptions/b[..]-...kSecurityGroups/myNetworkSecurityGroup)
random_id.randomId: Refreshing state... (ID: ZZic4bhOapM)
azurerm_virtual_network.myterraformnetwork: Refreshing state... (ID: /subscriptions/b[..]-...crosoft.Network/virtualNetworks/myVnet)
azurerm_public_ip.myterraformpublicip: Refreshing state... (ID: /subscriptions/b[..]-...t.Network/publicIPAddresses/myPublicIP)
azurerm_storage_account.mystorageaccount: Refreshing state... (ID: /subscriptions/b[..]-...e/storageAccounts/diag[..])
azurerm_subnet.myterraformsubnet: Refreshing state... (ID: /subscriptions/b[..]-...irtualNetworks/myVnet/subnets/mySubnet)
azurerm_network_interface.myterraformnic: Refreshing state... (ID: /subscriptions/b[..]-...rosoft.Network/networkInterfaces/myNIC)

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + azurerm_virtual_machine.myterraformvm
      id:                                                               <computed>
      availability_set_id:                                              <computed>
      boot_diagnostics.#:                                               "1"
      boot_diagnostics.0.enabled:                                       "true"
      boot_diagnostics.0.storage_uri:                                   "https://diag[..].blob.core.windows.net/"
      delete_data_disks_on_termination:                                 "false"
      delete_os_disk_on_termination:                                    "false"
      identity.#:                                                       <computed>
      location:                                                         "eastus"
      name:                                                             "myVM"
      network_interface_ids.#:                                          "1"
      network_interface_ids.0:                                          "/subscriptions/b[..]/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkInterfaces/myNIC"
      os_profile.#:                                                     "1"
      os_profile.1770182618.admin_password:                             <sensitive>
      os_profile.1770182618.admin_username:                             "azureuser"
      os_profile.1770182618.computer_name:                              "myvm"
      os_profile.1770182618.custom_data:                                <computed>
      os_profile_linux_config.#:                                        "1"
      os_profile_linux_config.69840937.disable_password_authentication: "true"
      os_profile_linux_config.69840937.ssh_keys.#:                      "1"
      os_profile_linux_config.69840937.ssh_keys.0.key_data:             "ssh-rsa [..]"
      os_profile_linux_config.69840937.ssh_keys.0.path:                 "/home/azureuser/.ssh/authorized_keys"
      resource_group_name:                                              "myResourceGroup"
      storage_data_disk.#:                                              <computed>
      storage_image_reference.#:                                        "1"
      storage_image_reference.363552096.id:                             ""
      storage_image_reference.363552096.offer:                          "UbuntuServer"
      storage_image_reference.363552096.publisher:                      "Canonical"
      storage_image_reference.363552096.sku:                            "16.04.0-LTS"
      storage_image_reference.363552096.version:                        "latest"
      storage_os_disk.#:                                                "1"
      storage_os_disk.0.caching:                                        "ReadWrite"
      storage_os_disk.0.create_option:                                  "FromImage"
      storage_os_disk.0.disk_size_gb:                                   <computed>
      storage_os_disk.0.managed_disk_id:                                <computed>
      storage_os_disk.0.managed_disk_type:                              "Premium_LRS"
      storage_os_disk.0.name:                                           "myOsDisk"
      storage_os_disk.0.write_accelerator_enabled:                      "false"
      tags.%:                                                           "1"
      tags.environment:                                                 "Terraform Demo"
      vm_size:                                                          "Standard_DS1_v2"


Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

azurerm_virtual_machine.myterraformvm: Creating...
  availability_set_id:                                              "" => "<computed>"
  boot_diagnostics.#:                                               "" => "1"
  boot_diagnostics.0.enabled:                                       "" => "true"
  boot_diagnostics.0.storage_uri:                                   "" => "https://diag[..].blob.core.windows.net/"
  delete_data_disks_on_termination:                                 "" => "false"
  delete_os_disk_on_termination:                                    "" => "false"
  identity.#:                                                       "" => "<computed>"
  location:                                                         "" => "eastus"
  name:                                                             "" => "myVM"
  network_interface_ids.#:                                          "" => "1"
  network_interface_ids.0:                                          "" => "/subscriptions/b[..]/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkInterfaces/myNIC"
  os_profile.#:                                                     "" => "1"
  os_profile.1770182618.admin_password:                             "<sensitive>" => "<sensitive>"
  os_profile.1770182618.admin_username:                             "" => "azureuser"
  os_profile.1770182618.computer_name:                              "" => "myvm"
  os_profile.1770182618.custom_data:                                "" => "<computed>"
  os_profile_linux_config.#:                                        "" => "1"
  os_profile_linux_config.69840937.disable_password_authentication: "" => "true"
  os_profile_linux_config.69840937.ssh_keys.#:                      "" => "1"
  os_profile_linux_config.69840937.ssh_keys.0.key_data:             "" => "ssh-rsa A[..]"
  os_profile_linux_config.69840937.ssh_keys.0.path:                 "" => "/home/azureuser/.ssh/authorized_keys"
  resource_group_name:                                              "" => "myResourceGroup"
  storage_data_disk.#:                                              "" => "<computed>"
  storage_image_reference.#:                                        "" => "1"
  storage_image_reference.363552096.id:                             "" => ""
  storage_image_reference.363552096.offer:                          "" => "UbuntuServer"
  storage_image_reference.363552096.publisher:                      "" => "Canonical"
  storage_image_reference.363552096.sku:                            "" => "16.04.0-LTS"
  storage_image_reference.363552096.version:                        "" => "latest"
  storage_os_disk.#:                                                "" => "1"
  storage_os_disk.0.caching:                                        "" => "ReadWrite"
  storage_os_disk.0.create_option:                                  "" => "FromImage"
  storage_os_disk.0.disk_size_gb:                                   "" => "<computed>"
  storage_os_disk.0.managed_disk_id:                                "" => "<computed>"
  storage_os_disk.0.managed_disk_type:                              "" => "Premium_LRS"
  storage_os_disk.0.name:                                           "" => "myOsDisk"
  storage_os_disk.0.write_accelerator_enabled:                      "" => "false"
  tags.%:                                                           "" => "1"
  tags.environment:                                                 "" => "Terraform Demo"
  vm_size:                                                          "" => "Standard_DS1_v2"
azurerm_virtual_machine.myterraformvm: Still creating... (10s elapsed)
azurerm_virtual_machine.myterraformvm: Still creating... (20s elapsed)
azurerm_virtual_machine.myterraformvm: Still creating... (30s elapsed)
azurerm_virtual_machine.myterraformvm: Still creating... (40s elapsed)
azurerm_virtual_machine.myterraformvm: Still creating... (50s elapsed)
azurerm_virtual_machine.myterraformvm: Still creating... (1m0s elapsed)
azurerm_virtual_machine.myterraformvm: Still creating... (1m10s elapsed)
azurerm_virtual_machine.myterraformvm: Still creating... (1m20s elapsed)
azurerm_virtual_machine.myterraformvm: Still creating... (1m30s elapsed)
azurerm_virtual_machine.myterraformvm: Creation complete after 1m35s (ID: /subscriptions/b[..]-...Microsoft.Compute/virtualMachines/myVM)

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

##################################################################
use built-in editor(vscode?) Comment out the VM so we can destroy
##################################################################

riaan@Azure:~/sites$ terraform apply
azurerm_resource_group.myterraformgroup: Refreshing state...
[..]

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  - azurerm_virtual_machine.myterraformvm


Plan: 0 to add, 0 to change, 1 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

azurerm_virtual_machine.myterraformvm: Destroying... (ID: /subscriptions/[..]-...Microsoft.Compute/virtualMachines/myVM)
[..]
azurerm_virtual_machine.myterraformvm: Destruction complete after 34s

Apply complete! Resources: 0 added, 0 changed, 1 destroyed.