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

6

28

When I run terraform init for my Google Cloud Platform project on my Apple Silicon macbook pro I get this error.

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

How can I work around this? I thought that the Rosetta2 emulator would check this box, but alas...

Expressionism answered 19/2, 2021 at 17:0 Comment(0)
E
17

Most providers already have packages available in newer versions. You can update the provider via: terraform init -upgrade If this route is not acceptable for you or if it does not solve the problem, look at the answer below.

Build Terraform's GCP provider from scratch! I modified this walkthrough. https://github.com/hashicorp/terraform/issues/27257#issuecomment-754777716

brew install --build-from-source terraform

This will install Golang as well (and that appears to be working as of this 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

The following directory probably does not already exist so lets create it and copy the binary we just built.

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

Note that ${HOME}/go is where your golang install will be located if you don't already have ${GOPATH} already defined. If you do, then modify the above commands to account for the location of your new build binaries.

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

After going back to my project voila!

➜ 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

Expressionism answered 19/2, 2021 at 17:0 Comment(6)
This is a great! One thing to note, is that you may need to specify other providers when cloning and copying. For example: git clone https://github.com/hashicorp/terraform-provider-aws.git and ${HOME}/go/bin/terraform-provider-awsHymettus
@DevinCairns that is covered in the GitHub issue link, but isn’t precisely relevant to the title of the post which is limited to the Google Provider.Expressionism
@Expressionism Thank you so much, It helped me a lot.Diggs
I get ./tools.go You can use the command: make fmt to reformat code. make: *** [fmtcheck] Error 1 and running make fmt does not fix it.Raja
Same here. Sad. Could it be a GO version issue ?Postrider
See this comment for a simpler way to do this using m1-terraform-provider-helperSkite
G
34

Build Terraform from scratch by using the tfenv package, which can build a specific version adapted to the platform architecture.

I ran the following to install a version that works under my M1 Macbook (version 1.3.3 in this case):

brew uninstall terraform
brew install tfenv
TFENV_ARCH=amd64 tfenv install 1.3.3
tfenv use 1.3.3
Gothenburg answered 22/10, 2022 at 16:2 Comment(4)
This suggestion solved my problem. I am using Macbook pro 2023. ThanksPlaytime
See this comment for a simpler way to do this using m1-terraform-provider-helperSkite
I still get open ~/.terraform.d/plugins/darwin_arm64: no such file or directory after using tfenv to install Terraform 1.6.5 with these exact instructions...Rote
Absolute life saver thank you!Grouping
E
17

Most providers already have packages available in newer versions. You can update the provider via: terraform init -upgrade If this route is not acceptable for you or if it does not solve the problem, look at the answer below.

Build Terraform's GCP provider from scratch! I modified this walkthrough. https://github.com/hashicorp/terraform/issues/27257#issuecomment-754777716

brew install --build-from-source terraform

This will install Golang as well (and that appears to be working as of this 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

The following directory probably does not already exist so lets create it and copy the binary we just built.

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

Note that ${HOME}/go is where your golang install will be located if you don't already have ${GOPATH} already defined. If you do, then modify the above commands to account for the location of your new build binaries.

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

After going back to my project voila!

➜ 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

Expressionism answered 19/2, 2021 at 17:0 Comment(6)
This is a great! One thing to note, is that you may need to specify other providers when cloning and copying. For example: git clone https://github.com/hashicorp/terraform-provider-aws.git and ${HOME}/go/bin/terraform-provider-awsHymettus
@DevinCairns that is covered in the GitHub issue link, but isn’t precisely relevant to the title of the post which is limited to the Google Provider.Expressionism
@Expressionism Thank you so much, It helped me a lot.Diggs
I get ./tools.go You can use the command: make fmt to reformat code. make: *** [fmtcheck] Error 1 and running make fmt does not fix it.Raja
Same here. Sad. Could it be a GO version issue ?Postrider
See this comment for a simpler way to do this using m1-terraform-provider-helperSkite
P
16

Thanks for this repo: https://github.com/kreuzwerker/m1-terraform-provider-helper. This will remove the overhead of building and compiling from us.

Usage:

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

This will compile the provider for our arm_64 in the location:

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

For this to be working, these are my findings:-

  • Your Terraform version should be at least >= 1.0.2
  • You should delete the older checksums of the requested provider [template in my case] from the .terraform.lock.hcl
    • or try running m1-terraform-provider-helper lockfile upgrade which finds and updates these checksums
  • Please change your provider and version that you need to be installed in the terraform code.

Go ahead now !!!

Pandorapandour answered 17/11, 2022 at 14:25 Comment(3)
This should be the accepted answer. m1-terraform-provider-helper is a great tool that really simplifies solving this problem by managing the build process and the binary provider files. It even updates the Terraform lockfile using m1-terraform-provider-helper lockfile upgrade.Skite
This worked for me! Simple straight forward solution.Ami
This was a lifesaver. Thanks!Silverside
P
5

OK. So, I ''found'' the answer to my problem. You most probably, like me, use the main.tf config that look like this:

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

Well, DROP this part, you do not need it:

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

If it still not working, reinstall TERRAFORM and start from a new fresh and clean directory for your project.

Hope this can help some one.

BTW, my Terraform version

Terraform v1.1.3
on darwin_arm64
+ provider registry.terraform.io/hashicorp/google v4.6.0

Postrider answered 15/1, 2022 at 21:5 Comment(2)
Thanks, this helped me sort out my env, which is the same as yours. Regarding the provider version, it's not that "you do not need it", but rather that that the version you're requesting is not supported for your env. After running terraform init, and seeing which version is supported, I re-added the required_providers section with the latest version...Imminent
Thank's for that clarification update !Postrider
P
4

This worked for me:

# Remove your local lock file!
rm .terraform.lock.hcl

# And then -
brew install kreuzwerker/taps/m1-terraform-provider-helper
m1-terraform-provider-helper activate
m1-terraform-provider-helper install hashicorp/template -v v2.2.0
terraform init
Puzzler answered 4/4, 2023 at 10:42 Comment(0)
W
1

You can list the supported provider versions by running:

PROVIDER="hashicorp/google"
curl --silent https://registry.terraform.io/v1/providers/${PROVIDER}/versions | jq -r '.versions[] | select(.platforms[] | contains({os: "darwin", arch: "arm64"})) | .version'

Then just use one of the supported versions in your required_providers block in the Terraform code.

Woodard answered 1/5, 2023 at 14:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.