Skip to content

Run multiple provider versions

Abner Garcia edited this page Oct 7, 2021 · 7 revisions

In order to run multiple versions of the same provider (in this case, mongodbatlas) we will need to download the provider(s) for local use, you can use terraform providers mirror for this purpose. For this example, we want to migrate to the new mongodbatlas v1.0.1 provider gradually from mongodbatlas v0.7.0

We need to create a folder where to put our providers, for example purposes we create a folder struct like this

project
│   main.tf  <- contains terraform definition for resources, datasources, etc.  
│   config.tfrc  <- configuration to use in terraform
└───providers    <- here we are going to add the local provider
│   └──registry.terraform.io
│      └──mongodb
|         └──mongodbatlas-new  <- we added the sufix `new` to indicate that this contains the latest version
|            └──1.0.1
|               └──darwin_amd64
|                  └──terraform-provider-mongodbatlas-new_v1.0.7  <- we added the sufix `old` to indicate that this contains the latest version

main.tf

The header of the file contains 2 providers, one of them is the original provider provided by terraform registry (mongodbatlas) and the other one is the one that we want to use locally, we need to change the name according of how we named the downloaded provider, in this case, we use the new sufix to be sure that we are using the latest version of the provider (we don't need to specify the version of this provider, due we only downloaded the v1.0.1

terraform {
  required_providers {
    mongodbatlas = {
      source = "mongodb/mongodbatlas"
      version = "0.7.0"
    }
    mongodbatlas-new = {
      source = "mongodb/mongodbatlas-new"
    }
  }
}

Inside the resource definition that are only contained in the latest provider, we need to add the property provider = <Name of the provider, in this case, for the resource mongodbatlas_private_endpoint (that are only available in version 0.7.0) we set the property provider = mongodbatlas-old to specify that this resource is going to be run from the old version

variable "project_id" {
  type    = string
  default = "<default value for project id>"
}

resource "mongodbatlas_private_endpoint" "test" {
  project_id    = var.project_id
  provider_name = "AWS"
  region        = "us-east-1"
}

resource "mongodbatlas_privatelink_endpoint" "test" {
  provider = mongodbatlas-new
  project_id    = var.project_id
  provider_name = "AWS"
  region        = "us-west-1"
}

config.tfrc

In this file we are going to specify where is located our folder with the downloaded providers, in the example, we downloaded the latest provider inside providers folder

provider_installation {

  filesystem_mirror {
    path    = "./providers"
  }
  direct {}
}

In order to terraform use this file as configuration, we need to set the env var TF_CLI_CONFIG_FILE before use terraform, like this:

export TF_CLI_CONFIG_FILE=<PATH TO FILE>/config.tfrc

Run

With all this steps, we only need to run terraform init:

Initializing the backend...

Initializing provider plugins...
- Finding mongodb/mongodbatlas versions matching "0.7.0"...
- Finding latest version of mongodb/mongodbatlas-new...
- Installing mongodb/mongodbatlas v0.7.0...
- Installed mongodb/mongodbatlas v0.7.0 (signed by a HashiCorp partner, key ID 2A32ED1F3AD25ABF)
- Installing mongodb/mongodbatlas-new v1.0.1...
- Installed mongodb/mongodbatlas-new v1.0.1 (unauthenticated)

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

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
commands will detect it and remind you to do so if necessary.

and if we use terraform plan, we are going to see that terraform plan to deploy resources from both provider versions:

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the
following symbols:
  + create

Terraform will perform the following actions:

  # mongodbatlas_private_endpoint.test will be created
  + resource "mongodbatlas_private_endpoint" "test" {
      + endpoint_service_name = (known after apply)
      + error_message         = (known after apply)
      + id                    = (known after apply)
      + interface_endpoints   = (known after apply)
      + private_link_id       = (known after apply)
      + project_id            = "612e501fab616866782e296e"
      + provider_name         = "AWS"
      + region                = "us-east-1"
      + status                = (known after apply)
    }

  # mongodbatlas_privatelink_endpoint.test will be created
  + resource "mongodbatlas_privatelink_endpoint" "test" {
      + endpoint_service_name            = (known after apply)
      + error_message                    = (known after apply)
      + id                               = (known after apply)
      + interface_endpoints              = (known after apply)
      + private_endpoints                = (known after apply)
      + private_link_id                  = (known after apply)
      + private_link_service_name        = (known after apply)
      + private_link_service_resource_id = (known after apply)
      + project_id                       = "612e501fab616866782e296e"
      + provider_name                    = "AWS"
      + region                           = "us-west-1"
      + status                           = (known after apply)
    }

Plan: 2 to add, 0 to change, 0 to destroy.

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you
run "terraform apply" now.