DEV Community

Georgios Giannoutsos Barkas
Georgios Giannoutsos Barkas

Posted on

GCP Cloud Functions with a Static IP - Terraform version

Following this great post from Alvaro, I converted the gcloud commands, to their respective Terraform config.



resource "google_compute_network" "cloud_function_network" {
  name = "cloud-function-network"
  auto_create_subnetworks = false
}

resource "google_vpc_access_connector" "connector" {
  name = "connector"
  region = "europe-west3"
  ip_cidr_range = "10.8.0.0/28"
  network = google_compute_network.cloud_function_network.name
}

resource "google_compute_address" "egress_ip_address" {
  name = "egress-ip-address"
  region = "europe-west3"
}

resource "google_compute_router" "router" {
  name    = "egress-router"
  region  = "europe-west3"
  network = google_compute_network.cloud_function_network.name
}

resource "google_compute_router_nat" "cloud_function_nat" {
  name = "egress-router-nat"
  router = google_compute_router.router.name
  region = google_compute_router.router.region
  nat_ip_allocate_option = "MANUAL_ONLY"
  nat_ips = google_compute_address.egress_ip_address.*.self_link

  source_subnetwork_ip_ranges_to_nat = "ALL_SUBNETWORKS_ALL_IP_RANGES"

  log_config {
    enable = true
    filter = "ERRORS_ONLY"
  }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)