-
Notifications
You must be signed in to change notification settings - Fork 50
/
main.tf
152 lines (128 loc) · 3.91 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
terraform {
required_providers {
tailscale = {
source = "tailscale/tailscale"
version = ">=0.13.5"
}
}
}
provider "azurerm" {
features {
resource_group {
prevent_deletion_if_contains_resources = false
}
virtual_machine {
delete_os_disk_on_deletion = true
graceful_shutdown = false
skip_shutdown_and_force_delete = true
}
}
}
provider "tailscale" {
tailnet = var.tailnet_name
api_key = var.tailscale_api_key
}
resource "random_pet" "ts" {
length = 2
separator = ""
}
resource "azurerm_resource_group" "ts" {
name = "rg-${random_pet.ts.id}"
location = var.location
}
resource "azurerm_virtual_network" "ts" {
name = "vnet-${random_pet.ts.id}"
address_space = [var.vnet_address_space]
location = azurerm_resource_group.ts.location
resource_group_name = azurerm_resource_group.ts.name
}
resource "azurerm_subnet" "ts" {
name = "snet-${random_pet.ts.id}"
resource_group_name = azurerm_resource_group.ts.name
virtual_network_name = azurerm_virtual_network.ts.name
address_prefixes = [var.snet_address_space]
}
resource "azurerm_network_security_group" "ts" {
name = "nsg-${random_pet.ts.id}"
location = azurerm_resource_group.ts.location
resource_group_name = azurerm_resource_group.ts.name
security_rule {
name = "AllowTailscaleInbound"
priority = 150
direction = "Inbound"
access = "Allow"
protocol = "Udp"
source_port_range = "*"
destination_port_range = "41641"
source_address_prefix = "Internet"
destination_address_prefix = "*"
}
}
resource "azurerm_subnet_network_security_group_association" "ts" {
subnet_id = azurerm_subnet.ts.id
network_security_group_id = azurerm_network_security_group.ts.id
}
resource "tls_private_key" "ts" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "azurerm_ssh_public_key" "ts" {
name = "ssh-${random_pet.ts.id}"
resource_group_name = azurerm_resource_group.ts.name
location = azurerm_resource_group.ts.location
public_key = tls_private_key.ts.public_key_openssh
}
resource "tailscale_tailnet_key" "ts" {
reusable = false
ephemeral = true
preauthorized = true
}
data "cloudinit_config" "ts" {
base64_encode = true
gzip = true
part {
content_type = "text/cloud-config"
content = file("./tailscale.yml")
}
part {
content_type = "text/x-shellscript"
content = templatefile("./tailscale.sh", {
tailscale_auth_key = tailscale_tailnet_key.ts.key
})
}
}
resource "azurerm_network_interface" "ts" {
name = "${random_pet.ts.id}-nic"
location = azurerm_resource_group.ts.location
resource_group_name = azurerm_resource_group.ts.name
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.ts.id
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_linux_virtual_machine" "ts" {
name = random_pet.ts.id
resource_group_name = azurerm_resource_group.ts.name
location = azurerm_resource_group.ts.location
size = var.vm_sku
admin_username = var.vm_username
network_interface_ids = [
azurerm_network_interface.ts.id,
]
admin_ssh_key {
username = var.vm_username
public_key = tls_private_key.ts.public_key_openssh
}
os_disk {
caching = "ReadWrite"
storage_account_type = var.vm_os_disk_storage_type
}
source_image_reference {
publisher = var.vm_source_image.publisher
offer = var.vm_source_image.offer
sku = var.vm_source_image.sku
version = var.vm_source_image.version
}
custom_data = data.cloudinit_config.ts.rendered
}