1+ resource "random_pet" "rg_name" {
2+ prefix = var. resource_group_name_prefix
3+ }
4+
5+ resource "azurerm_resource_group" "rg" {
6+ location = var. resource_group_location
7+ name = random_pet. rg_name . id
8+ }
9+
10+ # Create virtual network
11+ resource "azurerm_virtual_network" "myterraformnetwork" {
12+ name = " myVnet"
13+ address_space = [" 10.0.0.0/16" ]
14+ location = azurerm_resource_group. rg . location
15+ resource_group_name = azurerm_resource_group. rg . name
16+ }
17+
18+ # Create subnet
19+ resource "azurerm_subnet" "myterraformsubnet" {
20+ name = " mySubnet"
21+ resource_group_name = azurerm_resource_group. rg . name
22+ virtual_network_name = azurerm_virtual_network. myterraformnetwork . name
23+ address_prefixes = [" 10.0.1.0/24" ]
24+ }
25+
26+ # Create public IPs
27+ resource "azurerm_public_ip" "myterraformpublicip" {
28+ name = " myPublicIP"
29+ location = azurerm_resource_group. rg . location
30+ resource_group_name = azurerm_resource_group. rg . name
31+ allocation_method = " Dynamic"
32+ }
33+
34+ # Create Network Security Group and rule
35+ resource "azurerm_network_security_group" "myterraformnsg" {
36+ name = " myNetworkSecurityGroup"
37+ location = azurerm_resource_group. rg . location
38+ resource_group_name = azurerm_resource_group. rg . name
39+
40+ security_rule {
41+ name = " SSH"
42+ priority = 1001
43+ direction = " Inbound"
44+ access = " Allow"
45+ protocol = " Tcp"
46+ source_port_range = " *"
47+ destination_port_range = " 22"
48+ source_address_prefix = " *"
49+ destination_address_prefix = " *"
50+ }
51+ }
52+
53+ # Create network interface
54+ resource "azurerm_network_interface" "myterraformnic" {
55+ name = " myNIC"
56+ location = azurerm_resource_group. rg . location
57+ resource_group_name = azurerm_resource_group. rg . name
58+
59+ ip_configuration {
60+ name = " myNicConfiguration"
61+ subnet_id = azurerm_subnet. myterraformsubnet . id
62+ private_ip_address_allocation = " Dynamic"
63+ public_ip_address_id = azurerm_public_ip. myterraformpublicip . id
64+ }
65+ }
66+
67+ # Connect the security group to the network interface
68+ resource "azurerm_network_interface_security_group_association" "example" {
69+ network_interface_id = azurerm_network_interface. myterraformnic . id
70+ network_security_group_id = azurerm_network_security_group. myterraformnsg . id
71+ }
72+
73+ # Generate random text for a unique storage account name
74+ resource "random_id" "randomId" {
75+ keepers = {
76+ # Generate a new ID only when a new resource group is defined
77+ resource_group = azurerm_resource_group.rg.name
78+ }
79+
80+ byte_length = 8
81+ }
82+
83+ # Create storage account for boot diagnostics
84+ resource "azurerm_storage_account" "mystorageaccount" {
85+ name = " diag${ random_id . randomId . hex } "
86+ location = azurerm_resource_group. rg . location
87+ resource_group_name = azurerm_resource_group. rg . name
88+ account_tier = " Standard"
89+ account_replication_type = " LRS"
90+ }
91+
92+ # Create (and display) an SSH key
93+ resource "tls_private_key" "example_ssh" {
94+ algorithm = " RSA"
95+ rsa_bits = 4096
96+ }
97+
98+ # Create virtual machine
99+ resource "azurerm_linux_virtual_machine" "myterraformvm" {
100+ name = " myVM"
101+ location = azurerm_resource_group. rg . location
102+ resource_group_name = azurerm_resource_group. rg . name
103+ network_interface_ids = [azurerm_network_interface . myterraformnic . id ]
104+ size = " Standard_DS1_v2"
105+
106+ os_disk {
107+ name = " myOsDisk"
108+ caching = " ReadWrite"
109+ storage_account_type = " Premium_LRS"
110+ }
111+
112+ source_image_reference {
113+ publisher = " Canonical"
114+ offer = " UbuntuServer"
115+ sku = " 18.04-LTS"
116+ version = " latest"
117+ }
118+
119+ computer_name = " myvm"
120+ admin_username = " azureuser"
121+ disable_password_authentication = true
122+
123+ admin_ssh_key {
124+ username = " azureuser"
125+ public_key = tls_private_key. example_ssh . public_key_openssh
126+ }
127+
128+ boot_diagnostics {
129+ storage_account_uri = azurerm_storage_account. mystorageaccount . primary_blob_endpoint
130+ }
131+ }
0 commit comments