使用谷歌提供商执行“terraform apply”,错误(oauth2:无法获取令牌)

问题描述 投票:0回答:1

我已经解决这个问题两天了:

我按照课程中的说明创建了一个项目服务帐户。我为服务帐户分配了必要的权限(BigQuery 管理员、计算管理员、计算网络管理员、组织管理员、存储管理员、存储对象管理员、查看者),并将密钥放置在与main.tf 文件。在 main.tf 文件的“provider”块中,我将凭据设置为“./keys/my-creds.json”。

但是,当我执行“terraform apply”时,却显示错误:

google_storage_bucket.demo-bucket: Creating...
google_storage_bucket.demo-bucket: Still creating... [10s elapsed]
google_storage_bucket.demo-bucket: Still creating... [20s elapsed]
google_storage_bucket.demo-bucket: Still creating... [30s elapsed]
╷
│ Error: Post "https://storage.googleapis.com/storage/v1/b?alt=json&prettyPrint=false&project=coherent-ascent-379901": oauth2: cannot fetch token: Post "https://oauth2.googleapis.com/token": dial tcp 172.217.163.42:443: i/o timeout

这是我的main.tf的内容

terraform {
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "5.13.0"
    }
  }
}

provider "google" {
  credentials = "./keys/my-creds.json" # Credentials only needs to be set if you do not have the GOOGLE_APPLICATION_CREDENTIALS set
  project     = "coherent-ascent-379901"
  region      = "us-central1"
}



resource "google_storage_bucket" "demo-bucket" {
  name          = "coherent-ascent-379901-terra-bucket"
  location      = "US"
  force_destroy = true

  lifecycle_rule {
    condition {
      age = 2 //days
    }
    action {
      type = "AbortIncompleteMultipartUpload"
    }
  }
}

有人遇到过类似的问题吗?

这似乎是 OAuth2 令牌检索问题。以下是我的行动总结:

1.使用

gcloud auth list
检查身份验证状态并确认服务帐户和用户帐户均处于活动状态

2.使用

gcloud components update

将SDK更新到最新版本

3.通过运行

gcloud auth application-default print-access-token

成功获取访问令牌

4.尝试通过运行

gcloud auth application-default login

重新授权

尝试这些操作后,当我执行“terraform apply”时,错误仍然存在。

google-cloud-platform terraform google-cloud-storage terraform-provider-gcp
1个回答
0
投票

问题出在网络上。 Google 在我的国家/地区无法访问,我使用的是 VPN。并且终端程序不会自动跟随系统代理,需要单独的代理配置设置。我在 Clash 中打开了增强模式,这是一个 VPN 应用程序,“terraform apply”有效!

我希望这个问题可以帮助正在解决同样问题的人。

© www.soinside.com 2019 - 2024. All rights reserved.