gcp, terraform,

SERVICE_NETWORKING_NOT_ENABLED error in Terraform

Aug 10, 2022 · 1 min read · 34 views · Post a comment

Another GCP Terraform issue I was facing this week to share. It was related to not having API enabled as with most of them. This time the error I saw in the default console output was:

Error: Error, failed to create instance xxxx: googleapi: Error 400: Invalid request:
Incorrect Service Networking config for instance:
xxxx:xxxxx:SERVICE_NETWORKING_NOT_ENABLED., invalid

Prerequisites

  • GCP account
  • Terraform

Solution(s)

As for the solution, it’s a bit obvious. You need to enable the Service Networking API. Three ways to do it tho – Terraform, CLI and the good old manual way ordered by best practice.

gcloud CLI:

gcloud services enable servicenetworking.googleapis.com --project=<gcp_project_id>

Terraform:

## variables.tf
variable "gcp_services_list" {
  description = "The list of GCP APIs necessary for the project."
  type        = list(string)
}

## terraform.tfvars
gcp_env_services_list = [
  "cloudresourcemanager.googleapis.com",
  "servicenetworking.googleapis.com"
]

## main.tf
resource "google_project" "this" {
  { some code here... }
}

resource "google_project_service" "enable_google_apis" {
  count   = length(var.gcp_services_list)
  
  project = google_project.this.project_id
  service = var.gcp_services_list[count.index]

  disable_dependent_services = true
}

Regarding the manual way, just google it lol.

Conclusion

Tried everything and nothing works? Let me know in the comment section below. On a side note, follow our official channel on Telegram.