-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
179 lines (136 loc) · 5.65 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
terraform {
# Requiring Providers
# https://www.terraform.io/language/providers/requirements#requiring-providers
required_providers {
# Datadog Provider
# https://registry.terraform.io/providers/DataDog/datadog/latest/docs
datadog = {
source = "datadog/datadog"
}
# Google Cloud Platform Provider
# https://registry.terraform.io/providers/hashicorp/google/latest/docs
google = {
source = "hashicorp/google"
}
# Random Provider
# https://www.terraform.io/docs/providers/random/index.html
random = {
source = "hashicorp/random"
}
}
}
provider "datadog" {
api_key = var.datadog_api_key
app_key = var.datadog_app_key
}
# The google_cloud_identity_group resource requires this if you are using User ADCs (Application Default Credentials).
# Your account must have the serviceusage.services.use permission on the billing_project you defined.
# The following APIs must be enabled on the billing_project:
# - cloudresourcemanager.googleapis.com
# - cloudidentity.googleapis.com
# This is only needed during bootstrapping.
# provider "google" {
# billing_project = var.billing_project
# user_project_override = true
# }
# Datadog Google Cloud Platform Integration Module (osinfra.io)
# https://github.com/osinfra-io/terraform-datadog-google-integration
module "datadog" {
source = "github.com/osinfra-io/terraform-datadog-google-integration?ref=v0.3.0"
count = var.datadog_enable ? 1 : 0
api_key = var.datadog_api_key
is_cspm_enabled = true
is_security_command_center_enabled = true
labels = module.helpers.labels
project = module.project.id
}
# Google Project Module (osinfra.io)
# https://github.com/osinfra-io/terraform-google-project
module "project" {
source = "github.com/osinfra-io/terraform-google-project?ref=v0.4.5"
billing_account = var.project_billing_account
cis_2_2_logging_sink_project_id = var.project_cis_2_2_logging_sink_project_id
description = "terraform"
folder_id = var.project_folder_id
labels = module.helpers.labels
monthly_budget_amount = var.project_monthly_budget_amount
prefix = "plt-lz"
services = [
"billingbudgets.googleapis.com",
"cloudasset.googleapis.com",
"cloudbilling.googleapis.com",
"cloudidentity.googleapis.com",
"cloudkms.googleapis.com",
"cloudresourcemanager.googleapis.com",
"compute.googleapis.com",
"container.googleapis.com",
"iam.googleapis.com",
"iamcredentials.googleapis.com",
"monitoring.googleapis.com",
"pubsub.googleapis.com",
"servicenetworking.googleapis.com",
"serviceusage.googleapis.com",
"sqladmin.googleapis.com"
]
}
# Google Storage Bucket Module (osinfra.io)
# https://github.com/osinfra-io/terraform-google-storage-bucket
module "terraform_state_storage_bucket" {
source = "github.com/osinfra-io/terraform-google-storage-bucket?ref=v0.2.0"
for_each = local.service_accounts
labels = module.helpers.labels
location = "us"
name = "${each.key}-${random_id.bucket.hex}-${module.helpers.env}"
project = module.project.id
}
# Google Identity Group Membership
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloud_identity_group_membership
resource "google_cloud_identity_group_membership" "github_actions" {
for_each = local.service_accounts
# Use the following gcloud command to figure out the group_id
# gcloud identity groups search --organization=osinfra.io --labels="cloudidentity.googleapis.com/groups.discussion_forum"
# This should be the group_id for the gcp-billing-users group created in the google-cloud-hierarchy repository.
group = "groups/${var.billing_users_group_id}"
preferred_member_key {
id = google_service_account.github_actions[each.key].email
}
roles { name = "MEMBER" }
dynamic "roles" {
for_each = each.key == "plt-lz-backend" ? [1] : []
content {
name = "MANAGER"
}
}
depends_on = [
module.project
]
}
# Google Service Account Resource
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/google_service_account
resource "google_service_account" "github_actions" {
for_each = local.service_accounts
account_id = "${each.key}-github"
display_name = "Service account for GitHub Actions"
project = module.project.id
}
# Google Service Account IAM Member Resource
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/google_service_account_iam#google_service_account_iam_member
resource "google_service_account_iam_member" "github_actions" {
for_each = local.github_repositories
member = "principalSet://iam.googleapis.com/${var.workload_identity_pool_name}/attribute.repository/osinfra-io/${each.value.repository}"
role = "roles/iam.workloadIdentityUser"
service_account_id = google_service_account.github_actions[each.value.name].id
}
# Google Storage Bucket IAM Member
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/storage_bucket_iam#google_storage_bucket_iam_member
resource "google_storage_bucket_iam_member" "github_actions" {
for_each = local.service_accounts
bucket = module.terraform_state_storage_bucket[each.key].name
member = "serviceAccount:${google_service_account.github_actions[each.key].email}"
role = "roles/storage.objectAdmin"
}
# Random Random ID Resource
# https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/id
resource "random_id" "bucket" {
byte_length = 2
}