-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
211 lines (174 loc) · 5.06 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
variable "gcp_project_id" {
type = string
description = "The GCP project id (note: note project name)"
}
variable "gcp_region" {
type = string
description = "The GCP region to use for resources eg europe-west1"
}
variable "gcp_zone" {
type = string
description = "The GCP zone to use for resources eg europe-west1-b"
}
variable "gke_regional_cluster" {
type = bool
description = "Create a regional cluster or else a zonal cluster will be created"
}
variable "gke_instance_size" {
type = string
description = "The size of the instances to use in the GKE cluster eg n1-standard-1"
}
variable "gke_preemptible_nodes" {
type = bool
description = "Use preemptiable GKE nodes"
}
variable "gke_node_count" {
type = number
description = "Use number of nodes to create in the node pool"
}
variable "flux_giturl" {
type = string
description = "git repo containing the kuberntes manifests consumed by flux"
}
variable "flux_path" {
type = string
description = "Path within the git repo for the kubernetes manifests"
}
terraform {
backend "gcs" {
bucket = "ao-dev-ra-snowplow-infra"
prefix = "terraform"
}
required_providers {
helm = {
source = "hashicorp/helm"
version = "1.2.4"
}
}
}
provider "google" {
project = var.gcp_project_id
region = var.gcp_region
zone = var.gcp_zone
}
data "google_client_config" "provider" {}
# GKE Cluster
## Cluster
resource "google_container_cluster" "snowplow_gke" {
name = "snowplow-gke"
location = var.gke_regional_cluster ? var.gcp_region : var.gcp_zone
remove_default_node_pool = true
initial_node_count = 1
network = "default"
subnetwork = "default"
ip_allocation_policy {
cluster_ipv4_cidr_block = "/16"
services_ipv4_cidr_block = "/22"
}
}
## Node Pool
resource "google_container_node_pool" "snowplow_gke_nodes" {
name = "snowplow-gke-nodes"
location = var.gke_regional_cluster ? var.gcp_region : var.gcp_zone
cluster = google_container_cluster.snowplow_gke.name
node_count = var.gke_node_count
node_config {
preemptible = var.gke_preemptible_nodes
machine_type = var.gke_instance_size
oauth_scopes = [
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/compute",
"https://www.googleapis.com/auth/logging.write",
"https://www.googleapis.com/auth/monitoring",
"https://www.googleapis.com/auth/pubsub",
"https://www.googleapis.com/auth/bigquery",
"https://www.googleapis.com/auth/devstorage.read_only"
]
}
}
# PubSub topics
resource "google_pubsub_topic" "collector_good" {
name = "collector-good"
}
resource "google_pubsub_topic" "collector_bad" {
name = "collector-bad"
}
resource "google_pubsub_subscription" "collector_enricher_sub" {
name = "collector-enricher-sub"
topic = google_pubsub_topic.collector_good.name
}
resource "google_pubsub_topic" "enriched_good" {
name = "enriched-good"
}
resource "google_pubsub_topic" "enriched_bad" {
name = "enriched-bad"
}
resource "google_pubsub_subscription" "enricher_loader_sub" {
name = "enricher-loader-sub"
topic = google_pubsub_topic.enriched_good.name
}
resource "google_pubsub_topic" "loader_bad_rows" {
name = "loader-bad-rows"
}
resource "google_pubsub_topic" "loader_bad_inserts" {
name = "loader-bad-inserts"
}
resource "google_pubsub_topic" "loader_bq_types" {
name = "loader-bq-types"
}
resource "google_pubsub_subscription" "loader_bq_types_sub" {
name = "loader-bq-types-sub"
topic = google_pubsub_topic.loader_bq_types.name
}
# BigQuery
## Create the snowplow dataset in BQ which the loader will load into
resource "google_bigquery_dataset" "dataset" {
dataset_id = "snowplow"
friendly_name = "snowplow"
location = "EU"
delete_contents_on_destroy = false #Safety-net
}
# Create an IP which will be assigned to the load balancer by the ingress in GKE
resource "google_compute_global_address" "ip" {
name = "collector-ingress-ip-static"
}
# Setup Flux
## Conenct to the newly created GKE cluster
provider "kubernetes" {
load_config_file = false
host = "https://${google_container_cluster.snowplow_gke.endpoint}"
token = data.google_client_config.provider.access_token
cluster_ca_certificate = base64decode(
google_container_cluster.snowplow_gke.master_auth[0].cluster_ca_certificate,
)
}
## Setup Flux
resource "kubernetes_namespace" "flux_ns" {
metadata {
name = "flux"
}
}
resource "helm_release" "helm-flux" {
name = "flux"
chart = "flux"
repository = "https://charts.fluxcd.io"
namespace = kubernetes_namespace.flux_ns.metadata[0].name
set {
name = "registry.disableScanning"
value = true
}
set {
name = "syncGarbageCollection.enabled"
value = true
}
set {
name = "git.url"
type = "string"
value = var.flux_giturl
}
set {
name = "git.path"
type = "string"
value = var.flux_path
}
}