|
1 | | -# Configure the Google Cloud provider |
2 | | -provider "google" { |
3 | | - project = var.project_id |
4 | | - region = var.region |
5 | | -} |
6 | | - |
7 | | -# Enable required APIs |
8 | | -resource "google_project_service" "app_engine" { |
9 | | - service = "appengine.googleapis.com" |
10 | | -} |
11 | | - |
12 | | -resource "google_project_service" "secrets_manager" { |
13 | | - project = var.project_id |
14 | | - service = "secretmanager.googleapis.com" |
15 | | -} |
16 | | - |
17 | | -resource "google_project_service" "pub_sub" { |
18 | | - project = var.project_id |
19 | | - service = "pubsub.googleapis.com" |
20 | | -} |
21 | | - |
22 | | -# Create app engine service account |
23 | 1 | resource "google_service_account" "service_account" { |
24 | 2 | account_id = "jb-sw-realms" |
25 | 3 | display_name = "Juicebox Software Realms" |
26 | 4 | } |
27 | | - |
28 | | -# Create each tenant secret |
29 | | -resource "google_secret_manager_secret" "secret" { |
30 | | - for_each = var.tenant_secrets |
31 | | - project = var.project_id |
32 | | - secret_id = "jb-sw-tenant-${each.key}" |
33 | | - replication { |
34 | | - automatic = true |
35 | | - } |
36 | | -} |
37 | | - |
38 | | -# Add the secret data for each tenant secret |
39 | | -resource "google_secret_manager_secret_version" "secret" { |
40 | | - for_each = var.tenant_secrets |
41 | | - secret = google_secret_manager_secret.secret[each.key].id |
42 | | - secret_data = each.value |
43 | | -} |
44 | | - |
45 | | -# Grant access to the app engine for each tenant secret |
46 | | -resource "google_secret_manager_secret_iam_binding" "access" { |
47 | | - for_each = var.tenant_secrets |
48 | | - project = var.project_id |
49 | | - secret_id = google_secret_manager_secret.secret[each.key].id |
50 | | - role = "roles/secretmanager.secretAccessor" |
51 | | - |
52 | | - members = [ |
53 | | - "serviceAccount:${google_service_account.service_account.email}" |
54 | | - ] |
55 | | -} |
56 | | - |
57 | | -# Create Bigtable instance |
58 | | -resource "google_bigtable_instance" "instance" { |
59 | | - project = var.project_id |
60 | | - name = "jb-sw-realms" |
61 | | - display_name = "Juicebox Software Realms" |
62 | | - |
63 | | - cluster { |
64 | | - cluster_id = "jb-sw-realms-cluster" |
65 | | - zone = var.zone |
66 | | - autoscaling_config { |
67 | | - min_nodes = 1 |
68 | | - max_nodes = 5 |
69 | | - cpu_target = 80 |
70 | | - } |
71 | | - } |
72 | | -} |
73 | | - |
74 | | -# Grant access to the app engine for the Bigtable instance |
75 | | -resource "google_bigtable_instance_iam_binding" "access" { |
76 | | - project = var.project_id |
77 | | - instance = google_bigtable_instance.instance.name |
78 | | - role = "roles/bigtable.admin" |
79 | | - |
80 | | - members = [ |
81 | | - "serviceAccount:${google_service_account.service_account.email}" |
82 | | - ] |
83 | | -} |
84 | | - |
85 | | -# Create App Engine application |
86 | | -resource "google_app_engine_application" "app" { |
87 | | - project = var.project_id |
88 | | - location_id = var.region |
89 | | -} |
90 | | - |
91 | | -# Grant log writer permissions to app engine |
92 | | -resource "google_project_iam_binding" "logs_writer_binding" { |
93 | | - project = var.project_id |
94 | | - role = "roles/logging.logWriter" |
95 | | - members = [ |
96 | | - "serviceAccount:${google_service_account.service_account.email}" |
97 | | - ] |
98 | | -} |
99 | | - |
100 | | -# Grant object reader permissions to app engine so it can access Google Container Registry |
101 | | -resource "google_project_iam_binding" "storage_object_viewer_binding" { |
102 | | - project = var.project_id |
103 | | - role = "roles/storage.objectViewer" |
104 | | - members = [ |
105 | | - "serviceAccount:${google_service_account.service_account.email}" |
106 | | - ] |
107 | | -} |
108 | | - |
109 | | -# Define a custom role with the specific pub/sub perms needed. |
110 | | -resource "google_project_iam_custom_role" "pubsub_role" { |
111 | | - project = var.project_id |
112 | | - role_id = "pubsub_role" |
113 | | - title = "Role for managing pub/sub from a software realm" |
114 | | - description = "Role for managing pub/sub from a software realm" |
115 | | - permissions = ["pubsub.subscriptions.create", |
116 | | - "pubsub.topics.attachSubscription", |
117 | | - "pubsub.topics.create", |
118 | | - "pubsub.topics.publish", |
119 | | - "pubsub.subscriptions.consume", |
120 | | - ] |
121 | | -} |
122 | | - |
123 | | -# Grant pub/sub access to the service account |
124 | | -resource "google_project_iam_binding" "pubsub_binding" { |
125 | | - project = var.project_id |
126 | | - role = google_project_iam_custom_role.pubsub_role.name |
127 | | - members = [ |
128 | | - "serviceAccount:${google_service_account.service_account.email}" |
129 | | - ] |
130 | | -} |
0 commit comments