-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
176 lines (149 loc) · 3.88 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
173
174
175
176
provider "google" {}
variable "project_id" {
type = string
}
data "google_project" "wireguard" {
project_id = var.project_id
}
variable "subnetwork" {
type = string
}
data "google_compute_subnetwork" "wireguard" {
self_link = var.subnetwork
}
variable "zone" {
type = string
default = ""
}
data "google_compute_zones" "zones" {
project = var.project_id
region = data.google_compute_subnetwork.wireguard.region
status = "UP"
}
resource "random_shuffle" "zone" {
input = data.google_compute_zones.zones.names
}
locals {
zone = var.zone != "" ? var.zone : random_shuffle.zone.result[0]
}
variable "cidr" {
type = string
default = "192.168.78.0/24"
}
variable "source_ranges" {
type = list(string)
description = "Range of IPs allowed through the GCP firewall"
}
locals {
cidr_prefix = split("/", var.cidr)[1]
gateway = "${cidrhost(var.cidr, 1)}/${local.cidr_prefix}"
}
variable "peers" {
type = list(string)
}
variable "listen_port" {
type = number
default = 51820
}
resource "google_compute_firewall" "wireguard" {
project = data.google_project.wireguard.project_id
name = "wireguard"
network = data.google_compute_subnetwork.wireguard.network
direction = "INGRESS"
allow {
protocol = "udp"
ports = [var.listen_port]
}
source_ranges = var.source_ranges
target_tags = ["wg"]
}
resource "google_secret_manager_secret" "wireguard-pubkey" {
secret_id = "wireguard-pubkey"
labels = {}
project = data.google_project.wireguard.project_id
replication {
automatic = "true"
}
}
locals {
runcmds = "- [gcloud, secrets, versions, add, ${google_secret_manager_secret.wireguard-pubkey.secret_id}, --data-file=/etc/wireguard/pubkey]"
cloud_init = templatefile("${path.module}/cloud-init.yaml", {
peers = var.peers,
listen_port = var.listen_port,
runcmds = local.runcmds,
cidr = var.cidr,
gateway = local.gateway,
})
}
resource "google_compute_instance" "wireguard" {
project = data.google_project.wireguard.project_id
name = "wireguard"
zone = local.zone
tags = ["wg"]
machine_type = "e2-micro"
labels = {}
resource_policies = []
network_interface {
subnetwork = data.google_compute_subnetwork.wireguard.self_link
access_config {
network_tier = "STANDARD"
}
}
boot_disk {
initialize_params {
image = "ubuntu-os-cloud/ubuntu-minimal-2004-lts"
}
}
metadata = {
user-data = local.cloud_init
}
metadata_startup_script = <<EOF
exit 0
#${sha256(local.cloud_init)}
EOF
service_account {
scopes = ["cloud-platform"]
}
provisioner "local-exec" {
command = <<EOF
bash -c "while gcloud --project ${self.project} secrets versions list wireguard-pubkey --limit 1 --format json | jq --exit-status 'length == 0 or (.[0].state == \"DISABLED\")'>/dev/null; do
echo 'Waiting for pubkey secret...' && sleep 10
done"
EOF
}
provisioner "local-exec" {
when = destroy
command = <<EOF
bash -c "gcloud --project ${self.project} secrets versions disable --secret wireguard-pubkey \
$(gcloud --project ${self.project} secrets versions list --format json wireguard-pubkey --limit 1 | jq -r '.[0].name')
"
EOF
}
}
data "google_secret_manager_secret_version" "pubkey" {
depends_on = [
google_compute_instance.wireguard,
]
project = data.google_project.wireguard.project_id
secret = google_secret_manager_secret.wireguard-pubkey.secret_id
}
locals {
endpoint_ip = google_compute_instance.wireguard.network_interface[0].access_config[0].nat_ip
public_key = data.google_secret_manager_secret_version.pubkey.secret_data
interface_extra = <<EOF
MTU = 1380
EOF
}
output "ip" {
value = trimspace(local.endpoint_ip)
}
output "public_key" {
sensitive = true
value = trimspace(local.public_key)
}
output "interface_extra" {
value = trimspace(local.interface_extra)
}
output "port" {
value = var.listen_port
}