sidharth vijayakumar
sidharth vijayakumar

Reputation: 1571

Unable to create gcp vpc using terraform

I am trying to create VPC in GCP using Terraform but when i run terraform apply I am getting an error in terminal. I am new to terraform and GCP this is the code is have used

//Google Cloud provider
provider "google" {
 credentials = "${file("${var.credentials}")}"
 project     = "${var.gcp_project}"
 region      = "${var.region}"
}
// Create VPC
resource "google_compute_network" "vpc_network" {
 name                    = "${var.name}-vpc"
 auto_create_subnetworks = "false"
}

//variables.tf
variable "region" {}
variable "gcp_project" {}
variable "credentials" {}
variable "name" {}
variable "subnet_cidr" {}

// terraform.tfvars
region          = "europe-west2"
gcp_project     = "rock-prism-350316"
credentials     = "credentials.json"
name            = "dev"
subnet_cidr     = "10.10.0.0/24"

I am using a service account which has below access : Editor access for project, admin compute network

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

google_compute_network.vpc_network: Creating...
╷
│ Error: Error creating Network: Post 
"https://compute.googleapis.com/compute/v1/projects/rock-prism-350316/global/networks? 
alt=json": oauth2: cannot fetch token: unexpected EOF
│
│   with google_compute_network.vpc_network,
│   on main.tf line 8, in resource "google_compute_network" "vpc_network":
│    8:     resource "google_compute_network" "vpc_network" {

Upvotes: 0

Views: 1242

Answers (2)

sidharth vijayakumar
sidharth vijayakumar

Reputation: 1571

The issue was there a security software installed on my device and that was blocking the communication between GCP provider and terraform. I had to disable this from services once that was done it was working fine. There was no issues in code or in authentication.

Upvotes: 0

PeteP
PeteP

Reputation: 621

You can use the contents of a key file or a file path in the provider (see credentials section).
The error message you're getting shows that it's trying to create a network in project rock-prism-350316 using credentials for project sunlit-vortex-184612

Try correcting the gcp_project value in your tfvars file. It's also a good idea to add the project parameter to your VPC resource:

//Google Cloud provider
provider "google" {
 credentials = "${file("${var.credentials}")}"
 project     = "${var.gcp_project}"
 region      = "${var.region}"
}
// Create VPC
resource "google_compute_network" "vpc_network" {
 name                    = "${var.name}-vpc"
 project                 = "${var.gcp_project}"
 auto_create_subnetworks = "false"
}

//variables.tf
variable "region" {}
variable "gcp_project" {}
variable "credentials" {}
variable "name" {}
variable "subnet_cidr" {}

// terraform.tfvars
region          = "europe-west2"
gcp_project     = "rock-prism-350316"
credentials     = "credentials.json"
name            = "dev"
subnet_cidr     = "10.10.0.0/24"

Upvotes: 1

Related Questions