Terraform: How to add alias to a provider

Updated: February 4, 2024 By: Guest Contributor Post a comment

Introduction

Working with Terraform, a powerful tool from HashiCorp for building, changing, and versioning infrastructure safely and efficiently, can sometimes require managing multiple configurations for the same provider. This is where the concept of provider aliases comes into play, allowing you to instantiate a single provider multiple times with different configurations. This tutorial will guide you through the basics of adding an alias to a provider in Terraform, moving towards more advanced usages with practical examples.

Understanding Provider Aliases

Before diving into the specifics, let’s clarify what we mean by ‘provider’ and ‘alias’ in the context of Terraform. A provider is a plugin that Terraform uses to create and manage resources in a service. An alias, meanwhile, allows you to configure multiple instances of the same provider within a single Terraform plan.

Basic Configuration

To get started, you’ll need to understand how to define a provider with an alias. Consider this simple example where we define the AWS provider with a default (unnamed) instance and one with an alias:

provider "aws" {
  region = "us-west-2"
}

provider "aws" {
  alias  = "east"
  region = "us-east-1"
}

In the first block, we’ve defined the default AWS provider which will manage resources in the us-west-2 region. The second block introduces an alias named ‘east’, and it will manage resources in the us-east-1 region. To use the aliased provider, simply refer to it with its alias in your Terraform resources.

Advancing with Aliased Providers

As you become more familiar with using aliased providers, you may find yourself in situations where you need more complex configurations. For example, using multiple providers of the same type to manage resources in different accounts or regions simultaneously. Here is how you can achieve that:

provider "aws" {
  region = "us-west-2"
}

provider "aws" {
  alias  = "east"
  region = "us-east-1"
  profile = "my-second-account"
}

This configuration allows you to manage resources in two different AWS accounts. The ‘east’ aliased provider specifies a ‘profile’ parameter pointing to another AWS account. This is crucial for managing resources across multiple accounts securely and efficiently.

Using Providers with Modules

One of the strengths of Terraform is its modularity, allowing you to reuse configurations across projects. However, managing multiple provider instances within modules requires some additional considerations. Here’s how you can pass providers to a module:

module "vpc" {
  source = "./modules/vpc"

  providers = {
    aws = aws.east
  }
}

In this example, we’re passing the ‘east’ aliased AWS provider to a VPC module. This makes the aliased provider available within the module, allowing it to manage resources using the specified configuration.

Debugging and Outputs

When working with multiple providers, debugging and understanding the state of your infrastructure can become complex. Terraform provides powerful debugging capabilities that can help. Here’s a simple example of how you might use outputs to understand the configurations of your aliased providers:

output "east_provider_region" {
  value = aws_east.region
}

This assumes that ‘aws_east’ is a data source or other Terraform resource configured with the ‘east’ provider. Outputs like this one can be invaluable for debugging and ensuring that your configuration is as expected.

Real-World Scenario

Let’s apply what we’ve learned in a more complex example. Assume you’re deploying infrastructure across multiple AWS regions and accounts for high availability and isolation:

provider "aws" {
  region = "us-west-2"
}

provider "aws" {
  alias  = "east"
  region = "us-east-1"
  profile = "account-one"
}

provider "aws" {
  alias  = "europe"
  region = "eu-central-1"
  profile = "account-two"
}

This setup allows you to tailor your infrastructure deployment across different regions and accounts, proving the flexibility and power of Terraform with provider aliases.

Conclusion

Provider aliases in Terraform unlock the ability to manage diverse configurations and nuanced deployment strategies with ease. Starting with the basics before moving onto more complex scenarios allows you to fully leverage this powerful feature for sophisticated infrastructure management. Remember that careful planning of your provider configuration is key to making the most out of Terraform’s capabilities.