I was creating a Cloud Run service and the domain mapping with this Terraform code:
resource "google_cloud_run_service" "order" {
autogenerate_revision_name = false
location = "asia-southeast1"
name = "order"
project = "mycompany"
template {
spec {
containers {
args = []
command = []
image = "asia.gcr.io/mycompany/order:1.1.11"
ports {
container_port = 7075
}
resources {
limits = {
"cpu" = "1000m"
"memory" = "2048Mi"
}
requests = {}
}
}
}
}
}
resource "google_cloud_run_domain_mapping" "order" {
location = var.region
name = "order.${var.public_domain}"
metadata {
namespace = var.project_id
}
spec {
route_name = "order"
}
}
When I apply that code, the first apply fails.
Error: Error waiting to create DomainMapping: resource is in failed state "Ready:False", message: Route order does not exist.
Then after waiting for couple of minutes I reapplied and it finishes with no errors. I assumed that was because of at the first apply the resource google_cloud_run_service
was not ready yet.
So my question is how can we configure the resource creation of google_cloud_run_domain_mapping
waiting for google_cloud_run_service
to be created first?
If you look at the example in the google_cloud_run_domain_mapping
resource documentation you'll see that they reference the output of the google_cloud_run_service
resource:
resource "google_cloud_run_service" "default" {
name = "cloudrun-srv"
location = "us-central1"
metadata {
namespace = "my-project-name"
}
template {
spec {
containers {
image = "us-docker.pkg.dev/cloudrun/container/hello"
}
}
}
}
resource "google_cloud_run_domain_mapping" "default" {
location = "us-central1"
name = "verified-domain.com"
metadata {
namespace = "my-project-name"
}
spec {
route_name = google_cloud_run_service.default.name
}
}
This allows Terraform to realise that there is a dependency order between the two resources and so it will wait until the google_cloud_run_service
resource has been created first before attempting to create the google_cloud_run_domain_mapping
resource.
These following links document how Terraform builds a dependency graph between the resources you configure:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.