-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute.tf
135 lines (115 loc) · 4 KB
/
compute.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
// Create ssh keys for compute resources
resource "tls_private_key" "ssh" {
algorithm = "RSA"
rsa_bits = "2048"
}
resource "local_file" "private_key" {
content = tls_private_key.ssh.private_key_pem
filename = "pt_key.pem"
file_permission = "0600"
}
data "yandex_compute_image" "ubuntu_image" {
family = "ubuntu-2204-lts"
}
data "yandex_compute_image" "nginx_image" {
family = "lemp"
}
// Create Jump VM
resource "yandex_compute_instance" "jump-vm" {
folder_id = yandex_resourcemanager_folder.folder[0].id
name = "jump-vm"
hostname = "jump-vm"
platform_id = "standard-v3"
zone = var.az_name
resources {
cores = 2
memory = 2
}
boot_disk {
initialize_params {
image_id = data.yandex_compute_image.ubuntu_image.id
type = "network-hdd"
size = 10
}
}
network_interface {
subnet_id = yandex_vpc_subnet.subnet[0].id
ip_address = "${cidrhost(var.subnet_prefix_list[0], 101)}"
nat = true
nat_ip_address = yandex_vpc_address.public-ip-jump-vm.external_ipv4_address.0.address
security_group_ids = [yandex_vpc_security_group.mgmt-jump-vm-sg.id]
}
metadata = {
user-data = templatefile("./templates/cloud-init_jump-vm.tpl.yaml",
{
jump_vm_ssh_key_pub = "${chomp(tls_private_key.ssh.public_key_openssh)}",
jump_vm_admin_username = var.jump_vm_admin_username,
wg_port = var.wg_port,
wg_client_dns = "${cidrhost(var.subnet_prefix_list[0], 2)}",
wg_public_ip = "${yandex_vpc_address.public-ip-jump-vm.external_ipv4_address.0.address}",
wg_allowed_ip = "${join(",", var.subnet_prefix_list)}"
})
}
}
// Wait for SSH connection to Jump VM
resource "null_resource" "wait_for_ssh_jump_vm" {
connection {
type = "ssh"
user = "${var.jump_vm_admin_username}"
private_key = local_file.private_key.content
host = yandex_vpc_address.public-ip-jump-vm.external_ipv4_address.0.address
}
// Wait for WireGuard client config to be updated with keys in cloud-init process
provisioner "remote-exec" {
inline = [
"while [ ! -f ~/jump-vm-wg.conf ]; do sleep 5; echo \"Waiting for jump-vm-wg.conf to be created...\"; done",
"while grep -q CLIENT_PSK ~/jump-vm-wg.conf; do sleep 5; echo \"Waiting for jump-vm-wg.conf to be updated with keys...\"; done"
]
}
depends_on = [
yandex_compute_instance.jump-vm,
local_file.private_key,
yandex_vpc_address.public-ip-jump-vm
]
}
// Download WireGuard client config from Jump VM
resource "null_resource" "get_wg_client_config" {
provisioner "local-exec" {
command = "scp -i pt_key.pem -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ${var.jump_vm_admin_username}@${yandex_vpc_address.public-ip-jump-vm.external_ipv4_address.0.address}:jump-vm-wg.conf jump-vm-wg.conf"
}
depends_on = [
null_resource.wait_for_ssh_jump_vm
]
}
// Create web-server in dmz segment
resource "yandex_compute_instance" "dmz-web-server" {
folder_id = yandex_resourcemanager_folder.folder[2].id
name = "dmz-web-server"
hostname = "dmz-web-server"
platform_id = "standard-v3"
zone = var.az_name
resources {
cores = 2
memory = 2
}
boot_disk {
initialize_params {
image_id = data.yandex_compute_image.nginx_image.id
type = "network-hdd"
size = 10
}
}
network_interface {
subnet_id = yandex_vpc_subnet.subnet[2].id
ip_address = "${cidrhost(var.subnet_prefix_list[2], 100)}"
nat = false
security_group_ids = [yandex_vpc_security_group.dmz-web-sg.id, yandex_vpc_security_group.segment-sg[0].id]
}
metadata = {
user-data = templatefile("./templates/cloud-init_dmz-web-server.tpl.yaml",
{
ssh_key_pub = "${chomp(tls_private_key.ssh.public_key_openssh)}",
nginx_port = var.internal_app_port,
})
}
}