Skip to content

Instantly share code, notes, and snippets.

@LarsPeterson
Created July 28, 2021 21:09
Show Gist options
  • Save LarsPeterson/8b3173dbdec566067b6c4b7d48f504f8 to your computer and use it in GitHub Desktop.
Save LarsPeterson/8b3173dbdec566067b6c4b7d48f504f8 to your computer and use it in GitHub Desktop.
Terraform: Azure Linux VM
################################################# Azure Provider #################################################
## <https://www.terraform.io/docs/providers/azurerm/index.html>
provider "azurerm" {
features {}
}
################################################# Resource Group #################################################
## <https://www.terraform.io/docs/providers/azurerm/r/resource_group.html>
resource "azurerm_resource_group" "primary" {
name = "TerraformTrinity"
location = "eastus"
}
################################################# Virtual Network #################################################
## <https://www.terraform.io/docs/providers/azurerm/r/virtual_network.html>
resource "azurerm_virtual_network" "primary" {
name = "dora-virtual-network"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.primary.location
resource_group_name = azurerm_resource_group.primary.name
}
################################################# Subnet #################################################
## <https://www.terraform.io/docs/providers/azurerm/r/subnet.html>
resource "azurerm_subnet" "subnet" {
name = "dora-subnet"
resource_group_name = azurerm_resource_group.primary.name
virtual_network_name = azurerm_virtual_network.primary.name
address_prefix = "10.0.2.0/24"
}
################################################# Public Network #################################################
## <https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/public_ip>
resource "azurerm_public_ip" "primary" {
name = "dora-public-ip"
resource_group_name = azurerm_resource_group.primary.name
location = azurerm_resource_group.primary.location
allocation_method = "Static"
}
################################################# Network Interface #################################################
## <https://www.terraform.io/docs/providers/azurerm/r/network_interface.html>
resource "azurerm_network_interface" "primary" {
name = "dora-network-interface"
location = azurerm_resource_group.primary.location
resource_group_name = azurerm_resource_group.primary.name
ip_configuration {
name = "dora-network-interface-config"
subnet_id = azurerm_subnet.subnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.primary.id
}
}
################################################# TLS/SSH PRIVATE KEY #################################################
## <https://www.terraform.io/docs/providers/azurerm/r/windows_virtual_machine.html>
resource "tls_private_key" "primary" {
algorithm = "RSA"
rsa_bits = 4096
}
output "tls_private_key" {
value = tls_private_key.primary.private_key_pem
sensitive = true
}
################################################# Virtual Machine #################################################
## <https://www.terraform.io/docs/providers/azurerm/r/windows_virtual_machine.html>
resource "azurerm_linux_virtual_machine" "dora" {
name = "DoraVM"
location = "eastus"
resource_group_name = azurerm_resource_group.primary.name
network_interface_ids = [azurerm_network_interface.primary.id]
size = "Standard_DS1_v2"
os_disk {
name = "DoraOS"
caching = "ReadWrite"
storage_account_type = "Premium_LRS"
}
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
computer_name = "dora"
admin_username = "dora"
disable_password_authentication = true
admin_ssh_key {
username = "dora"
public_key = file("~/.ssh/dora.pub")
}
}
################################################# Virtual Machine Extension #################################################
## <https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_machine_extension>
resource "azurerm_virtual_machine_extension" "primary" {
name = "hostname"
virtual_machine_id = azurerm_linux_virtual_machine.dora.id
publisher = "Microsoft.Azure.Extensions"
type = "CustomScript"
type_handler_version = "2.0"
settings = <<SETTINGS
{
"commandToExecute": "hostname && uptime"
}
SETTINGS
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment