Thursday 5 May 2022

Terraform Providers

 



Terraform supports hundreds of providers via their plugins (terms plugins and providers are often used interchangeably). They are distributed by Hashicorp's Terraform registry. There are 3 tiers of providers:
  • official: owned and maintained by Hashicorp. 
    • aws
    • gcp
    • azure
    • local
  • verified: owned and maintained by 3rd party company that is Hashicorp's partner
    • bigip by F5Networks
    • heroku by Heroku
    • digitalocean by DigitalOcean
    • linode by Linode
  • community: published and maintained by individual contributors
 
linode provider is maintained by Linode and is thus a verified provider



Here are some useful commands related to providers:

terraform providers - prints all providers used in the configuration directory

Example:

$ terraform providers

Providers required by configuration:
.
├── provider[registry.terraform.io/hashicorp/local]
└── provider[registry.terraform.io/hashicorp/aws]

terraform providers mirror /path/to/destination/dir - to copy provider plugins needed for the current configuration directory to another directory
 
 
Note: If you haven't run terraform init yet, terraform providers might return something like this:
 
$ terraform providers

Error: Backend initialization required, please run "terraform init"

│ Reason: Initial configuration of the requested backend <backend_name>│
│ The "backend" is the interface that Terraform uses to store state,
│ perform operations, etc. If this message is showing up, it means that the
│ Terraform configuration you're using is using a custom configuration for
│ the Terraform backend.

│ Changes to backend configurations require reinitialization. This allows
│ Terraform to set up the new configuration, copy existing state, etc. Please run
│ "terraform init" with either the "-reconfigure" or "-migrate-state" flags to
│ use the current configuration.

│ If the change reason above is incorrect, please verify your configuration
│ hasn't changed and try again. At this point, no changes to your existing
│ configuration or state have been made.
╵ 


Version Constraints

 
By default TF will download the latest version of plugins required by resources specified in the configuration file (.tf file). To use some particular version of the plugin, we need to use a terraform block which is used to configure settings related to Terraform itself. Inside it, we need to use another block, called terraform_providers inside which we can list all providers and their settings:


terraform {
  required_providers {
    local = {
      source = "hashicorp/local"
      version = "2.2.2"
    }
  }
}


We can get this snippet for a given provider if we go to https://registry.terraform.io/browse/providers, select desired provider and click on USE PROVIDER button in the upper right corner:

 

Version constraint can be specified in multiple ways:
 
version = "!= 2.0.0" - get the available version before 2.0.0 but not 2.0.0
version = "< 2.0.0"
version = "> 2.0.0"
version = "> 1.5.0, < 2.0.0, != 1.7.0" 
 
Pessimistic constraint operator:
 
version = "~> 1.5" - get the version 1.5, 1.6 ...up to 1.9
version = "~> 1.5.0" - get the version 1.5.0, 1.5.1...up to 1.5.9

---

No comments: