How to create an empty Cloud Run service initially without a container URL?

Viewed 752

Is it possible to create a cloud run service without specifying the container? The container will be deployed later through cloud build. But now when I create the cloud run initially, it asks for container image, and I don't have it. Without the container it doesn't work on both the gcp UI and terraform. How to do this? What is the approach for such cases?

resource "google_cloud_run_service" "default" {
  provider = google-beta
  name     = var.service_name
  location = var.region

  template {
    spec {
      containers {
        image = "us-central1-docker.pkg.dev/holabola-dev/holabolaartifacts/holabola:latest"
      }
    }
  }

  metadata {
    annotations = {
      "autoscaling.knative.dev/minScale" = "1"
      "autoscaling.knative.dev/maxScale" = "1000"
      "run.googleapis.com/launch-stage" : "BETA"
      "run.googleapis.com/vpc-access-connector" = "vpc-connector"
    }
  }
}
2 Answers

You'll have to wait until the container image is built before you can deploy it on Cloud Run. Cloud Run is for running stateless containers. Without containers, you can't "run" anything.

Or you can create an "empty" Cloud Run service by deploying a demo container (hello) and run your Terraform script when your target container is available then update the service. But from what I understand, it doesn't really solve anything because there's an option to create a service in Terraform.

Google Cloud provides a dummy docker image for this purposes, it is called us-docker.pkg.dev/cloudrun/container/hello and you can configure your terraform file with this image with something like this:

resource "google_cloud_run_service" "backend-service" {
  name     = "backend-service"
  location = "europe-east1"

  template {
    spec {
      containers {
        image = "us-docker.pkg.dev/cloudrun/container/hello"
        ports {
          container_port = 1234
          name           = "http1"
        }


      }
      service_account_name = "myserviceaccount@myserviceaccount.iam.gserviceaccount.com"
    }

  }

  traffic {
    percent         = 100
    latest_revision = true
  }

}

Related