-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.tf
139 lines (118 loc) · 3.38 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
# Descricao
# Boa parte do codigo que esta aqui e "boilerplate"
# em outras palavras, codigo pronto que esta na documentacao,
# apenas parametrizado (variables\outputs).
# Rede (VPC + Subnet)
module "vpc" {
source = "terraform-google-modules/network/google"
version = "3.2.2"
project_id = "rubeus-proxysql"
network_name = "ha-final"
subnets = [
{
subnet_name = "subnet-us"
subnet_ip = "10.10.10.0/24"
subnet_region = "us-central1"
},
{
subnet_name = "subnet-asia"
subnet_ip = "10.10.20.0/24"
subnet_region = "asia-east1"
}
]
}
# Firewall
resource "google_compute_firewall" "fw-dev" {
name = "fw-dev"
network = module.vpc.network_self_link
allow {
protocol = "tcp"
ports = ["22"]
}
source_ranges = ["0.0.0.0/0"]
}
resource "google_compute_firewall" "fw-lb" {
name = "fw-lb"
network = module.vpc.network_self_link
allow {
protocol = "tcp"
ports = ["80", "443"]
}
source_ranges = ["130.211.0.0/22", "35.191.0.0/16"]
}
# Infos da subnet
# Preciso registrar o self_link das subnets corretamente, como estou usando modulo
# estou buscando por um datasource.
data "google_compute_subnetwork" "subnet-us" {
name = "subnet-us"
region = "us-central1"
depends_on = [module.vpc]
}
data "google_compute_subnetwork" "subnet-asia" {
name = "subnet-asia"
region = "asia-east1"
depends_on = [module.vpc]
}
# Templates
module "instance-template-us" {
source = "./mod-template-mig"
name = "us"
metadata_startup_script = "./scripts/us.sh"
network = module.vpc.network_self_link
subnetwork = data.google_compute_subnetwork.subnet-us.self_link
}
module "instance-template-asia" {
source = "./mod-template-mig"
name = "asia"
metadata_startup_script = "./scripts/asia.sh"
network = module.vpc.network_self_link
subnetwork = data.google_compute_subnetwork.subnet-asia.self_link
}
# Instance Groups
module "mig-us" {
source = "./mod-mig"
name = "mig-us"
base_instance_name = "us-web"
region = "us-central1"
instance_template = module.instance-template-us.self_link
distribution_policy_zones = ["us-central1-a", "us-central1-b"]
resource_depends_on = [module.vpc]
}
module "mig-asia" {
source = "./mod-mig"
name = "mig-asia"
base_instance_name = "asia-web"
region = "asia-east1"
instance_template = module.instance-template-asia.self_link
distribution_policy_zones = ["asia-east1-a"]
resource_depends_on = [module.vpc]
}
# Loadbalance
module "mod-lb" {
source = "./mod-lb"
backends = [
module.mig-us.instance_group,
module.mig-asia.instance_group
]
}
output "lb-ip" {
value = module.mod-lb.ip
}
# Demo count + if
resource "google_compute_instance" "bastion" {
count = terraform.workspace == "prd" ? 3 : 2
name = format("%s-%s", "bastion", count.index)
machine_type = "e2-medium"
zone = "us-central1-a"
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
subnetwork = data.google_compute_subnetwork.subnet-us.self_link
access_config {
// Ephemeral IP
}
}
}