Skip to content

Instantly share code, notes, and snippets.

@archmangler
Created September 19, 2018 17:07
Show Gist options
  • Save archmangler/3a43c2eb228c22e7faae54bbee43b98f to your computer and use it in GitHub Desktop.
Save archmangler/3a43c2eb228c22e7faae54bbee43b98f to your computer and use it in GitHub Desktop.
Full Azure VM example including boot diagnostics
/*Tested Ok example of Azure VM deployment*/
variable "prefix" {
default = "tfvmex"
}
/*Create a resource group in a region*/
resource "azurerm_resource_group" "main" {
name = "${var.prefix}-resources"
location = "West US 2"
}
/*create a primary virtual network*/
resource "azurerm_virtual_network" "main" {
name = "${var.prefix}-network"
address_space = ["10.0.0.0/16"]
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
}
/*Create a subnet on a virtual network in a resource group*/
resource "azurerm_subnet" "internal" {
name = "internal"
resource_group_name = "${azurerm_resource_group.main.name}"
virtual_network_name = "${azurerm_virtual_network.main.name}"
address_prefix = "10.0.2.0/24"
}
/*create a primary network interface (before the VM is created)*/
resource "azurerm_network_interface" "main" {
name = "${var.prefix}-nic"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
ip_configuration {
name = "testconfiguration1"
subnet_id = "${azurerm_subnet.internal.id}"
private_ip_address_allocation = "dynamic"
}
}
/*Create a virtual machine in the defined resource group with the
created primary interface in the defined network on the designated
subnet :-) */
resource "azurerm_virtual_machine" "main" {
name = "${var.prefix}-vm"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
network_interface_ids = ["${azurerm_network_interface.main.id}"]
vm_size = "Standard_DS1_v2"
# Uncomment this line to delete the OS disk automatically when deleting the VM
delete_os_disk_on_termination = true
# Uncomment this line to delete the data disks automatically when deleting the VM
#delete_data_disks_on_termination = true
storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}
storage_os_disk {
name = "myosdisk1"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile {
computer_name = "hostname"
admin_username = "testadmin"
admin_password = "Password1234!"
}
os_profile_linux_config {
disable_password_authentication = false
}
boot_diagnostics {
enabled = "true"
storage_uri = "${azurerm_storage_account.diagstorage2.primary_blob_endpoint}"
}
tags {
environment = "development"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment