|
| 1 | +resource "random_id" "suffix" { |
| 2 | + byte_length = 4 |
| 3 | +} |
| 4 | + |
| 5 | +locals { |
| 6 | + dataset_name = split("/", data.google_bigquery_dataset.dataset.id)[length(split("/", data.google_bigquery_dataset.dataset.id)) - 1] |
| 7 | + staging_bucket_name = "${var.gcp_project_id}-clickpipe-staging-${random_id.suffix.hex}" |
| 8 | + sa_name = "clickpipe-bigquery-${random_id.suffix.hex}" |
| 9 | + sa_display_name = "ClickPipe BigQuery Service Account" |
| 10 | +} |
| 11 | + |
| 12 | +// Ensures the BigQuery dataset and tables exist |
| 13 | +data "google_bigquery_dataset" "dataset" { |
| 14 | + project = var.gcp_project_id |
| 15 | + dataset_id = var.bigquery_dataset_id |
| 16 | +} |
| 17 | + |
| 18 | +data "google_bigquery_table" "table" { |
| 19 | + for_each = toset(var.bigquery_table_names) |
| 20 | + |
| 21 | + dataset_id = var.bigquery_dataset_id |
| 22 | + table_id = each.value |
| 23 | +} |
| 24 | + |
| 25 | +// This bucket is used by ClickPipe to stage data during BigQuery exports |
| 26 | +resource "google_storage_bucket" "clickpipes_staging_bucket" { |
| 27 | + name = local.staging_bucket_name |
| 28 | + location = var.gcp_region |
| 29 | + project = var.gcp_project_id |
| 30 | + force_destroy = true // do not use in production |
| 31 | + |
| 32 | + uniform_bucket_level_access = true |
| 33 | + |
| 34 | + lifecycle_rule { |
| 35 | + condition { |
| 36 | + age = 1 |
| 37 | + } |
| 38 | + action { |
| 39 | + type = "Delete" |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// Service account for ClickPipe to access BigQuery and GCS |
| 45 | +resource "google_service_account" "clickpipes" { |
| 46 | + project = var.gcp_project_id |
| 47 | + account_id = local.sa_name |
| 48 | + display_name = local.sa_display_name |
| 49 | + description = "Service account for ClickPipe to access BigQuery and GCS" |
| 50 | +} |
| 51 | + |
| 52 | +// Service account key for ClickPipe |
| 53 | +resource "google_service_account_key" "clickpipes_key" { |
| 54 | + service_account_id = google_service_account.clickpipes.name |
| 55 | + public_key_type = "TYPE_X509_PEM_FILE" |
| 56 | + private_key_type = "TYPE_GOOGLE_CREDENTIALS_FILE" |
| 57 | +} |
| 58 | + |
| 59 | +// Allows to view BigQuery datasets and tables with dataset-level condition |
| 60 | +resource "google_project_iam_member" "bigquery_data_viewer" { |
| 61 | + project = var.gcp_project_id |
| 62 | + role = "roles/bigquery.dataViewer" |
| 63 | + member = "serviceAccount:${google_service_account.clickpipes.email}" |
| 64 | + |
| 65 | + condition { |
| 66 | + title = "Restrict access to specific dataset" |
| 67 | + description = "Allow access only to the designated BigQuery dataset" |
| 68 | + expression = "resource.name.startsWith(\"projects/${var.gcp_project_id}/datasets/${local.dataset_name}\")" |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// This allows ClickPipes to run BigQuery export jobs |
| 73 | +resource "google_project_iam_member" "bigquery_job_user" { |
| 74 | + project = var.gcp_project_id |
| 75 | + role = "roles/bigquery.jobUser" |
| 76 | + member = "serviceAccount:${google_service_account.clickpipes.email}" |
| 77 | +} |
| 78 | + |
| 79 | +// GCS Object Admin role with bucket-level condition |
| 80 | +resource "google_project_iam_member" "storage_object_admin" { |
| 81 | + project = var.gcp_project_id |
| 82 | + role = "roles/storage.objectAdmin" |
| 83 | + member = "serviceAccount:${google_service_account.clickpipes.email}" |
| 84 | + |
| 85 | + condition { |
| 86 | + title = "Restrict access to staging bucket" |
| 87 | + description = "Allow access only to the ClickPipe staging bucket" |
| 88 | + expression = "resource.name.startsWith(\"projects/_/buckets/${local.staging_bucket_name}\")" |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// GCS Bucket Viewer role with bucket-level condition |
| 93 | +resource "google_project_iam_member" "storage_bucket_viewer" { |
| 94 | + project = var.gcp_project_id |
| 95 | + role = "roles/storage.bucketViewer" |
| 96 | + member = "serviceAccount:${google_service_account.clickpipes.email}" |
| 97 | + |
| 98 | + condition { |
| 99 | + title = "Restrict access to staging bucket" |
| 100 | + description = "Allow access only to the ClickPipe staging bucket" |
| 101 | + expression = "resource.name.startsWith(\"projects/_/buckets/${local.staging_bucket_name}\")" |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +output "clickpipes_bigquery_service_account_email" { |
| 106 | + value = google_service_account.clickpipes.email |
| 107 | +} |
0 commit comments