/
azure_dsvm.tf
160 lines (133 loc) · 4.28 KB
/
azure_dsvm.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
variable "location" {
description = "Datacenter location to deploy the VM into"
default = "westeurope"
}
variable "vm_name" {
description = "Name of the virtual machine (acts as prefix for all generated resources)"
default = "dsvm"
}
variable "admin_user" {
description = "Admin username"
default = "root"
}
variable "admin_public_key" {
description = "Path to Public SSH key of the admin user"
default = "~/.ssh/id_rsa.pub"
}
variable "admin_private_key" {
description = "Path to Private SSH key of the admin user"
default = "~/.ssh/id_rsa"
}
variable "vm_type" {
description = "The type of VM to deploy"
default = "Standard_NC6"
}
resource "azurerm_resource_group" "ds" {
name = var.vm_name
location = var.location
}
resource "azurerm_virtual_network" "ds" {
name = "${var.vm_name}-network"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.ds.location
resource_group_name = azurerm_resource_group.ds.name
}
resource "azurerm_subnet" "ds" {
name = "${var.vm_name}-subnet"
resource_group_name = azurerm_resource_group.ds.name
virtual_network_name = azurerm_virtual_network.ds.name
address_prefix = "10.0.2.0/24"
}
resource "azurerm_network_interface" "ds" {
name = "${var.vm_name}-ni"
location = azurerm_resource_group.ds.location
resource_group_name = azurerm_resource_group.ds.name
ip_configuration {
name = "${var.vm_name}-cfg"
subnet_id = azurerm_subnet.ds.id
private_ip_address_allocation = "dynamic"
public_ip_address_id = azurerm_public_ip.ds.id
}
}
resource "azurerm_virtual_machine" "ds" {
name = "${var.vm_name}-vm"
location = azurerm_resource_group.ds.location
resource_group_name = azurerm_resource_group.ds.name
network_interface_ids = [azurerm_network_interface.ds.id]
vm_size = var.vm_type
delete_os_disk_on_termination = true
delete_data_disks_on_termination = true
plan {
name = "linuxdsvmubuntu"
publisher = "microsoft-ads"
product = "linux-data-science-vm-ubuntu"
}
storage_image_reference {
publisher = "microsoft-ads"
offer = "linux-data-science-vm-ubuntu"
sku = "linuxdsvmubuntu"
version = "latest"
}
storage_os_disk {
name = "${var.vm_name}-osdisk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
# Optional data disks
storage_data_disk {
name = "${var.vm_name}-data"
managed_disk_type = "Standard_LRS"
create_option = "FromImage"
lun = 0
disk_size_gb = "120"
}
os_profile {
computer_name = var.vm_name
admin_username = var.admin_user
}
os_profile_linux_config {
disable_password_authentication = true
ssh_keys {
path = "/home/${var.admin_user}/.ssh/authorized_keys"
key_data = file(var.admin_public_key)
}
}
tags = {
environment = "datascience-vm, ${var.vm_name}"
}
provisioner "remote-exec" {
inline = [
"mkdir -p work",
]
connection {
type = "ssh"
user = var.admin_user
private_key = file(var.admin_private_key)
host = azurerm_public_ip.ds.ip_address
}
}
}
# create a public IP to bind against the VM
resource "azurerm_public_ip" "ds" {
name = "${var.vm_name}-ip"
location = azurerm_resource_group.ds.location
resource_group_name = azurerm_resource_group.ds.name
public_ip_address_allocation = "static"
tags = {
environment = "datascience-vm, ${var.vm_name}"
}
}
# dump reference to public IP and VM ID to local files; if anything changes just re-run terraform apply to re-generate the files locally
resource "null_resource" "ds" {
triggers = {
vm_id = azurerm_virtual_machine.ds.id
ip_address = azurerm_public_ip.ds.ip_address
}
provisioner "local-exec" {
command = "echo ${azurerm_virtual_machine.ds.id} > .vm-id"
}
provisioner "local-exec" {
command = "echo ${var.admin_user}@${azurerm_public_ip.ds.ip_address} > .vm-ip"
}
}