3

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
  • 1
    You can't! But, above all I don't understand your use case. Why do you need to create a service which "delivers no service"? Commented Feb 17, 2021 at 15:36
  • 6
    @guillaumeblaquiere one reason might be that Terraform is creating the entire project from scratch including the Container Registry. If Terraform is building the Registry then it will be empty so how to configure Cloud Run so that it can be updated by CI/CD tools after the infrastructure is up. I can see this as a common requirement as most companies separate their infrastructure setup and CI/CD setup. CI/CD needs the container registry and cloud run information to update, but cloud run needs the container the CI/CD hasn't built yet? Commented Sep 16, 2021 at 4:10

3 Answers 3

4

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 = "[email protected]"
    }

  }

  traffic {
    percent         = 100
    latest_revision = true
  }

}

Sign up to request clarification or add additional context in comments.

1 Comment

I have a problem when uploading an image via github actions etc and deploying it to cloud run. If I use terraform after a successful deploy, it tries to revert to hello because it is the uploaded image and not us-docker.pkg.dev/cloudrun/container/hello.
1

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.

Comments

0

You could use the ignore_changes meta-argument to tell Terraform to ignore future changes in image as explained in this other answer in StackOverflow.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.