Error when creating GCP VPC with Terraform - cannot fetch token: unexpected EOF

Viewed 2889

when I create network vpc using terraform, it showed error "oauth2: cannot fetch token: unexpected EOF" Does anybody knows what it means?

Here is my terraform code:

provider "google" {
  version = "3.5.0"
  credentials = file("my_service_account_key_json_file")
  project = "myproject"
  region  = "us-central1"
  zone    = "us-central1-c"
}

resource "google_compute_network" "vpc_network" { #line 9
  name = "terraform-network"
}

terraform {
  backend "gcs" {
    bucket = "my_bucket"
    prefix = "my_folder"
    credentials = "my_service_account_key_json_file"
  }
}

here is the error I got when runing terraform apply:

google_compute_network.vpc_network: Creating...

Error: Error creating Network: Post 
https://www.googleapis.com/compute/v1/projects/<myproject>/global/networks?alt=json: oauth2: 
cannot fetch token: unexpected EOF

  on main.tf line 9, in resource "google_compute_network" "vpc_network":
  9: resource "google_compute_network" "vpc_network" {
1 Answers

This answer was created to support the community partially based on the answers provided in the comments.

A few things to validate here:

  1. Verify the Authentication key json file and path.

    A correct definition of a .json authentication key file would be: credentials = file("/home/service-account-key.json"), where the file "service-account-key.json" is in the directory "/home/" and was obtained using the guide Creating and managing service account keys.

  2. Verify the Service Account User role.

    In this particular case the role should be one granted for VPC creation like the Compute Network Admin role, and here you can find the complete Compute Engine IAM roles and permissions reference.

Take a look at this How to, which describes the whole process of creating a Service Account, giving the correct permissions and using it in terraform.

Related