How to use multiple GCP providers in Terraform

Reading Time: 3 minutes

Terraform provides us a way to use multiple configurations for the same providers to deploy our resources. With terraform, we can make use of aliases to create multiple configurations for the same provider.

Providers in terraform

Providers are the heart of terraform. Terraform uses providers to interface/sync between the Terraform engine and the supported cloud platform.

Terraform supports a large number of providers. e.g. AWS, GCP, etc

Using Single Provider in terraform

In terraform, we generally use a single provider configuration. Provider configuration is stored inside the “provider block” in the root of the directory.

Let’s see how a provider block looks like.

provider "google" {
  project     = "my-project-id"
  region      = "us-central1"
  zone        = "us-central1-c"
}

With the above provider block configuration terraform will provision the infrastructure in the given project and region. By default, resources use a default provider configuration (one without an alias argument).

Multiple providers block

There can be many scenarios where we can use more than one provider block:

  • We can define multiple configurations of the provider, and select which one to use on a per-resource or per-module basis.
  • Create resources in different region within the same GCP account.
  • Create resources in different regions of different GCP account.

If you want to make one VM in us-east1 and another VM in us-west1 then you can make use of multiple providers block.

Demo

Let’s see how we can implement this:

First, let’s have a look at the directory structure of our code.

In provider.tf I have configured both the providers. Resource.tf contains the configuration of our VM.

Let’s now look inside the providers.tf file

Here we have created two provider blocks, one is to create resources in the us-central1 region and another one to create in us-east1. The first one is the default one(the one without an alias).

Now, how will terraform know when to create resources in us-central1 or us-east1?

By default, resources use a default provider configuration (one without an alias argument). However, we can use the alternate provider in a resource, data, or module by referencing it as <PROVIDER NAME>.<ALIAS>.

Let’s understand this by having a look at our resource.tf file

In the above file, we have defined two resources block. The first resource will be created in the us-central. However, the second resource will be created in the us-east1 because we are referencing the second resource to use the alternate provider’s block(with region us-east1)

Let’s apply the configurations now to verify.

So, we can clearly see in the above snapshot that the vm-2 is created in us-east1 and vm-1 in us-central.

Conclusion

In this blog, we learned how to use multiple configurations of the GCP provider to create resources in different regions. Similarly, we can also use multiple AWS providers.

Happy Learning!

Reference

Written by 

DevOps Engineer at Knoldus. Loves to solve new problems. Always eager to learn new technologies.

Leave a Reply