簡體   English   中英

如何讓 `terraform init` 在我的 Apple Silicon Macbook Pro 上為 Google Provider 運行?

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

當我在 Apple Silicon macbook pro 上為我的 Google Cloud Platform 項目運行terraform init時,出現此錯誤。

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

我該如何解決這個問題? 我以為 Rosetta2 模擬器會勾選這個框,但是唉......

從頭開始構建Terraform的另一種方法是使用tfenv package,它可以構建適應平台架構的特定版本。

我運行以下命令來安裝適用於我的 M1 Macbook 的版本(在我的例子中是版本 1.3.3):

brew uninstall terraform
brew install tfenv
TFENV_ARCH=amd64 tfenv install 1.3.3
tfenv use 1.3.3

大多數提供商已經提供了更新版本的軟件包。 您可以通過以下方式更新提供程序: terraform init -upgrade如果您無法接受此路線或無法解決問題,請查看下面的答案。

從頭開始構建 Terraform 的 GCP 提供程序。 我修改了這個演練。 https://github.com/hashicorp/terraform/issues/27257#issuecomment-754777716

brew install --build-from-source terraform

這也將安裝 Golang(這似乎在這篇文章中有效)

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

感謝這個 repo: https://github.com/kreuzwerker/m1-terraform-provider-helper 這將消除我們構建和編譯的開銷。

用法

brew install kreuzwerker/taps/m1-terraform-provider-helper
m1-terraform-provider-helper activate # (In case you have not activated the helper)
m1-terraform-provider-helper install hashicorp/template -v v2.2.0 # Install and compile

這將在以下位置為我們的 arm_64 編譯提供程序:

~/.terraform.d/plugins/registry.terraform.io/hashicorp/template/2.2.0/darwin_arm64

為了使它起作用,這些是我的發現:-

  • 您的 terraform 版本應該至少 >= 1.0.2
  • 您應該從.terraform.lock.hcl中刪除請求的提供者 [在我的例子中是模板] 的舊校驗和
  • 請在 terraform 代碼中更改您需要安裝的提供商和版本。

現在提前Go !!!

大多數提供商已經提供了更新版本的軟件包。

您可以通過以下方式更新提供程序:

terraform init -upgrade

如果這條路線不適合你或者沒有解決問題,請看reka18的回答。 並且可能在此提供程序的存儲庫中創建一個問題以請求此 package 以便其他人將來可以從中受益。

好的。 所以,我“找到”了我的問題的答案。 您很可能像我一樣使用如下所示的 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 上
+ 提供者注冊中心.terraform.io/hashicorp/google v4.6.0

您能否為 Azure 平台提供類似的步驟。 我在我的 Macbook 上遇到了類似的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM