I want to deploy a google cloud run service using terraform. When I try to deploy via 'port' block for defining the container port, getting error, I have to pass the container port from template tag but unable to do that. Here is my .tf file -
resource "google_cloud_run_service" "default" { name = "cloudrun-srv" location = "us-central1" template { spec { containers { image = "" port { container_port = 19006 } } } } traffic { percent = 100 latest_revision = true }
}
data "google_iam_policy" "noauth" { binding { role = "roles/run.invoker" members = [ "allUsers", ] }
}
resource "google_cloud_run_service_iam_policy" "noauth" { location = google_cloud_run_service.default.location project = google_cloud_run_service.default.project service = google_cloud_run_service.default.name policy_data = data.google_iam_policy.noauth.policy_data
}
output "url" { value = "${google_cloud_run_service.default.status[0].url}"
}With the port tag, here is the error -
And if I not pass the Port block, here is the error -
I have to pass the container port value as 19006 because of my container is running on that port only. How I pass the container port 19006 instead of default port 8080.
3 Answers
I had a look at REST API exposed by Google for creating a Cloud Run service.
This starts with the entry here:
where the body contains a Service.
which contains a ServiceSpec
which contains a RevisionRemplate
which contains a RevisionSpec
which contains a Container
which contains a ContainerPort
If we now map this to the source of the Terraform extension to handle creation of Cloud Run Services, we find:
and in the comments, we find the following:
In the context of a Revision, we disallow a number of the fields of this Container, including: name, ports, and volumeMounts. The runtime contract is documented here:
While name and volumeMounts seems ok to me at this point, I'm not sensing the reason that ports are not mapped.
From this though, I seem to see that the inability to specify a port through Terraform seems to be explicit as opposed to an omission. I also seem to see that the ability to specify a port is indeed present in the REST API at Google.
I was then going to suggest that you raise a defect through Github but then wondered if it was already present. I did some digging and there is already a request for the missing feature:
Allow specifying 'container_port' and 'request_timeout' for google_cloud_run_service
My belief is that the core answer to your question then becomes:
2What you are trying to do should work with Terraform and has been raised as an issue and we must wait for the resolution in the Terraform provider.
The block should be ports (i.e. plural), not port
I needed to expose port 9000 and solved it this way:
resource "google_cloud_run_service" "service" { ... template { spec { containers { ... ports { container_port = 9000 } } } }
} 1