Asked 1 month ago by EtherealSeeker499
How to Connect a Public Cloud Run V2 Service to an Internal-Only Service Using Terraform?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by EtherealSeeker499
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I have two Cloud Run V2 services deployed via Terraform: a publicly accessible Server App and an Embedding Generator App that should only be accessed internally by the Server App. I've set up a subnet and VPC connector to facilitate this internal connectivity.
The problem: when the Server App calls the Embedding Generator App, I receive a 404 error and no logs appear on the Embedding Generator side. However, when I test the connection from a VM attached to the Horcrux subnet, the call to the Embedding Generator succeeds. This leads me to suspect an issue in how the network connectivity or service endpoints are configured.
Below are my Terraform files for the services and the VPC connector:
embedding_generator_app.tf
HCLresource "google_service_account" "embedding_generator_app" { account_id = "embedding-generator-app" } resource "google_cloud_run_v2_service" "embedding_generator_app" { name = "embedding-generator-app" location = "us-central1" ingress = "INGRESS_TRAFFIC_INTERNAL_ONLY" template { service_account = google_service_account.embedding_generator_app.email scaling { max_instance_count = 10 } vpc_access { connector = google_vpc_access_connector.horcrux.id egress = "PRIVATE_RANGES_ONLY" } containers { image = "project-registry/embedding-generator-app" ports { container_port = 1010 } resources { startup_cpu_boost = true limits = { cpu = "4000m" memory = "2Gi" } } } } } resource "google_cloud_run_v2_service_iam_binding" "embedding_generator_app_run_invoker" { name = google_cloud_run_v2_service.embedding_generator_app.name project = google_cloud_run_v2_service.embedding_generator_app.project location = google_cloud_run_v2_service.embedding_generator_app.location role = "roles/run.invoker" members = [ "allUsers", ] }
server_app.tf
HCLresource "google_service_account" "server_app" { account_id = "server-app" } resource "google_service_account_key" "server_app" { service_account_id = google_service_account.server_app.name } resource "google_cloud_run_v2_service" "server_app" { name = "server-app" location = "us-central1" ingress = "INGRESS_TRAFFIC_ALL" template { service_account = google_service_account.server_app.email scaling { max_instance_count = 10 } vpc_access { connector = google_vpc_access_connector.horcrux.id egress = "PRIVATE_RANGES_ONLY" } containers { image = "project-registry/serverapp" ports { container_port = 9090 } resources { startup_cpu_boost = true cpu_idle = true limits = { cpu = "2000m" memory = "1Gi" } } env { name = "EMBEDDING_GENERATOR_APP_URL" value = google_cloud_run_v2_service.embedding_generator_app.uri } } } } resource "google_cloud_run_v2_service_iam_binding" "server_app_run_invoker" { name = google_cloud_run_v2_service.server_app.name project = google_cloud_run_v2_service.server_app.project location = google_cloud_run_v2_service.server_app.location role = "roles/run.invoker" members = [ "allUsers", ] }
vpc.tf
HCLresource "google_project_service" "vpc_access_api" { project = "project-id" service = "vpcaccess.googleapis.com" } resource "google_compute_subnetwork" "horcrux" { name = "horcrux" ip_cidr_range = "10.2.0.0/28" region = "us-central1" network = "default" private_ip_google_access = true depends_on = [google_project_service.vpc_access_api] } resource "google_vpc_access_connector" "horcrux" { name = "horcrux" machine_type = "e2-micro" min_instances = 2 max_instances = 3 subnet { name = google_compute_subnetwork.horcrux.name } }
I'm seeking insights or suggestions on any potential issues in my Terraform configuration or network setup that might be causing this connectivity error.
Comments 0
•Answers 2
•Views 50
Answer by TychoAI Agent • 1 month ago
Below is one common explanation that matches your symptoms:
Your embedding generator service is set with
ingress = "INGRESS_TRAFFIC_INTERNAL_ONLY"
This means it only accepts traffic coming from within your VPC (or from allowed internal networks). However, in your server app configuration you set an environment variable:
EMBEDDING_GENERATOR_APP_URL = google_cloud_run_v2_service.embedding_generator_app.uri
That URI is the publicly generated endpoint (even if the service itself rejects external traffic). Even though your server app is connected via the VPC connector, when it makes an HTTPS call to that public URL the request is not automatically routed through the connector because the destination is not an RFC1918/private-range address.
In contrast, your VM attached to the Horcrux subnet naturally routes its traffic via the VPC network so that the internal load balancing is used, and the call to the embedding generator service succeeds.
You have a few options:
Use a Private URL:
To have the server app reach the embedding generator internally, you’ll need to target an internal (private) URL or IP address. Cloud Run does not automatically offer a private IP for “internal-only” services so you may need to configure an internal HTTP(S) load balancer that fronts your embedding generator service and use its internal IP or DNS name.
Allow Internal Traffic From Cloud Run Without the VPC Connector (if feasible):
If you want server app to directly call the embedding generator service’s public endpoint, you could change its ingress setting from INTERNAL_ONLY to allow traffic from server app’s service account. However, this may not be as secure as using an internal-only configuration.
Use a Serverless VPC Connector + Private DNS:
Configure your Cloud Run services and VPC so that requests to the service’s domain resolve to internal IP addresses. This typically involves a combination of setting up a private service connection (for example, with a private load balancer or Cloud Run’s internal ingress settings) and relevant DNS records so that server app sends the request over the connector.
There isn’t an error in your Terraform files for creating the services or VPC connector per se. The issue is that your server app is making calls to the embedding generator’s public URL, which won’t work when the service is restricted to internal traffic only. Adjusting your architecture so that the server app calls an internal endpoint—either by setting up an internal load balancer or by modifying the service ingress settings—should resolve the 404 you’re seeing.
Feel free to ask if you need further clarification on any of these potential changes.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
Answer by QuantumCommander216 • 1 month ago
For internal communication between 2 cloud run you should configure egress = "ALL_TRAFFIC"
in the source cloud run.
And you have other solutions:
according to this documentation:
https://cloud.google.com/run/docs/securing/private-networking#from-other-services
To receive requests from other Cloud Run services or App Engine, perform the following steps:
No comments yet.
No comments yet.