Terraform Multiple Providers: Complete Guide

Published: 2025-08-01
8 min read
Share:

Terraform multiple providers allow you to use more than one provider plugin within the same configuration.

This is useful when infrastructure spans multiple cloud platforms or when utility providers such as random, local, or tls are needed alongside cloud resources.

In this guide, you'll learn how to configure, initialize, and use multiple providers in a single Terraform project. You'll also see real-world examples, common mistakes, and best practices used in production environments.

What Are Terraform Providers?

Before working with Terraform multiple providers, it's important to understand how Terraform providers work.

A provider is a plugin that allows Terraform to interact with external systems. Providers act as the bridge between Terraform and the APIs of cloud platforms, SaaS tools, networking services, and local utilities.

Some commonly used providers include:

  • aws for Amazon Web Services
  • azurerm for Microsoft Azure
  • google for Google Cloud Platform
  • kubernetes for Kubernetes clusters
  • local for local file operations
  • random for generating random values

Terraform downloads providers from the official Terraform Registry.

Using multiple providers simply means managing resources from more than one provider within the same Terraform configuration.

Why Use Multiple Providers in Terraform?

In real projects, it's common to combine cloud providers, utility providers, and local tooling within the same Terraform configuration.

Some common scenarios include:

  • Provisioning infrastructure on AWS while storing DNS records in Cloudflare
  • Deploying resources in Azure and generating passwords using the Random provider
  • Managing Kubernetes resources after provisioning clusters in AWS
  • Creating local configuration files alongside cloud infrastructure
  • Working across multiple cloud providers during migrations

A common example is generating secure passwords with the Random provider and immediately using those values when provisioning cloud resources. This avoids hardcoding credentials and keeps infrastructure definitions reproducible.

If you're already familiar with building a Terraform AWS project, multiple providers are often the next step toward managing more complex infrastructure workflows.

How to Configure Terraform Multiple Providers

Let's start with a simple example using the Local and Random providers.

Step 1: Declare Required Providers

Create a main.tf file and define the providers:

terraform {
  required_providers {
    local = {
      source  = "hashicorp/local"
      version = ">= 2.0.0"
    }

    random = {
      source  = "hashicorp/random"
      version = ">= 3.0.0"
    }
  }
}

Pro Tip

Always pin provider versions in production environments.

Using version constraints helps prevent unexpected behavior caused by provider upgrades. You can learn more about Terraform version constraints and how to manage provider compatibility safely.

Step 2: Create Resources from Multiple Providers

Now define resources that use both providers:

resource "local_file" "pet" {
  filename = "/tmp/pets.txt"
  content  = "We love pets"
}

resource "random_pet" "my_pet" {
  prefix    = "awesome"
  separator = "-"
  length    = 2
}

In this example:

  • local_file creates a file on the local machine
  • random_pet generates a random name such as awesome-lion-tiger

Both resources are managed within the same Terraform project even though they come from different providers.

Running Terraform Init with Multiple Providers

After defining providers and resources, initialize the working directory:

terraform init

Terraform scans your configuration, identifies required providers, and downloads them from the official Terraform Registry.

During initialization, Terraform also:

  • Creates the .terraform directory
  • Downloads provider plugins
  • Generates or updates the dependency lock file
  • Verifies provider requirements

You may see output similar to:

- Reusing previous version of hashicorp/local from the dependency lock file
- Installing hashicorp/random v3.x.x...

Common Mistake

Many beginners add a new provider and immediately run terraform plan.

Terraform will not download the new provider until terraform init is executed again. Whenever you add or modify provider requirements, rerun terraform init.

Planning and Applying Infrastructure Changes

Once initialization is complete, generate an execution plan:

terraform plan

Terraform compares the current infrastructure state with the desired configuration and displays proposed changes.

Resources scheduled for creation are marked with a + symbol:

+ random_pet.my_pet will be created
  + id        = (known after apply)
  + length    = 2
  + prefix    = "awesome"
  + separator = "-"

If everything looks correct, apply the changes:

terraform apply

Terraform will ask for confirmation before creating the resources.

After approval, both the local file and random name will be created during the same execution.

If you're new to Terraform workflows, reviewing common Terraform commands can help you understand each stage of the deployment lifecycle.

Real-World Example: AWS and Random Providers Together

Production environments frequently combine cloud providers with utility providers.

