-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
104 lines (86 loc) · 2.89 KB
/
Copy pathmain.tf
File metadata and controls
104 lines (86 loc) · 2.89 KB
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
locals {
script_vars = {
INSTANCE_CONNECTION_NAME = data.google_sql_database_instance.default.connection_name
CLOUD_SQL_PROXY_VERSION = var.cloud_sql_proxy_version
CONNECTION_MODE = var.psc_connection ? "--psc" : "--private-ip"
}
}
resource "random_id" "default" {
byte_length = 2
}
data "google_compute_zones" "available" {
region = var.region
project = var.project
}
resource "random_shuffle" "default" {
input = data.google_compute_zones.available.names
}
data "google_compute_subnetwork" "default" {
name = var.subnetwork
region = var.region
project = var.host_project != "" ? var.host_project : var.project
}
data "google_sql_database_instance" "default" {
name = var.db_name
project = var.project
}
// create service account with cloudsql.editor role
resource "google_service_account" "default" {
account_id = "cloudsqlproxy-${random_id.default.hex}"
project = var.project
display_name = "Service Account for Cloud SQL Proxy"
}
resource "google_project_iam_member" "project" {
role = "roles/cloudsql.editor"
member = google_service_account.default.member
project = var.project
}
// create compute instance that hosts the proxy
resource "google_compute_instance" "default" {
name = "cloudsqlproxy-${random_id.default.hex}"
description = "Cloud SQL Proxy - ${base64sha256(jsonencode(local.script_vars))}" # force instance replacement on var change
project = var.project
machine_type = "e2-small"
zone = random_shuffle.default.result[0]
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
}
}
network_interface {
subnetwork = data.google_compute_subnetwork.default.self_link
}
metadata_startup_script = templatefile("${path.module}/startup-script.sh", local.script_vars)
service_account {
email = google_service_account.default.email
scopes = ["cloud-platform"]
}
}
// create iam binding to allow user to create iap tunnel
resource "google_iap_tunnel_instance_iam_binding" "enable_iap" {
project = var.project
zone = resource.random_shuffle.default.result[0]
instance = resource.google_compute_instance.default.name
role = "roles/iap.tunnelResourceAccessor"
members = var.members
}
// optionally create fw
resource "google_compute_firewall" "default" {
count = var.create_firewall_rule ? 1 : 0
project = var.host_project != "" ? var.host_project : var.project
name = "cloudsqlproxy-${random_id.default.hex}"
network = var.network
allow {
protocol = "tcp"
ports = toset(concat(["22"], var.additional_ports))
}
# https://cloud.google.com/iap/docs/using-tcp-forwarding#before_you_begin
# This is the netblock needed to forward to the instances
source_ranges = ["35.235.240.0/20"]
}
// enable api
resource "google_project_service" "default" {
project = var.project
service = "iap.googleapis.com"
disable_on_destroy = false
}