Terraform installing multiple versions of azurerm provider - azure

We've been using older versions of both Terraform and the azurerm provider and I'm trying to update the code for newer (for us) versions of each, in this case:
Terraform: v0.13.7
azurerm: v2.25
As part of the recoding, I'm switching to using the Terraform provider block:
terraform {
required_version = "~> 0.13"
required_providers {
azurerm = {
version = "= 2.25.0"
source = "hashicorp/azurerm"
}
}
}
provider azurerm {
skip_provider_registration = true
features {}
}
Terraform is downloading v2.25 of the provider but also the most version v2.67:
$ terraform init
Initializing the backend...
Initializing provider plugins...
- terraform.io/builtin/terraform is built in to Terraform
- Finding hashicorp/azurerm versions matching "2.25.0"...
- Finding latest version of -/azurerm...
- Installing hashicorp/azurerm v2.25.0...
- Installed hashicorp/azurerm v2.25.0 (signed by HashiCorp)
- Installing -/azurerm v2.67.0...
- Installed -/azurerm v2.67.0 (signed by HashiCorp)
So far this doesn't seem to be an issue, but I cannot understand why it's downloading multiple versions. Could it be because, in another code file which defines the backend (we use Azure storage for Terraform state), it's seeing that "azurerm" reference and treating it as a "new" one?
terraform {
backend azurerm {
container_name = "terraforminfra-v2"
key = "state/postgres.tfstate"
}
}

OK, I figured it out ... the "old" reference to the provider was still in the Terraform state, which I think is what was triggering the download of the most current version.

Related

Terraform 1.x Module - Provider - Cloudflare

When I ran init:
Initializing modules...
Initializing the backend...
Initializing provider plugins...
Finding cloudflare/cloudflare versions matching "~> 3.0"...
Finding latest version of hashicorp/cloudflare...
Installing cloudflare/cloudflare v3.31.0...
Installed cloudflare/cloudflare v3.31.0 (signed by a HashiCorp partner, key ID DE413CEC881C3283)
Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/cli/plugins/signing.html
Error: Failed to query available provider packages
Could not retrieve the list of available versions for provider hashicorp/cloudflare: provider registry registry.terraform.io does not have a provider named
registry.terraform.io/hashicorp/cloudflare
Did you intend to use cloudflare/cloudflare? If so, you must specify that source address in each module which requires that provider. To see which modules are currently
depending on hashicorp/cloudflare, run the following command:
terraform providers
Module:
cat dns.tf
module "dns" {
source = "./dns"
}
Definition:
cat cloudflare.tf
terraform {
required_providers {
cloudflare = {
source = "cloudflare/cloudflare"
version = "~> 3.0"
}
}
}
provider "cloudflare" {
email = "CLOUDFLARE_EMAIL"
api_key = "CLOUDFLARE_API_KEY"
}

Terraform won't upgrade provider

I am trying to upgrade the terraform kubernetes provider to the latest version 2.7.1. The following is an excerpt from the current terraform configuration:
terraform {
required_version = ">= 0.14.9"
required_providers {
aws = {
source = "hashicorp/aws"
version = "3.60.0"
}
kubernetes = {
source = "hashicorp/kubernetes"
version = "2.5.0" # Trying to change this...
}
helm = {
source = "hashicorp/helm"
version = "2.3.0"
}
...
}
}
Following these instructions I tried:
to change the version to >= 2.5.0;
to change the version to 2.7.1 (or even 2.5.1, just for the test);
and run terraform init -upgrade. Unfortunately, attempt (1) has no effect, and (2) gives an error:
Upgrading modules...
[...]
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/kubernetes versions matching "2.5.0, >= 2.5.1"...
- Finding hashicorp/helm versions matching ">= 2.3.0, 2.3.0"...
[...]
- Using previously-installed hashicorp/helm v2.3.0
[...]
╷
│ Error: Failed to query available provider packages
│
│ Could not retrieve the list of available versions for provider hashicorp/kubernetes: no available releases match the given constraints 2.5.0, >= 2.5.1
I tried to init a new project (in an empty directory) with the above, and it correctly installs version 2.7.1, therefore I presume the problem is with the pre-existing terraform state. How can I upgrade the provider?

How do I change the Terraform Provider.aws version

I have a pipeline in Jenkins that allows me to change my AWS infrastructure with Terraform. The build failed yesterday because and I noticed the provider.aws changed from:
provider.aws: version = "~> 3.15"
to
provider.aws: version = "~> 3.20".
I understand that this includes breaking changes.
Does anyone know how I can manually change that number manually back to 3.15?
In Terraform 0.11 it was done with version attribute when the provider was declared, e.g.:
provider "aws" {
version = "3.15"
}
These days e.g. Terraform 0.13 it is done in the required_providers section, e.g.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "3.15"
}
}
}
Not sure, but I think in Terraform 0.12 both could be used.

