I'm using tfenv to switch terraform versions.Lately I'm getting this error and i'm not sure how to solve this.
Can someone pls let me know what is happening here, why its not able to detect min-required version?
Error:
tfenv install min-required && tfenv use min-required && terraform init -backend=false ./stage; terraform validate -var="api_key=xxxx" ./stage
Error: Could not determine required_version based on your terraform sources.
Make sure at least one of your *tf files includes a required version section like
terraform {
required_version = ">= 0.0.0"
}
see https://www.terraform.io/docs/configuration/terraform.html for details
tfenv-min-required failed
grep: brackets ([ ]) not balanced
No versions matching 'min-required' found in remote
Makefile:
DUTY_VAR=-var=api_key=xxxx test: tfenv install min-required && tfenv use min-required && terraform init -backend=false ./stage; terraform validate ${DUTY_VAR} ./stage
Terraform folder/file structure:
|-terraform (directory)
|-modules ( directory)
| - main.tf
| - variables.tf
| - versions.tf
|
|-stage(root-module)(directory)
| - backend.tf
| - main.tf
| - .terraform-version
| - providers.tf
version.tf
terraform {
required_version = "0.11.13"
}
backend.tf
terraform {
required_version = "0.11.13"
backend "s3" {
bucket = "nonprod"
key = "tfstate/terraform.tfstate"
dynamodb_table = "terraform-state-lock"
region = "us-east-1"
encrypt = true
role_arn = "arn:aws:iam::zzzzzzz:role/yyy"
}
}
There are two parts in this answer as explained below,
Part 1
As specified in the error, please try to specify the terraform version in required_version setting as below,
required_version = ">= 0.11.13"
Please note the >=
Part 2
As per the Terraform documentation, modules should constrain only their minimum allowed versions of Terraform and providers, such as >= 0.12.0
Please refer the Terraform Version Constraint Documentation
So instead of Terraform version 0.11.13, try with version 0.12.0
Related
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?
When I execute terraform init I have the following output :
Initializing modules...
- compute in modules\compute
- private_network in modules\private_network
- public_ip in modules\public_ip
- route in modules\route
Initializing the backend...
Initializing provider plugins...
- Finding terraform-provider-openstack/openstack versions matching "~> 1.35.0"...
- Finding latest version of hashicorp/openstack...
- Installing terraform-provider-openstack/openstack v1.35.0...
- Installed terraform-provider-openstack/openstack v1.35.0 (self-signed, key ID 4F80527A391BEFD2)
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/plugins/signing.html
Error: Failed to query available provider packages
Could not retrieve the list of available versions for provider
hashicorp/openstack: provider registry registry.terraform.io does not have a
provider named registry.terraform.io/hashicorp/openstack
And this is my provider.tf file (Inspired from the official Terraform's documentation):
# Define required providers
terraform {
required_version = ">= 0.14.0"
required_providers {
openstack = {
source = "terraform-provider-openstack/openstack"
version = "~> 1.35.0"
}
}
}
# Configure the OpenStack Provider
provider "openstack" {
user_name = "username"
tenant_name = "tenantname"
password = "mypasswd"
auth_url = "http://my.openstack.lan:5000"
region = "RegionOne"
}
# Create a web server
#resource "openstack_compute_instance_v2" "test-server" {
# ...
#}
And when I check the version I have this:
Terraform v0.14.2
Your version of Terraform is out of date! The latest version
is 1.0.3. You can update by downloading from https://www.terraform.io/downloads.html
This is when I run terraform apply
Error: Could not load plugin
Plugin reinitialization required. Please run "terraform init".
Plugins are external binaries that Terraform uses to access and manipulate
don't satisfy the version constraints, or are otherwise incompatible.
Terraform automatically discovers provider requirements from your
configuration, including providers used in child modules. To see the
requirements and constraints, run "terraform providers".
2 problems:
- Failed to instantiate provider "registry.terraform.io/hashicorp/openstack"
to obtain schema: unknown provider "registry.terraform.io/hashicorp/openstack"
- Failed to instantiate provider
"registry.terraform.io/terraform-provider-openstack/openstack" to obtain
schema: unknown provider
And finally terraform providers
Providers required by configuration:
.
├── provider[registry.terraform.io/terraform-provider-openstack/openstack] ~> 1.35.0
├── module.compute
│ └── provider[registry.terraform.io/hashicorp/openstack]
├── module.private_network
│ └── provider[registry.terraform.io/hashicorp/openstack]
├── module.public_ip
│ └── provider[registry.terraform.io/hashicorp/openstack]
└── module.route
└── provider[registry.terraform.io/hashicorp/openstack]
To manually manage provider versions, use terraform mirror.
Basically: create provider path, download && install provider, set terraform mirror path.
# mkdir -p /root/.local/share/terraform/plugins/registry.terraform.io/terraform-provider-openstack/openstack/1.40.0/linux_amd64/
# curl -L https://releases.hashicorp.com/terraform-provider-openstack/1.40.0/terraform-provider-openstack_1.40.0_linux_amd64.zip -o terraform-provider-openstack.zip
# unzip terraform-provider-openstack.zip
# mv terraform-provider-openstack_v1.40.0 /root/.local/share/terraform/plugins/registry.terraform.io/terraform-provider-openstack/openstack/1.40.0/linux_amd64/
# terraform providers mirror /root/.local/share/terraform/plugins/
Once done, when terraform init, it will load provider from this path.
(I use this path because in previous versions of TF, providers were there).
In addition, I've created a docker image when I deploy TF on openstack from Gitlab. Here is the Dockerfile:
FROM hashicorp/terraform:0.14.7
RUN apk update && \
apk upgrade && \
apk add --no-cache \
curl && \
mkdir -p /root/.local/share/terraform/plugins/registry.terraform.io/terraform-provider-openstack/openstack/1.40.0/linux_amd64/ && \
curl -L https://releases.hashicorp.com/terraform-provider-openstack/1.40.0/terraform-provider-openstack_1.40.0_linux_amd64.zip -o terraform-provider-openstack.zip && \
unzip terraform-provider-openstack.zip && \
mv terraform-provider-openstack_v1.40.0 /root/.local/share/terraform/plugins/registry.terraform.io/terraform-provider-openstack/openstack/1.40.0/linux_amd64/ && \
terraform providers mirror /root/.local/share/terraform/plugins/
ENTRYPOINT /bin/sh
There is a hack to avoid such error:
You can define openstack provider as blank and source your openrc on the same terminal where you are going to execute your terraform plan/apply command ->
provider "openstack" {}
Your openrc.sh should contain that as well
read -sr OS_PASSWORD_INPUT
export TF_VAR_os_password=$OS_PASSWORD_INPUT
My provider.tf has the below lines:
terraform {
required_providers {
openstack = {
source = "terraform-provider-openstack/openstack"
version = "~> 1.42.0"
}
}
}
provider "openstack" {}
I am using os_password in my template. In that case I used TF_VAR_os_password as environment variable and within variable.tf I am just defining the variable name as:
variable "os_password" {
sensitive = true
}
Terraform is trying to change a version (0.2.2) of my helm_release resource but this version are not in my code neither in Terraform state:
$ terraform version
Terraform v0.14.2
+ provider registry.terraform.io/hashicorp/aws v3.20.0
+ provider registry.terraform.io/hashicorp/helm v1.3.2
+ provider registry.terraform.io/hashicorp/kubernetes v1.13.3
+ provider registry.terraform.io/hashicorp/null v3.0.0
$ terraform plan
...
...
# module.k8s_cluster.helm_release.oauth_proxy will be updated in-place
~ resource "helm_release" "oauth_proxy" {
id = "oauth"
name = "oauth"
~ version = "3.2.0" -> "0.2.2"
# (25 unchanged attributes hidden)
}
Coud you help me, please? Any help would be appreciated.
There were depreacated Helm repositories on my code. I changed it and now it's working.
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.
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.