Terraform
provider block reference for component configurations
Terraform relies on plugins called providers to interact with cloud providers, SaaS providers, and other APIs. Use the provider block to configure your Stack's declared providers.
If you are declaring a provider block in a traditional Terraform configuration file, ending in .tf, refer to the Terraform configuration provider block reference instead.
Background
In component configurations, you must declare a provider in the required_providers block at the top level of your tfcomponent.hcl file before you can use that provider.
After your provider declaration, use the provider block to further configure your providers. Unlike traditional Terraform configurations, provider blocks in component configurations support the following:
- The
for_eachmeta-argument for dynamic provider configurations. - Defining provider aliases in the block header.
- Using a
configblock to pass in provider arguments.
After configuring your providers, pass them to your Stack's component blocks using the providers argument. Passing configurations directly to components ensures that every component in your Stack uses the same provider configuration. To learn more and view examples, refer to Declare providers.
Configuration model
The provider block supports the following arguments:
Complete configuration
All available arguments are defined in the following provider block:
provider "<PROVIDER_NAME>" "<ALIAS>" {
for_each = <MAP_OR_SET>
config {
<PROVIDER_ARGUMENTS>
}
}
Specification
A provider block supports the following configuration.
provider "<PROVIDER_NAME>" "<ALIAS>"
The provider block requires two labels: the name of the provider and an alias. The provider name must match the name of the provider in your required_providers block. The provider alias serves as a unique identifier for this specific provider configuration.
The provider block supports the following arguments:
| Argument | Description | Type | Required? |
|---|---|---|---|
config | A block containing the provider's configuration arguments. The config argument is required if the provider requires you to configure it before Terraform can use it. | Block | Optional |
for_each | Creates multiple instances of the provider configuration based on a map or set. | Map or Set | Optional |
config
The config block contains the configuration arguments for your provider. A provider's configuration arguments are specific to each provider and are documented by the provider developer.
provider "<NAME>" "<ALIAS>" {
config {
<NAME> = <VALUE>
}
}
Component configurations require all provider arguments to be nested within a config block.
Summary
- Data type: Block
- Default: None
for_each
The for_each meta-argument creates multiple instances of a provider configuration based on a map or set of values. The for_each meta-argument lets you dynamically create provider configurations based on your inputs, which is beneficial for multi-region deployments.
provider "<NAME>" "<ALIAS>" {
for_each = [ "<VALUE>" ]
# . . .
}
When you use for_each, you can reference individual provider instances using provider.<PROVIDER_NAME>.<ALIAS>[<KEY>] syntax in your component blocks.
Summary
- Data type: Map or Set
- Default: None
Examples
The following examples demonstrate common use cases for provider blocks in Stacks.
Fundamental provider configuration
In the following example, Terraform configures the AWS provider main with the region from var.aws_region and authenticates using the role ARN from var.role_arn and identity token from var.identity_token:
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.7.0"
}
}
provider "aws" "main" {
config {
region = var.aws_region
assume_role_with_web_identity {
role_arn = var.role_arn
web_identity_token = var.identity_token
}
}
}
After defining providers, you pass them to your component blocks using the providers argument:
component "storage" {
source = "./modules/s3"
providers = {
aws = provider.aws.main
}
inputs = {
bucket_name = "my-bucket"
}
}
To learn more about authenticating with cloud providers within Stacks, refer to Declare providers.
Multi-region provider configuration
In the following example, Terraform creates an AWS provider regional for each region in var.regions. Terraform creates each provider configuration with environment tags from var.environment and region-specific tags using each.value:
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.7.0"
}
}
provider "aws" "regional" {
for_each = var.regions
config {
region = each.value
assume_role_with_web_identity {
role_arn = var.role_arn
web_identity_token = var.identity_token
}
default_tags {
tags = {
Environment = var.environment
Region = each.value
}
}
}
}
To refer to individual provider instances in your component blocks, use the provider.<PROVIDER_NAME>.<ALIAS>[<KEY>] syntax. For example, to reference the regional provider for the us-west-2 region, you would use provider.aws.regional["us-west-2"].
You can also use for_each with component blocks to create multiple components for each region:
component "regional_storage" {
for_each = var.regions
source = "./modules/s3"
providers = {
aws = provider.aws.regional[each.value]
}
inputs = {
region = each.value
bucket_name = "my-bucket-${each.value}"
}
}