I am building with Terraform a Cloud Scheduler job that will hit a service deployed in Cloud Run. Because the service and the scheduler are deployed in different pipelines, it is possible that the service does not exist yet when running the scheduler pipeline. This is why I am using data "google_cloud_run_service" to retrieve the service and then control whether it exists in the scheduler block. It is here that I don't find the right syntax to use in count.
data "google_cloud_run_service" "run-service" {
name = "serv-${var.project_env}"
location = var.region
}
resource "google_cloud_scheduler_job" "job" {
count = length(data.google_cloud_run_service.run-service.status)
name = "snap-job-${var.project_env}"
description = "Call write API on my service"
schedule = "* * * * *"
time_zone = "Etc/UTC"
http_target {
http_method = "GET"
uri = "${data.google_cloud_run_service.run-service.status[0].url}/write"
oidc_token {
service_account_email = google_service_account.sa_scheduler.email
}
}
depends_on = [google_app_engine_application.app]
}
- The above
length(data.google_cloud_run_service.run-service.status)
control will not make any effect, and Terraform tries to create the scheduler even though there is no service defined. - I have also tried other variations with similar result such as
length(data.google_cloud_run_service.run-service.status[0]) > 0 ? 1 : 0
. - Other options that I tried will not work either with different errors:
data.google_cloud_run_service.run-service ? 1 : 0
: data.google_cloud_run_service.run-service is object with 9 attributes;The condition expression must be of type booldata.google_cloud_run_service.run-service.status[0].url ? 1 : 0
: data.google_cloud_run_service.run-service is object with 9 attributes;The condition expression must be of type bool