1. Declare Your Resources
First, define two AWS EC2 key pairs namedalpha and beta:
2. Configure the Default Provider
Specify the default AWS provider region:aws_key_pair.alpha and aws_key_pair.beta will be created in us-east-1.
3. Add an Aliased Provider
To deploybeta in a different region, add a second provider block with an alias:
- The first block remains the default (
us-east-1). - The second block is identified by
alias = "central"and targets ca-central-1.
4. Assign the Aliased Provider to a Resource
Use theprovider meta-argument within your resource to select the aliased provider:
alphacontinues to use the default AWS provider (us-east-1).betanow uses the aws.central provider (ca-central-1).
Provider aliases are ideal for multi-region or multi-account strategies in a single configuration. You can also combine aliases with workspaces or backends for more complex deployments.
5. Verify with tofu show
After running tofu apply, inspect your deployed resources:
alpha appears in us-east-1 (default) and beta in ca-central-1 (aliased).
6. Resource–Provider Mapping
| Resource | Provider Block | Region |
|---|---|---|
| aws_key_pair.alpha | aws (default) | us-east-1 |
| aws_key_pair.beta | aws.central (alias) | ca-central-1 |
Make sure your AWS credentials have permissions for each region or profile you target. Missing credentials can cause resource creation failures.