Error while installing hashicorp/custom: provider registry

I assume I am using the latest version of azurerm:
provider "azurerm" {
version = "=2.34.0"
features {}
}
As soon as I add this resource to my tf script:
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/policy_assignment
I get this error when I do terraform init:
>terraform init
Initializing the backend...
Initializing provider plugins...
- Finding latest version of hashicorp/custom...
- Finding hashicorp/azurerm versions matching "2.34.0"...
- Installing hashicorp/azurerm v2.34.0...
- Installed hashicorp/azurerm v2.34.0 (signed by HashiCorp)
Error: Failed to install provider
Error while installing hashicorp/custom: provider registry
registry.terraform.io does not have a provider named
registry.terraform.io/hashicorp/custom
Am I missing any custom terraform provider? Looking at the Terraform documentation in the link above, I expect the azurerm_policy_definition resource must be included n the azurerm
Thanks to #ChristianPearce who deserves the credit for the answer.
This is a common and potentially misleading error.
There could me many scripting issues that cause this error.
In my case my resource name had typo in it like below:
resource "azurerm_virtual_network_typo_in_type" "main" {
This could also be a problem with terraform sub-modules as discussed in https://github.com/hashicorp/terraform/issues/25602.
For community providers, every module requires a required_providers block with an entry specifying the provider source.
So basically you need to have this in all your modules and in your main tf-script (replace the custom-prov-name by you actual provider):
terraform {
required_providers {
custom-prov-name = {
source = ".../custom-prov-name"
}
}
}

How to use the IBM Cloud Provider plug-in for Terraform with Terraform 0.13?

Terraform 0.13 just came out (https://www.hashicorp.com/blog/announcing-hashicorp-terraform-0-13/) and it changes how to work with 3rd party providers (https://www.terraform.io/upgrade-guides/0-13.html#explicit-provider-source-locations).
I'm encountering an error when running terraform init:
$ terraform init
Initializing the backend...
Initializing provider plugins...
- Finding latest version of hashicorp/ibm...
Error: Failed to install provider
Error while installing hashicorp/ibm: provider registry registry.terraform.io
does not have a provider named registry.terraform.io/hashicorp/ibm
This used to work before with Terraform 0.12.29 and the IBM provider 1.10.0.
Here are the instructions for Linux and the current versions of Terraform and the IBM provider:
Install Terraform
Download Terraform 0.13
wget https://releases.hashicorp.com/terraform/0.13.0/terraform_0.13.0_linux_amd64.zip
Unzip the provider
unzip terraform_0.13.0_linux_amd64.zip
Move it to a folder in your path, such as:
mv terraform /usr/local/bin/
Ensure the version is 0.13
terraform version
Install the IBM provider
Create the folder where the plugin will be put:
mkdir -p ~/.terraform.d/plugins/localdomain/provider/ibm/1.10.0/linux_amd64
Get the provider:
wget https://github.com/IBM-Cloud/terraform-provider-ibm/releases/download/v1.10.0/terraform-provider-ibm_1.10.0_linux_amd64.zip
Unzip the provider:
unzip terraform-provider-ibm_1.10.0_linux_amd64.zip
Move the provider to the folder previously created:
mv terraform-provider-ibm_v1.10.0 ~/.terraform.d/plugins/localdomain/provider/ibm/1.10.0/linux_amd64
Test with a simple Terraform file
Create main.tf
terraform {
required_providers {
ibm = {
source = "localdomain/provider/ibm"
version = "1.10.0"
}
}
}
variable ibmcloud_api_key {
}
provider "ibm" {
ibmcloud_api_key = var.ibmcloud_api_key
}
resource ibm_resource_group new_group {
name = "created-by-terraform"
}
Create terraform.tfvars and fill in your IBM Cloud API key:
ibmcloud_api_key="REPLACE_WITH_YOUR_KEY"
Initialize Terraform
terraform init
will result in:
Initializing the backend...
Initializing provider plugins...
- Finding localdomain/provider/ibm versions matching "1.10.0"...
- Installing localdomain/provider/ibm v1.10.0...
- Installed localdomain/provider/ibm v1.10.0 (unauthenticated)
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
commands will detect it and remind you to do so if necessary.
And apply
terraform apply
will result in:
...
Enter a value: yes
ibm_resource_group.new_group: Creating...
ibm_resource_group.new_group: Creation complete after 2s [id=2142c8122344458d59b8729708464a]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Happy terraforming!
The IBM Provider is now published to the repository so you can use the new terraform 13 provider syntax such as:
terraform {
required_version = ">= 0.13"
required_providers {
ibm = {
source = "IBM-Cloud/ibm"
version = "1.11.2"
}
}
}
terraform {
required_version = ">= 0.13.3"
required_providers {
ibm = {
source = "ibm-cloud/ibm"
version = "1.12.0"
}
}
}
This will give you the latest version.

Resources