Aller au contenu

Terraform#

Some useful informations around terraform usage.

Provider source address edition#

In a previous toolbox version we re packaged every provider by overriding their provider-source-address attribute. It impacts terraform objects whithin states which registered their provider path. When a shell is updated it providers may have a new provider source address.

In such cases trackbone terraform shell.nix may fail with such error messages

terraform init
│ Error: Failed to query available provider packages
│
│ Could not retrieve the list of available versions for provider toolbox/vault: provider registry.terraform.io/toolbox/vault was not found in any of the search locations
│
│   - /nix/store/hxpgcq849g6299mg4mv989xmjz6ypq3p-terraform-1.7.1/libexec/terraform-providers
╵

tree /nix/store/hxpgcq849g6299mg4mv989xmjz6ypq3p-terraform-1.7.1/libexec/terraform-providers
/nix/store/hxpgcq849g6299mg4mv989xmjz6ypq3p-terraform-1.7.1/libexec/terraform-providers
└── registry.terraform.io
    ├── hashicorp
       ├── vault
          └── ...
              

In such a scenario 2 things must be done to fix the issue:

  • fix the lib code: configurations reference their providers within an object called terraform The source attribute is impacted by provider-source-address atribute. Thus terraform blocks shoudle be edited this way:
 terraform {
   required_providers {
     vault = {
-      source                = "toolbox/vault"
+      source                = "hashicorp/vault"
       configuration_aliases = [vault.src, vault.dest]
     }
   }
  • fix the state:
terraform state replace-provider registry.terraform.io/toolbox/vault registry.terraform.io/hashicorp/vault

It can also be fixed automatically with trackbone pre_tasks, state files updates are easy:

let MyConfig= "keycloak-vault-cloud"
envs: [string]: configurations: [MyConfig]: run: actions: pre_plan: """
    terraform init &>/dev/null || true # will fail but is mandatory
    terraform state replace-provider registry.terraform.io/toolbox/aws registry.terraform.io/hashicorp/aws
    terraform state replace-provider registry.terraform.io/toolbox/aws registry.terraform.io/hashicorp/s
    terraform state replace-provider registry.terraform.io/toolbox/vault registry.terraform.io/hashicorp/vault 
    terraform state replace-provider registry.terraform.io/toolbox/aws registry.terraform.io/hashicorp/aws 
    terraform state replace-provider registry.terraform.io/toolbox/random registry.terraform.io/hashicorp/random 
    terraform state replace-provider registry.terraform.io/toolbox/keycloak registry.terraform.io/mrparkers/keycloak 
    terraform state replace-provider registry.terraform.io/toolbox/azurerm registry.terraform.io/hashicorp/azurerm 
    terraform state replace-provider registry.terraform.io/toolbox/azureread registry.terraform.io/hashicorp/azureread 
    terraform state replace-provider registry.terraform.io/toolbox/kubernetes registry.terraform.io/hashicorp/kubernetes 
    terraform state replace-provider registry.terraform.io/toolbox/kubectl/ registry.terraform.io/gavinbunney/kubectl 
    terraform state replace-provider registry.terraform.io/toolbox/flexibleengine registry.terraform.io/flexibleenginecloud/flexibleengine 
    terraform state replace-provider registry.terraform.io/toolbox/azureread registry.terraform.io/hashicorp/azureread 
    terraform state replace-provider registry.terraform.io/toolbox/azuread registry.terraform.io/hashicorp/azuread 
    terraform state replace-provider registry.terraform.io/toolbox/huaweicloud registry.terraform.io/huaweicloud/huaweicloud 
    terraform state replace-provider registry.terraform.io/toolbox/controltower registry.terraform.io/idealo/controltower 
    terraform state replace-provider registry.terraform.io/toolbox/helm registry.terraform.io/hashicorp/helm 
    terraform state replace-provider registry.terraform.io/toolbox/gitlab registry.terraform.io/gitlabhq/gitlab 
    terraform state replace-provider registry.terraform.io/toolbox/cloudinit registry.terraform.io/hashicorp/cloudinit 
    terraform state replace-provider registry.terraform.io/toolbox/harbor registry.terraform.io/goharbor/harbor 
"""

Note it is safe to run such commands even if the state references no objects of a provider type.