Reputation: 6239
-ip_address_types=PRIVATE
argument I've found in some of the documentation.Slightly simplified Terraform code for reproducing the state that confuses me is here: https://github.com/hallvors/gcp-network-issue-demo To test this, do the following:
./local-secrets/google-project-credentials.json
terraform workspace new staging
terraform init
terraform apply
When Terraform is done, you should have a database and a VM set up in the project.
sudo apt install postgresql-client-common postgresql-client
psql --host 10.167.0.3 -U gcp-network-issue-demo-staging-db-user gcp-network-issue-demo-staging-database
What am I missing?
Upvotes: 3
Views: 2352
Reputation: 6239
The cause of this problem was me failing to understand that a network interface can have both public and private IPs/networks. So my code set up one interface for the public and one for the private network for the google_compute_instance:
# Update VM needs a public IP
network_interface {
network = "default"
access_config {
}
}
network_interface {
network = var.network
subnetwork = var.subnetwork
}
Now, I still don't fully understand networking but it appears you can not (easily?) specify what interface the database connection attempts should use and it does not automatically pick the right one. The fix is in this commit, configuring both access to the private network and the public one in one network interface:
https://github.com/hallvors/gcp-network-issue-demo/commit/ea14174c1087c89b92310b5b4913e12a4e17130d
Upvotes: 2