hashicorp / terraform-provider-google

Terraform Provider for Google Cloud Platform
https://registry.terraform.io/providers/hashicorp/google/latest/docs
Mozilla Public License 2.0
2.25k stars 1.7k forks source link

VPC Direct Connection for Cloud Functions v2 #16076

Open titan-graham opened 9 months ago

titan-graham commented 9 months ago

Community Note

Description

Recently the terraform resources for Cloud Run were updated to support VPC direct connections πŸ‘

It would be very useful if the google_cloudfunctions2_function resource could also specify VPC Direct connection as an alternative to setting a VPC connector in the service_config block.

New or Affected Resource(s)

Potential Terraform Configuration

  service_config {
    vpc_access{
      network_interfaces {
        network = "default"
        subnetwork = "default"
        tags = ["tag1", "tag2", "tag3"]
      }
      egress = "ALL_TRAFFIC"
    }
  }

References

b/305197548

imrannayer commented 9 months ago

@titan-graham this feature is only available for Cloud Run. Do you have any reference for this feature with cloud function?

titan-graham commented 9 months ago

@imrannayer hmm πŸ€” maybe I've made an assumption here. A v2 cloud function deploys a v2 cloud run instance for it's compute, I thought therefore it would be a supported use case for Direct VPC access - since the benefits are the same...

I don't see any docs in GCP specific to functions and direct VPC access, but the option is available in the cloud run instance provisioned by a function.

Screenshot 2023-10-06 at 17 18 40

silvpol commented 6 months ago

It works if you change it manually on the automatically-provisioned Cloud Run service. The Cloud Run service has same name as the Cloud Function so you can just use it's name directly. I was considering just importing the created Cloud Run service into Terraform but that felt too hacky.

N.B. you can also use Managed Domains feature directly in Terraform with Cloud Functions:

resource "google_cloud_run_domain_mapping" "RESOURCE_NAME" {
  name     = "example.com"
  project  = PROJECT_ID
  location = LOCATION

  metadata {
    namespace = PROJECT_ID
  }

  spec {
    route_name = google_cloudfunctions2_function.MY_FUNCTION.name
  }
}
rosmo commented 4 months ago

Looks like this is not available from API perspective, so it either needs to be added to the API (sounds to me like just exposing an annotations field would make the most sense) or use some kind of after-creation operation of fetching the Cloud Run service and patching it.