-
Notifications
You must be signed in to change notification settings - Fork 0
/
terraform_cloud.tf.example
300 lines (251 loc) · 9.44 KB
/
terraform_cloud.tf.example
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
provider "tfe" {
token = var.tfc_token
}
locals {
cicd_project_id = module.tfc_cicd.project_id
tfc_org_name = var.tfc_org_name
tfc_projects = {
"bootstrap" = {
vcs_repo = var.vcs_repos.bootstrap,
},
"org" = {
vcs_repo = var.vcs_repos.organization,
},
"env" = {
vcs_repo = var.vcs_repos.environments,
},
"net" = {
vcs_repo = var.vcs_repos.networks,
},
"proj" = {
vcs_repo = var.vcs_repos.projects,
},
}
tfc_workspaces = {
"bootstrap" = {
"0-shared" = { vcs_branch = "production", directory = "/envs/shared" }
},
"org" = {
"1-shared" = { vcs_branch = "production", directory = "/envs/shared" }
},
"env" = {
"2-production" = { vcs_branch = "production", directory = "/envs/production" },
"2-non-production" = { vcs_branch = "non-production", directory = "/envs/non-production" },
"2-development" = { vcs_branch = "development", directory = "/envs/development" },
},
"net" = {
"3-production" = { vcs_branch = "production", directory = "/envs/production" },
"3-non-production" = { vcs_branch = "non-production", directory = "/envs/non-production" },
"3-development" = { vcs_branch = "development", directory = "/envs/development" },
"3-shared" = { vcs_branch = "production", directory = "/envs/shared" },
},
"proj" = {
"4-ml-production" = { vcs_branch = "production", directory = "/ml_business_unit/production" },
"4-ml-non-production" = { vcs_branch = "non-production", directory = "/ml_business_unit/non-production" },
"4-ml-development" = { vcs_branch = "development", directory = "/ml_business_unit/development" },
"4-ml-shared" = { vcs_branch = "production", directory = "/ml_business_unit/shared" },
},
}
tfc_workspaces_flat = flatten([
for project, workspaces in local.tfc_workspaces : [
for name, config in workspaces : {
name = name
project = project
working_directory = config["directory"]
branch = config["vcs_branch"]
}
]
]
)
tfc_workspace_map = { for v in local.tfc_workspaces_flat : v.name => v }
sa_mapping = {
for k, v in local.tfc_projects : k => {
sa_name = google_service_account.terraform-env-sa[k].name
sa_email = google_service_account.terraform-env-sa[k].email
attribute = "*"
}
}
tfc_agent_pool = "${var.tfc_agent_pool_name}-${random_string.suffix.result}"
workspaces_execution_mode = var.enable_tfc_cloud_agents == true ? "agent" : "remote"
workspaces_agent_pool_id = var.enable_tfc_cloud_agents == true ? tfe_agent_pool.tfc_agent_pool[0].id : ""
}
resource "tfe_project" "main" {
for_each = local.tfc_projects
organization = local.tfc_org_name
name = each.key
}
resource "tfe_workspace" "main" {
for_each = local.tfc_workspace_map
organization = local.tfc_org_name
name = each.value.name
working_directory = each.value.working_directory
project_id = tfe_project.main[each.value.project].id
global_remote_state = true
terraform_version = var.tfc_terraform_version
agent_pool_id = local.workspaces_agent_pool_id
execution_mode = local.workspaces_execution_mode
vcs_repo {
identifier = local.tfc_projects[each.value.project].vcs_repo
branch = each.value.branch
oauth_token_id = var.vcs_oauth_token_id
}
}
data "tfe_workspace" "main" {
for_each = local.tfc_workspace_map
name = each.value.name
organization = local.tfc_org_name
depends_on = [tfe_workspace.main]
}
resource "tfe_variable_set" "main" {
name = "Terraform Cloud Keys Varset"
description = "A resource that allows you to reuse the same variables across multiple workspaces"
global = false
organization = local.tfc_org_name
}
resource "tfe_variable" "tfc_oauth_id_vcs" {
key = "vcs_oauth_token_id"
value = var.vcs_oauth_token_id
category = "terraform"
description = "The VCS Connection OAuth Connection Token ID"
workspace_id = tfe_workspace.main["0-shared"].id
sensitive = true
}
resource "tfe_variable" "tfc_token" {
key = "tfc_token"
value = var.tfc_token
category = "terraform"
description = "The token used to authenticate with Terraform Cloud"
workspace_id = tfe_workspace.main["0-shared"].id
sensitive = true
}
resource "tfe_variable" "tfc_terraform_version" {
key = "tfc_terraform_version"
value = var.tfc_terraform_version
category = "terraform"
description = "TF version desired for TFC workspaces"
workspace_id = tfe_workspace.main["0-shared"].id
}
resource "tfe_variable" "tfc_organization_name" {
key = "tfc_org_name"
value = var.tfc_org_name
category = "terraform"
description = "Name of the TFC organization"
variable_set_id = tfe_variable_set.main.id
}
resource "tfe_variable" "gcp_provider_auth" {
key = "TFC_GCP_PROVIDER_AUTH"
value = "true"
category = "env"
description = "If true TFC will attempt to use dynamic credentials to authenticate to GCP"
variable_set_id = tfe_variable_set.main.id
}
resource "tfe_variable" "gcp_workload_provider_name" {
key = "TFC_GCP_WORKLOAD_PROVIDER_NAME"
value = module.tfc-oidc.provider_name
category = "env"
description = "The canonical name of the workload identity provider."
variable_set_id = tfe_variable_set.main.id
}
resource "tfe_variable" "service_account_email" {
for_each = local.tfc_workspace_map
key = "TFC_GCP_RUN_SERVICE_ACCOUNT_EMAIL"
value = google_service_account.terraform-env-sa[each.value.project].email
category = "env"
description = "The service account email to use for the plan phase of a run."
workspace_id = tfe_workspace.main[each.key].id
}
resource "tfe_workspace_variable_set" "main" {
for_each = local.tfc_workspace_map
variable_set_id = tfe_variable_set.main.id
workspace_id = tfe_workspace.main[each.key].id
}
resource "tfe_run_trigger" "networks_shared_production" {
workspace_id = tfe_workspace.main["3-production"].id
sourceable_id = tfe_workspace.main["3-shared"].id
}
resource "tfe_run_trigger" "projects_ml_shared_production" {
workspace_id = tfe_workspace.main["4-ml-production"].id
sourceable_id = tfe_workspace.main["4-ml-shared"].id
}
module "tfc_cicd" {
source = "terraform-google-modules/project-factory/google"
version = "~> 12.0"
name = "${var.project_prefix}-b-cicd-wif-tfc"
random_project_id = true
org_id = var.org_id
folder_id = google_folder.bootstrap.id
billing_account = var.billing_account
activate_apis = [
"compute.googleapis.com",
"admin.googleapis.com",
"iam.googleapis.com",
"billingbudgets.googleapis.com",
"cloudbilling.googleapis.com",
"serviceusage.googleapis.com",
"cloudresourcemanager.googleapis.com",
"iamcredentials.googleapis.com",
"container.googleapis.com",
"gkeconnect.googleapis.com",
"gkehub.googleapis.com",
"connectgateway.googleapis.com"
]
}
module "tfc-oidc" {
source = "GoogleCloudPlatform/tf-cloud-agents/google//modules/tfc-oidc"
version = "0.1.0"
project_id = module.tfc_cicd.project_id
pool_id = "foundation-pool"
provider_id = "foundation-tfc-provider"
sa_mapping = local.sa_mapping
tfc_organization_name = local.tfc_org_name
attribute_condition = "assertion.sub.startsWith(\"organization:${local.tfc_org_name}:\")"
}
data "google_client_config" "default" {
}
resource "random_string" "suffix" {
length = 4
special = false
upper = false
}
# Create a new agent pool in organization
resource "tfe_agent_pool" "tfc_agent_pool" {
count = var.enable_tfc_cloud_agents == true ? 1 : 0
name = local.tfc_agent_pool
organization = local.tfc_org_name
}
# Create a new token for the agent pool
resource "tfe_agent_token" "tfc_agent_token" {
count = var.enable_tfc_cloud_agents == true ? 1 : 0
agent_pool_id = tfe_agent_pool.tfc_agent_pool.id
description = var.tfc_agent_pool_token_description
}
# Create the infrastructure for the agent to run
module "tfc_agent_gke" {
source = "./modules/tfc-agent-gke"
count = var.enable_tfc_cloud_agents == true ? 1 : 0
project_id = module.tfc_cicd.project_id
project_number = module.tfc_cicd.project_number
tfc_agent_token = tfe_agent_token.tfc_agent_token[0].token
create_service_account = false
service_account_email = google_service_account.terraform-env-sa["bootstrap"].email
service_account_id = google_service_account.terraform-env-sa["bootstrap"].id
//If you are using Terraform Cloud Agents, un-comment this block after the first apply according README instructions
# providers = {
# kubernetes = kubernetes
# }
}