google-cloud-platform - 具有Terraform的Google Cloud凭据

标签 google-cloud-platform terraform terraform-provider-gcp terragrunt

这是一个新手问题,但是我刚刚开始使用Terraform/Terragrunt进行GCP设置,并且我发现获取GCP凭据的工作流程非常令人困惑。我来自专门使用AWS的地方,在那里获取凭证并在AWS CLI中对其进行配置非常简单。

基本上,Google Cloud Provider documentation指出您应该定义一个provider块,如下所示:

provider "google" {
  credentials = "${file("account.json")}"
  project     = "my-project-id"
  region      = "us-central1"
  zone        = "us-central1-c"
}

credentials字段显示我(显然)必须生成一个服务帐户,并在文件系统上的某个位置保留JSON。

但是,如果运行命令gcloud auth application-default login,这将生成一个位于~/.config/gcloud/application_default_credentials.json的 token ;或者,我也可以使用gcloud auth login <my-username>。从那里,我可以使用gcloud命令从命令行访问Google API(这也是Terraform在后台执行的操作)。

那么,为什么Terraform提供程序需要服务帐户的JSON文件?为什么不能仅使用gcloud CLI工具已经使用的凭证?

顺便说一句,如果我将Terraform配置为指向application_default_credentials.json文件,则会出现以下错误:

Initializing modules...

Initializing the backend...

Error: Failed to get existing workspaces: querying Cloud Storage failed: Get https://www.googleapis.com/storage/v1/b/terraform-state-bucket/o?alt=json&delimiter=%2F&pageToken=&prefix=projects%2Fsomeproject%2F&prettyPrint=false&projection=full&versions=false: private key should be a PEM or plain PKCS1 or PKCS8; parse error: asn1: syntax error: sequence truncated

最佳答案

if I configure Terraform to point to the application_default_credentials.json file, I get the following errors:



提供程序配置中的credentials字段需要服务帐户 key 文件的路径,而不是用户帐户凭据文件的路径。如果要使用您的用户帐户进行身份验证,请尝试省略credentials,然后运行gcloud auth application-default login;如果Terraform找不到您的凭据文件,则可以将GOOGLE_APPLICATION_CREDENTIALS环境variabe设置为指向~/.config/gcloud/application_default_credentials.json

阅读有关服务帐户与用户帐户的主题的here for more。对于它的值(value),Terraform文档明确建议不要使用application-default login:

This approach isn't recommended- some APIs are not compatible with credentials obtained through gcloud



同样,GCP docs声明以下内容:

Important: For almost all cases, whether you are developing locally or in a production application, you should use service accounts, rather than user accounts or API keys.

关于google-cloud-platform - 具有Terraform的Google Cloud凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57453468/

相关文章:

firebase - 如何使用 Firebase 存储实现 CDN?

github - Google Cloud Builder 触发器给出 "No such blob"错误

azure - 错误 "GroupsClient.BaseClient.Post() An invalid operation was included in the following modified references: ' Members'"是什么意思?

c# - 如何连续监听 ASP.NET Core 应用程序中的 Pub/Sub 消息?

google-cloud-platform - 如何更改 google dataprep 的数据流作业运行的区域/区域

amazon-web-services - Kubeadm 为什么我的节点没有显示,虽然 kubelet 说它加入了?

azure - 使用 Terraform 从 Azure 上的应用程序服务的出站 IP 进行 MySQL 防火墙规则

google-cloud-platform - 无法将 `google_dns_record_set` 从 count 转换为 for_each

google-cloud-platform - 如何使用 terraform 停止 GCP vm 实例

terraform - 如何安装多个或两个版本的 Terraform?