From b2f9a42a90e384a42be608e8c9e9d06bf524c3bf Mon Sep 17 00:00:00 2001
From: Rich Wareham <rjw57@cam.ac.uk>
Date: Thu, 11 Jun 2020 11:53:01 +0100
Subject: [PATCH] Allow sql_instance_connection_name to be empty

Sometimes we don't need a SQL instance for the webapp. Allow
sql_instance_connection_name to be empty and, if so, don't add the Cloud
SQL connection roles to the service account or add the SQL instance
annotation to the webapp.

Closes #5
---
 main.tf      | 43 ++++++++++++++++++++++++++-----------------
 variables.tf |  2 +-
 2 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/main.tf b/main.tf
index 38b13ae..1bcc6bc 100644
--- a/main.tf
+++ b/main.tf
@@ -8,7 +8,10 @@ resource "google_service_account" "webapp" {
 }
 
 # The webapp service account has the ability to connect to the SQL instance.
+# (Only if sql_instance_connection_name is non-empty.)
 resource "google_project_iam_member" "webapp_sql_client" {
+  count = (var.sql_instance_connection_name != "") ? 1 : 0
+
   project = local.sql_instance_project
   role    = "roles/cloudsql.client"
   member  = "serviceAccount:${google_service_account.webapp.email}"
@@ -28,23 +31,29 @@ resource "google_cloud_run_service" "webapp" {
 
   template {
     metadata {
-      annotations = {
-        # Maximum number of auto-scaled instances.  For a container with
-        # N-workers, maxScale should be less than 1/N of the maximum connection
-        # count for the Cloud SQL instance.
-        "autoscaling.knative.dev/maxScale" = var.max_scale
-
-        # Cloud SQL instances to auto-magically make appear in the container as
-        # Unix sockets.
-        "run.googleapis.com/cloudsql-instances" = var.sql_instance_connection_name
-
-        # As mentioned at https://www.terraform.io/docs/configuration/resources.html#ignore_changes
-        # placeholders need to be created as the adding the key to the map is
-        # considered a change and not ignored by ignore_changes
-        "client.knative.dev/user-image"     = "placeholder"
-        "run.googleapis.com/client-name"    = "placeholder"
-        "run.googleapis.com/client-version" = "placeholder"
-      }
+      annotations = merge(
+        # Annotations which are always set:
+        {
+          # Maximum number of auto-scaled instances.  For a container with
+          # N-workers, maxScale should be less than 1/N of the maximum connection
+          # count for the Cloud SQL instance.
+          "autoscaling.knative.dev/maxScale" = var.max_scale
+
+          # As mentioned at https://www.terraform.io/docs/configuration/resources.html#ignore_changes
+          # placeholders need to be created as the adding the key to the map is
+          # considered a change and not ignored by ignore_changes
+          "client.knative.dev/user-image"     = "placeholder"
+          "run.googleapis.com/client-name"    = "placeholder"
+          "run.googleapis.com/client-version" = "placeholder"
+        },
+
+        # Annotations which are only set if there is a Cloud SQL instance:
+        (var.sql_instance_connection_name != "") ? {
+          # Cloud SQL instances to auto-magically make appear in the container as
+          # Unix sockets.
+          "run.googleapis.com/cloudsql-instances" = var.sql_instance_connection_name
+        } : {},
+      )
 
       # See the README for information on this.
       name = random_id.webapp_revision_name.hex
diff --git a/variables.tf b/variables.tf
index cba5cef..17f7a1f 100644
--- a/variables.tf
+++ b/variables.tf
@@ -16,7 +16,7 @@ variable "sql_instance_project" {
 
 variable "sql_instance_connection_name" {
   description = "SQL instance connection name"
-  type        = string
+  default     = ""
 }
 
 variable "cloud_run_region" {
-- 
GitLab