使用 Terraform 创建 GCP VPC 时出错 - 无法获取令牌:意外 EOF

当我使用 terraform 创建网络 vpc 时,它显示错误“oauth2:无法获取令牌:意外 EOF”有人知道这是什么意思吗?

这是我的地形代码:

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"
  }
}

这是我在运行 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" {
stack overflow Error when creating GCP VPC with Terraform - cannot fetch token: unexpected EOF
原文答案

答案:

作者头像

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

这里有几件事要验证:

1.验证身份验证密钥JSON文件和路径。

.json身份验证密钥文件的正确定义是: `credentials = file("/home/service-account-key.json")` ,其中“ service-account-key.json”文件在目录中“/home//”,并使用指南 [Creating and managing service account keys](https://cloud.google.com/iam/docs/creating-managing-service-account-keys) 获得。

2.验证 Service Account User role

在这种特殊情况下,该角色应像 [Compute Network Admin role](https://cloud.google.com/compute/docs/access/iam#compute.networkAdmin) 一样为VPC创建授予,在这里您可以找到完整的 [Compute Engine IAM roles and permissions](https://cloud.google.com/compute/docs/access/iam) 参考。

看看这个 How to ,它描述了创建服务帐户,给出正确的权限并在Terraform中使用它的整个过程。