-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
172 lines (144 loc) · 4.99 KB
/
main.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
160
161
162
163
164
165
166
167
168
169
170
171
172
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>2.0"
}
}
}
resource "random_id" "suffix" {
byte_length = 2
}
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "jupyterhub" {
name = var.name_rg
location = var.location
}
resource "azurerm_virtual_network" "jupyterhub" {
name = "${var.name}_vnet"
depends_on = [azurerm_resource_group.jupyterhub]
location = var.location
resource_group_name = var.name_rg
address_space = ["10.0.0.0/16"]
}
resource "azurerm_subnet" "jupyterhub" {
name = "${var.name}_subnet"
depends_on = [azurerm_virtual_network.jupyterhub]
resource_group_name = var.name_rg
address_prefixes = ["10.0.2.0/24"]
virtual_network_name = azurerm_virtual_network.jupyterhub.name
}
resource "azurerm_public_ip" "jupyterhub" {
name = "${var.name}_public_ip"
depends_on = [azurerm_resource_group.jupyterhub]
location = var.location
resource_group_name = var.name_rg
allocation_method = "Dynamic"
}
// Get public ip of the current device
data "http" "personal_ip" {
url = "https://ifconfig.co/json"
request_headers = {
Accept = "application/json"
}
}
locals {
depends_on = [data.http.personal_ip]
personal_ip = jsondecode(data.http.personal_ip.body)
}
resource "azurerm_network_security_group" "jupyterhub" {
name = "${var.name}_nsg"
depends_on = [azurerm_resource_group.jupyterhub, local.personal_ip]
location = var.location
resource_group_name = var.name_rg
security_rule {
name = "SSH"
priority = 1001
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = "${local.personal_ip.ip}/32"
destination_address_prefix = "*"
}
security_rule {
name = "Browser"
priority = 1002
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "80"
source_address_prefixes = compact(concat(["${local.personal_ip.ip}/32"], var.whitelisted_ip_addresses))
destination_address_prefix = "*"
}
}
// Create NIC to connect VM to IP, subnet and NSG
resource "azurerm_network_interface" "jupyterhub" {
name = "${var.name}_nic"
depends_on = [azurerm_resource_group.jupyterhub]
location = var.location
resource_group_name = var.name_rg
ip_configuration {
name = "${var.name}_nic_configuration"
subnet_id = azurerm_subnet.jupyterhub.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.jupyterhub.id
}
}
resource "azurerm_network_interface_security_group_association" "jupyterhub" {
network_interface_id = azurerm_network_interface.jupyterhub.id
network_security_group_id = azurerm_network_security_group.jupyterhub.id
}
resource "azurerm_linux_virtual_machine" "jupyterhub" {
name = "${var.name}-${random_id.suffix.hex}"
depends_on = [azurerm_resource_group.jupyterhub, azurerm_network_interface.jupyterhub]
location = var.location
resource_group_name = var.name_rg
network_interface_ids = [azurerm_network_interface.jupyterhub.id]
size = var.instance_size
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
admin_username = var.default_user
disable_password_authentication = true
admin_ssh_key {
username = var.default_user
public_key = file("${var.ssh_key_location}.pub")
}
tags = {
"${var.default_user}" = var.name
}
}
// Retrieve Azure VM with a data object, the resource does not contain the value
data "azurerm_public_ip" "ip_ref" {
name = "${var.name}_public_ip"
depends_on = [azurerm_public_ip.jupyterhub, azurerm_linux_virtual_machine.jupyterhub]
resource_group_name = var.name_rg
}
// Configure VMs with Ansible.
// First to a remote-exec so that we are sure the VM is configured once we run Ansible
resource "null_resource" "ansible" {
depends_on = [data.azurerm_public_ip.ip_ref]
provisioner "remote-exec" {
inline = ["echo VM is configured"]
connection {
user = "ansible"
private_key = file(var.ssh_key_location)
host = data.azurerm_public_ip.ip_ref.ip_address
}
}
provisioner "local-exec" {
command = "ansible-playbook -i '${data.azurerm_public_ip.ip_ref.ip_address},' --skip-tags add_user ../../playbooks/jupyterhub/playbook.yaml"
}
}