Created
January 24, 2025 10:29
-
-
Save cornfeedhobo/1c1f311f5df07499ab2730624ce3e995 to your computer and use it in GitHub Desktop.
azure vm terraform
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
provider "azurerm" {} | |
resource "azurerm_resource_group" "example" { | |
name = "example" | |
location = "North Central US" | |
} | |
resource "azurerm_virtual_network" "example" { | |
name = "example" | |
address_space = [ | |
"10.20.20.0/24", | |
"fd00:2020::/64", | |
] | |
location = azurerm_resource_group.example.location | |
resource_group_name = azurerm_resource_group.example.name | |
} | |
resource "azurerm_subnet" "example" { | |
name = "example" | |
resource_group_name = azurerm_resource_group.example.name | |
virtual_network_name = azurerm_virtual_network.example.name | |
address_prefixes = [ | |
"10.20.20.0/24", | |
"fd00:2020::/64", | |
] | |
} | |
resource "azurerm_network_security_group" "example" { | |
name = "example" | |
location = azurerm_resource_group.example.location | |
resource_group_name = azurerm_resource_group.example.name | |
security_rule { | |
name = "AllowSSH" | |
priority = 100 | |
direction = "Inbound" | |
protocol = "Tcp" | |
source_address_prefix = "*" | |
source_port_range = "*" | |
destination_address_prefix = "*" | |
destination_port_range = "22" | |
access = "Allow" | |
} | |
} | |
resource "azurerm_subnet_network_security_group_association" "example" { | |
subnet_id = azurerm_subnet.example.id | |
network_security_group_id = azurerm_network_security_group.example.id | |
} | |
resource "azurerm_public_ip" "example-ipv4" { | |
name = "example-ipv4" | |
location = azurerm_resource_group.example.location | |
resource_group_name = azurerm_resource_group.example.name | |
allocation_method = "Static" | |
sku = "Standard" | |
ip_version = "IPv4" | |
} | |
resource "azurerm_public_ip" "example-ipv6" { | |
name = "example-ipv6" | |
location = azurerm_resource_group.example.location | |
resource_group_name = azurerm_resource_group.example.name | |
allocation_method = "Static" | |
sku = "Standard" | |
ip_version = "IPv6" | |
} | |
resource "azurerm_network_interface" "example" { | |
name = "example-internal" | |
location = azurerm_resource_group.example.location | |
resource_group_name = azurerm_resource_group.example.name | |
ip_configuration { | |
name = "internal-ipv4" | |
subnet_id = azurerm_subnet.example.id | |
private_ip_address_allocation = "Dynamic" | |
private_ip_address_version = "IPv4" | |
public_ip_address_id = azurerm_public_ip.example-ipv4.id | |
primary = true | |
} | |
ip_configuration { | |
name = "internal-ipv6" | |
subnet_id = azurerm_subnet.example.id | |
private_ip_address_allocation = "Dynamic" | |
private_ip_address_version = "IPv6" | |
public_ip_address_id = azurerm_public_ip.example-ipv6.id | |
} | |
} | |
resource "random_pet" "example" {} | |
resource "azurerm_virtual_machine" "example" { | |
name = "example" | |
location = azurerm_resource_group.example.location | |
resource_group_name = azurerm_resource_group.example.name | |
network_interface_ids = [azurerm_network_interface.example.id] | |
vm_size = "Standard_A1_v2" | |
// az vm image list --publisher SUSE --offer SLES --all --output table | |
storage_image_reference { | |
publisher = "SUSE" | |
offer = "sles-15-sp6" | |
sku = "gen1" | |
version = "latest" | |
} | |
storage_os_disk { | |
name = "example-osdisk" | |
caching = "ReadWrite" | |
create_option = "FromImage" | |
disk_size_gb = 40 | |
} | |
storage_data_disk { | |
name = "example-datadisk" | |
caching = "ReadWrite" | |
create_option = "Empty" | |
lun = 1 | |
disk_size_gb = 100 | |
} | |
os_profile { | |
computer_name = random_pet.example.id | |
admin_username = "azuser" | |
} | |
os_profile_linux_config { | |
disable_password_authentication = true | |
ssh_keys { | |
path = "/home/azuser/.ssh/authorized_keys" | |
key_data = file("~/.ssh/id_ed25519.pub") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment