Skip to content

Commit feab0b3

Browse files
authored
MRG: Merge pull request #3 from octue/switch-to-global-load-balancer
Add CDN and fix bugs
2 parents 242d526 + ef3f2ff commit feab0b3

8 files changed

+96
-40
lines changed

README.md

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,11 @@ locals {
7878
7979
8080
module "octue_django_api" {
81-
source = "git::github.com/octue/terraform-octue-django-api.git?ref=0.2.0"
81+
source = "git::github.com/octue/terraform-octue-django-api.git?ref=0.3.0"
8282
project = var.google_cloud_project_id
8383
region = var.google_cloud_region
8484
resource_affix = var.resource_affix
85+
api_url = var.api_url
8586
environment = local.environment
8687
}
8788
@@ -114,6 +115,22 @@ variable "resource_affix" {
114115
type = string
115116
default = "<name-of-your-api>"
116117
}
118+
119+
120+
variable "api_url" {
121+
type = string
122+
default = "api.your-org.com"
123+
}
124+
125+
126+
variable "maintainer_service_account_emails" {
127+
type = set(string)
128+
default = [
129+
"dev1@<your-google-project-id>.iam.gserviceaccount.com",
130+
"dev2@<your-google-project-id>.iam.gserviceaccount.com",
131+
]
132+
}
133+
117134
```
118135

119136
## Dependencies
@@ -152,17 +169,21 @@ terraform destroy
152169

153170
# Input reference
154171

155-
| Name | Type | Required | Default |
156-
|------------------------------------|---------------|----------|-----------------------------------------------------------------------------------------|
157-
| `google_cloud_project_id` | `string` | Yes | N/A |
158-
| `google_cloud_region` | `string` | Yes | N/A |
159-
| `resource_affix` | `string` | Yes | N/A |
160-
| `environment` | `string` | No | `"main"` |
161-
| `secret_names` | `set(string)` | No | `set(["django-secret-key", "database-proxy-url", "database-url", "stripe-secret-key"])` |
162-
| `tasks_queue_name_suffix` | `string` | No | `""` |
163-
| `database_availability_type` | `string` | No | `"ZONAL"` |
164-
| `maintainer_service_account_names` | `set(string)` | No | `["default"]` |
165-
| `deletion_protection` | `bool` | No | `true` |
172+
| Name | Type | Required | Default |
173+
|-------------------------------------|---------------|----------|-----------------------------------------------------------------------------------------|
174+
| `google_cloud_project_id` | `string` | Yes | N/A |
175+
| `google_cloud_region` | `string` | Yes | N/A |
176+
| `resource_affix` | `string` | Yes | N/A |
177+
| `api_url` | `string` | Yes | N/A |
178+
| `maintainer_service_account_emails` | `set(string)` | Yes | N/A |
179+
| `environment` | `string` | No | `"main"` |
180+
| `secret_names` | `set(string)` | No | `set(["django-secret-key", "database-proxy-url", "database-url", "stripe-secret-key"])` |
181+
| `tasks_queue_name_suffix` | `string` | No | `""` |
182+
| `minimum_instances` | `number` | No | `0` |
183+
| `maximum_instances` | `number` | No | `10` |
184+
| `database_tier` | `string` | No | `"db-f1-micro"` |
185+
| `database_availability_type` | `string` | No | `"ZONAL"` |
186+
| `deletion_protection` | `bool` | No | `true` |
166187

167188
See [`variables.tf`](/variables.tf) for descriptions.
168189

VERSION.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.2.0
1+
0.3.0

cloud_run.tf

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@ resource "google_cloud_run_v2_service" "server" {
66
ingress = "INGRESS_TRAFFIC_ALL"
77
deletion_protection = var.deletion_protection
88

9+
# The min instance count is set at the *service* level, not the *revision* level. See here for more info:
10+
# https://cloud.google.com/run/docs/configuring/min-instances#revisions
11+
scaling {
12+
min_instance_count = var.minimum_instances
13+
}
14+
915
template {
1016
service_account = google_service_account.server_service_account.email
1117

1218
scaling {
13-
max_instance_count = 10
19+
max_instance_count = var.maximum_instances
1420
}
1521

1622
volumes {
@@ -246,12 +252,14 @@ resource "google_cloud_run_v2_job" "manager" {
246252
}
247253

248254
lifecycle {
249-
ignore_changes = [
250-
template[0].template[0].containers[0].args,
251-
template[0].template[0].containers[0].image,
252-
client,
253-
client_version,
254-
]
255+
# TODO: The state was stale with revision creating changes
256+
# ignore_changes = [
257+
# template[0].template[0].containers[0].args,
258+
# template[0].template[0].containers[0].image,
259+
# client,
260+
# client_version,
261+
# ]
262+
ignore_changes = all
255263
}
256264

257265
depends_on = [google_secret_manager_secret.secrets]

database.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ resource "google_sql_database_instance" "postgres_instance" {
66
deletion_protection = var.deletion_protection
77
settings {
88
edition = "ENTERPRISE"
9-
tier = "db-f1-micro"
9+
tier = var.database_tier
1010
deletion_protection_enabled = var.deletion_protection
1111

1212
database_flags {

iam_roles.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
locals {
22
server_service_account_email = "serviceAccount:${google_service_account.server_service_account.email}"
33
maintainer_service_account_emails = toset(
4-
[for account in google_service_account.maintainers : "serviceAccount:${account.email}"]
4+
[for email in var.maintainer_service_account_emails : "serviceAccount:${email}"]
55
)
6-
all_service_account_emails = setunion(toset([local.server_service_account_email]), local.maintainer_service_account_emails)
76
}
87

98

iam_service_accounts.tf

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,3 @@ resource "google_service_account" "server_service_account" {
44
display_name = "${var.resource_affix}--server--${var.environment}"
55
project = var.google_cloud_project_id
66
}
7-
8-
9-
resource "google_service_account" "maintainers" {
10-
for_each = var.maintainer_service_account_names
11-
account_id = "maintainer-${each.key}"
12-
display_name = "maintainer-${each.key}"
13-
project = var.google_cloud_project_id
14-
description = "Allow ${each.key} to access most resources related to Octue Twined services."
15-
}

load_balancer.tf

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ resource "google_compute_region_network_endpoint_group" "load_balancer_neg" {
88
}
99
}
1010

11+
1112
resource "google_compute_backend_service" "load_balancer_backend" {
1213
connection_draining_timeout_sec = 0
1314
load_balancing_scheme = "EXTERNAL_MANAGED"
@@ -18,6 +19,16 @@ resource "google_compute_backend_service" "load_balancer_backend" {
1819
session_affinity = "NONE"
1920
timeout_sec = 30
2021
locality_lb_policy = "ROUND_ROBIN"
22+
enable_cdn = true
23+
24+
cdn_policy {
25+
cache_mode = "CACHE_ALL_STATIC"
26+
signed_url_cache_max_age_sec = 3600
27+
client_ttl = 3600
28+
default_ttl = 3600
29+
max_ttl = 86400
30+
serve_while_stale = 86400
31+
}
2132

2233
backend {
2334
balancing_mode = "UTILIZATION"
@@ -48,7 +59,7 @@ resource "google_compute_managed_ssl_certificate" "ssl" {
4859

4960
managed {
5061
domains = [
51-
"api.bezier.octue.com"
62+
var.api_url
5263
]
5364
}
5465

variables.tf

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ variable "resource_affix" {
1616
}
1717

1818

19+
variable "api_url" {
20+
type = string
21+
description = "The URL for the API e.g. 'api.strands.octue.com'"
22+
}
23+
24+
25+
variable "maintainer_service_account_emails" {
26+
type = set(string)
27+
description = "The email addresses of the maintainers' IAM service accounts."
28+
}
29+
30+
1931
variable "environment" {
2032
type = string
2133
default = "main"
@@ -35,13 +47,34 @@ variable "secret_names" {
3547
}
3648

3749

50+
variable "minimum_instances" {
51+
type = number
52+
default = 0
53+
description = "The minimum number of instances for the Cloud Run service (set at the service level, not revision level)."
54+
}
55+
56+
57+
variable "maximum_instances" {
58+
type = number
59+
default = 10
60+
description = "The maximum number of instances for the Cloud Run service (set at the revision level, not service level)."
61+
}
62+
63+
3864
variable "tasks_queue_name_suffix" {
3965
type = string
4066
default = ""
4167
description = "An optional suffix to be added to the resource name of the task queue. Only use when attempting to recreate a queue after it has been deleted as a queue with the same name cannot be created within 7 days."
4268
}
4369

4470

71+
variable "database_tier" {
72+
type = string
73+
default = "db-f1-micro"
74+
description = "The machine type to use for the database."
75+
}
76+
77+
4578
variable "database_availability_type" {
4679
type = string
4780
default = "ZONAL"
@@ -53,13 +86,6 @@ variable "database_availability_type" {
5386
}
5487

5588

56-
variable "maintainer_service_account_names" {
57-
type = set(string)
58-
default = ["default"]
59-
description = "The names of each maintainer IAM service account that should be created. They'll automatically be prefixed with 'maintainer-'."
60-
}
61-
62-
6389
variable "deletion_protection" {
6490
type = bool
6591
default = true

0 commit comments

Comments
 (0)