Module meta-arguments
Meta-arguments control how a module behaves, enabling multi-instance creation, explicit dependency ordering, and provider configuration management.
The previous topic describes the source argument. This topic describes meta-arguments and how they change the way modules behave.
Overview
Meta-arguments are a special set of configuration options that do not directly define resource attributes but control the behavior and usage of a module. They provide additional flexibility, enabling a module to adapt to different deployment scenarios and architectural requirements. You can use meta-arguments to create multiple module instances, control the order in which resources are deployed, manage provider configurations, and customize the lifecycle behavior of modules.
Terraform modules support the following meta-arguments: providers (passes specific provider configurations to child modules), depends_on (defines explicit dependencies between modules), count (creates multiple identical module instances), and for_each (creates different module instances based on mappings or collections). These meta-arguments are particularly important in multi-region deployments, conditional resource creation, cross-account resource management, and large-scale infrastructure orchestration.
Proper use of meta-arguments simplifies cross-region resource deployment, enables conditional resource creation, ensures proper initialization order, and allows you to manage multiple environments with a single configuration. This topic focuses on the providers meta-argument. For information about other meta-arguments, see depends_on, count, and for_each.
providers
providers specifies which provider configurations from the parent module are available within a child module. This is especially useful for multi-region or multi-account resource management.
Usage
# Define the default Alibaba Cloud provider configuration in the root module for the China (Hangzhou) region.
provider "alicloud" {
region = "cn-hangzhou"
}
# Define another configuration for the China (Beijing) region and use the alias "bj".
provider "alicloud" {
alias = "bj"
region = "cn-beijing"
}
# Use the configuration of the China (Beijing) region when instantiating the child module
module "ecs_cluster" {
source = "./modules/ecs-cluster"
providers = {
alicloud = alicloud.bj
}
}
Default behavior: Inherit the default provider
The providers argument is optional if the child module does not declare any configuration aliases. If omitted, the child module inherits all default provider configurations from its parent module — that is, configurations that do not use alias.
If providers is specified, this default inheritance is overridden and the child module only has access to the provider configurations that you explicitly specify.
Usage and behavior
The value of providers is a map in which:
-
The key is the provider configuration name used within the child module.
-
The value is the provider configuration name from the parent module.
Both the key and the value must be unquoted references to provider configurations. For the default configuration, use the local name of the provider. For an aliased configuration, use the <PROVIDER>.<ALIAS> reference.
In the child module, resources are assigned to provider configurations as follows:
-
Terraform selects the default provider based on the name of the resource type.
-
Resources use the
providerargument to specify an alternative configuration.
If the module receives a providers mapping at call time, the provider configuration names used within the module are remapped to the configurations specified in the parent module.
When to specify providers
There are two main reasons to use providers:
-
Use different default provider configurations for child modules: for example, to deploy module resources to different regions.
-
Configure modules that require multiple configurations of the same provider: for example, to manage resource interactions between different regions.
Change the default provider configuration
Most reusable modules use only the default provider configuration and can automatically inherit it from the caller when providers is omitted.
However, in a configuration that uses multiple configurations from the same provider, you may want some child modules to use the default configuration while others use an aliased configuration. This typically occurs when you use one provider to manage resources across multiple regions of the same cloud platform.
By using providers (as shown in the preceding example), you can achieve this without modifying the child module. Although the code within the child module always refers to the default provider configuration, the actual configuration can differ for each instance.
For a similar module, see hybrid-cloud-network.
Modules with alternative provider configurations
In rare cases, a single reusable module may require multiple configurations from the same provider. For example, a module that configures a connection between two Alibaba Cloud regions requires both a source region and a destination region. In this case, the root module might look like this:
# Define the configuration of the China (Hangzhou) region.
provider "alicloud" {
alias = "hangzhou"
region = "cn-hangzhou"
}
# Define the configuration of the China (China) region.
provider "alicloud" {
alias = "beijing"
region = "cn-beijing"
}
# Map the two provider configurations in the module call.
module "vpc_peering" {
source = "./modules/vpc-peering"
providers = {
alicloud.source = alicloud.hangzhou
alicloud.destination = alicloud.beijing
}
}
Non-default provider configurations are never automatically inherited, so any module that uses aliased providers always requires an explicit providers mapping. The module documentation must specify all required provider configuration names.
For a similar module, see cen-cross-region-networking-with-qos.
Examples
Cross-region resource management
# Define the provider configuration for the China (Hangzhou) region.
provider "alicloud" {
region = "cn-hangzhou"
}
# Define the provider configuration for the China (Shenzhen) region.
provider "alicloud" {
alias = "shenzhen"
region = "cn-shenzhen"
}
# Use the default China (Hangzhou) region to deploy the primary VPC.
module "primary_vpc" {
source = "alibaba/vpc/alicloud"
vpc_name = "primary-vpc"
vpc_cidr = "10.0.0.0/16"
# If providers is not specified, the default China (Hangzhou) region is used.
}
# Deploy a disaster recovery VPC in the China (Shenzhen) region.
module "dr_vpc" {
source = "alibaba/vpc/alicloud"
vpc_name = "dr-vpc"
vpc_cidr = "10.1.0.0/16"
providers = {
alicloud = alicloud.shenzhen
}
}
Cross-region data replication
When a module needs to manage resources across two regions:
# Inside the child module (./modules/oss-replication/main.tf)
terraform {
required_providers {
alicloud = {
source = "aliyun/alicloud"
# Declare that this module requires two Alibaba Cloud provider configurations.
configuration_aliases = [ alicloud.source, alicloud.replica ]
}
}
}
# Create a source bucket.
resource "alicloud_oss_bucket" "source" {
provider = alicloud.source
bucket = var.source_bucket_name
# Other configurations.
}
# Create a destination bucket.
resource "alicloud_oss_bucket" "replica" {
provider = alicloud.replica
bucket = var.replica_bucket_name
# Other configurations.
}
# Configure replication rules.
resource "alicloud_oss_bucket_replication" "this" {
provider = alicloud.source
bucket = alicloud_oss_bucket.source.bucket
target_bucket = alicloud_oss_bucket.replica.bucket
target_region = var.replica_region
# Other configurations.
}
Call the module in the root module:
provider "alicloud" {
alias = "hangzhou"
region = "cn-hangzhou"
}
provider "alicloud" {
alias = "beijing"
region = "cn-beijing"
}
module "oss_replication" {
source = "./modules/oss-replication"
source_bucket_name = "source-data-bucket"
replica_bucket_name = "replica-data-bucket"
replica_region = "cn-beijing"
providers = {
alicloud.source = alicloud.hangzhou
alicloud.replica = alicloud.beijing
}
}
For a similar module, see cen-cross-region-networking-with-qos.
providers lets you control which provider configurations child modules use, simplifying cross-region or cross-account resource management.