A common pattern is generating credentials using the Random provider and storing them securely in AWS.

resource "random_password" "db_password" {
  length  = 20
  special = true
}

resource "aws_ssm_parameter" "database_password" {
  name  = "/application/database-password"
  type  = "SecureString"
  value = random_password.db_password.result
}

In this example:

  • The Random provider generates a secure password
  • The AWS provider stores the password in AWS Systems Manager Parameter Store

This pattern is commonly used when building infrastructure with services such as AWS IAM with Terraform, AWS EC2 with Terraform, and AWS S3 with Terraform.

How Terraform Resolves Resources Across Multiple Providers

From Terraform's perspective, each provider is simply another plugin loaded into the execution graph.

Once initialized, Terraform can coordinate resources from all providers during the same planning and apply operations.

This allows you to:

  • Pass values between resources from different providers
  • Manage related infrastructure in a single project
  • Maintain a unified state file
  • Reduce duplication across configurations
  • Build reusable Terraform modules that work with different providers

Terraform automatically tracks dependencies and determines the correct order of operations during deployment.

Using Provider Aliases

Provider aliases are useful when you need multiple configurations of the same provider.

For example, you may want to manage resources in two AWS regions:

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

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

resource "aws_s3_bucket" "primary" {
  bucket = "example-primary-bucket"
}

resource "aws_s3_bucket" "secondary" {
  provider = aws.west
  bucket   = "example-secondary-bucket"
}

Aliases make it possible to:

  • Deploy resources across multiple regions
  • Manage multiple AWS accounts
  • Connect to different Kubernetes clusters
  • Support multi-environment deployments

This is one of the most common real-world uses of Terraform multiple providers.

Best Practices for Using Multiple Providers

Follow these practices to keep configurations maintainable and predictable:

  1. Use provider version constraints to avoid unexpected upgrades.
  2. Separate large configurations into logical files.
  3. Run terraform plan frequently during development.
  4. Keep provider documentation readily available.
  5. Use aliases instead of duplicating configurations.
  6. Store secrets securely instead of hardcoding values.
  7. Validate configurations before deployment.

You can also combine these practices with Terraform workspaces, Terraform modules, and remote state management for larger environments.

Common Challenges When Working with Multiple Providers

Teams often encounter a few recurring issues:

Provider Version Conflicts

Different modules may require different provider versions.

Review provider requirements carefully and maintain consistent version constraints across modules.

Missing Initialization

Adding a provider without rerunning terraform init causes Terraform to fail because the plugin is not installed locally.

Incorrect Provider References

When aliases are used, resources must explicitly reference the correct provider configuration.

State Management Complexity

As projects grow, managing state becomes increasingly important.

Review Terraform state fundamentals and remote backends with AWS S3 before scaling larger deployments.

FAQ — Terraform Multiple Providers

Can I use more than two providers in Terraform?

Yes.

Terraform supports using multiple providers within the same configuration. There is no practical limit for most infrastructure projects.

Do I need to run terraform init after adding a new provider?

Yes.

Terraform must download the new provider plugin before it can be used. Running terraform init installs the required provider and updates dependency information.

Can resources share data across different providers?

Yes.

Terraform allows outputs and attributes from one resource to be referenced by another resource, even when they belong to different providers.

What happens if providers have similar resource names?

Terraform identifies resources using provider namespaces and resource types, preventing naming conflicts between providers.

Is it possible to define multiple configurations for the same provider?

Yes.

Provider aliases allow multiple configurations of the same provider. This is commonly used for multi-region and multi-account deployments.

Should I always declare providers explicitly?

While Terraform can often infer providers automatically, explicitly declaring them is considered a best practice because it provides version control, better documentation, and more predictable deployments.

Key Takeaways

Most production Terraform deployments eventually use more than one provider.

Whether you're combining AWS and Kubernetes, AWS and Cloudflare, or utility providers such as Random and Local, the workflow remains the same:

  1. Declare the required providers.
  2. Run terraform init.
  3. Plan the changes.
  4. Apply the configuration.
  5. Manage dependencies and versions carefully.

Once you understand this pattern, you can build more flexible and scalable infrastructure while keeping everything managed through a single Terraform workflow.

Free Engineering ToolsNEW

8 free, 100% client-side tools for developers — no signup, no data uploads.

Explore all tools