如何在我的 Apple Silicon Macbook Pro 上为 Google 提供商获取 `terraform init` 到 运行?

How can I get `terraform init` to run on my Apple Silicon Macbook Pro for the Google Provider?

当我在我的 Apple Silicon macbook pro 上 运行 terraform init 我的 Google Cloud Platform 项目时,我得到这个错误。

Provider registry.terraform.io/hashicorp/google v3.57.0 does not have a package available for your current platform, darwin_arm64.

我该如何解决这个问题?我以为 Rosetta2 模拟器会勾选这个框,但是唉...

大多数供应商已经有更新版本的软件包。 您可以通过以下方式更新提供者: terraform init -upgrade 如果这条路线不适合您或者不能解决问题,请查看下面的答案。

从头开始构建 Terraform 的 GCP 提供程序!我修改了这个演练。 https://github.com/hashicorp/terraform/issues/27257#issuecomment-754777716

brew install --build-from-source terraform

这也将安装 Golang(从这个 post 开始似乎可以正常工作)

git clone https://github.com/hashicorp/terraform-provider-google.git
cd terraform-provider-google
git checkout v3.22.0
go get -d github.com/pavius/impi/cmd/impi
make tools
go fmt
make build

以下目录可能不存在,所以让我们创建它并复制我们刚刚构建的二进制文件。

mkdir -p ${HOME}/.terraform.d/plugins/registry.terraform.io/hashicorp/google/3.22.0/darwin_arm64
cp ${HOME}/go/bin/terraform-provider-google ${HOME}/.terraform.d/plugins/registry.terraform.io/hashicorp/google/3.22.0/darwin_arm64

请注意,如果您尚未定义 ${GOPATH},则 ${HOME}/go 是您的 golang 安装所在的位置。如果这样做,请修改上述命令以说明新构建二进制文件的位置。

cp ${GOPATH}/bin/terraform-provider-google ${HOME}/.terraform.d/plugins/registry.terraform.io/hashicorp/google/3.22.0/darwin_arm64

回到我的项目后瞧!

➜ terraform init

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/google...
- Installing hashicorp/google v3.22.0...
- Installed hashicorp/google v3.22.0 (unauthenticated)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other

好的。所以,我''found''是我问题的答案。您很可能像我一样使用如下所示的 main.tf 配置:

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

provider "google" {
  credentials = file("foo.json")

  project = "foo"
  region  = "us-central1"
  zone    = "us-central1-c"
}

resource "google_compute_network" "vpc_network" {
  name = "terraform-network"
}

好吧,放弃这部分,你不需要它:

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

如果仍然无法正常工作,请重新安装 TERRAFORM 并为您的项目从一个全新的干净目录开始。

希望这对某些人有所帮助。

顺便说一句,我的 Terraform 版本

Terraform v1.1.3
于 darwin_arm64
+ 提供商 registry.terraform.io/hashicorp/google v4.6.0