Adding alias to a provider to use multiple instances of the same provider
When we write Terraform configuration, some providers contain properties for resource access and authentication such as a URL, authentication token, username, or password.
If we want to use multiple different configurations of the same provider in one Terraform configuration, for example, to provision resources in multiple Azure subscriptions in the same configuration, we can use the alias
provider property.
Let’s get started!
Getting ready
First, apply this basic Terraform code to create resources on Azure:
provider "azurerm" {
subscription_id = "xxxx-xxx-xxx-xxxxxx"
features {}
}
resource "azurerm_resource_group" "rg" {
name = "rg-sub1"
location = "westeurope"
}
resource "azurerm_resource_group" "rg2" {
name = "rg-sub2"
location = westeurope"
}
This Terraform configuration will create two Azure resource groups on the subscription that is configured by the provider (or in the default subscription on your Azure account).
In order to create an Azure resource group in another subscription, we need to use the alias
property.
In this recipe, we will use the alias
provider property, and to illustrate it we will provision two Azure resource groups in two different subscriptions in one Terraform configuration.
The requirement for this recipe is to have an Azure account, which you can get for free here: https://azure.microsoft.com/en-us/free/
We will also use the azurerm
provider with basic configuration.
You can find your available active subscriptions (subscription IDs) at https://portal.azure.com/#view/Microsoft_Azure_Billing/SubscriptionsBlade.
The source code of this recipe is available here: https://github.com/PacktPublishing/Terraform-Cookbook-Second-Edition/tree/main/CHAP02/alias
How to do it…
Perform the following steps to use multiple instances from one provider:
- In
main.tf
, update the initial Terraform configuration in theprovider
section:provider "azurerm" { subscription_id = "xxxx-xxx-xxxxx-xxxxxx" alias = "sub1" features {} } provider "azurerm" { subscription_id = "yyyy-yyyyy-yyyy-yyyyy" alias = "sub2" features {} }
- Then update the two existing
azurerm_resource_group
resources:resource "azurerm_resource_group" "example1" { provider = azurerm.sub1 name = "rg-sub1" location = "westeurope" } resource "azurerm_resource_group" "example2" { provider = azurerm.sub2 name = "rg-sub2" location = "westeurope" }
- Finally, to apply the changes, run the Terraform workflow with the
init
,plan
, andapply
commands.
How it works…
In Step 1, we duplicate the provider (azurerm
) block and, on each provider, we add the alias
property with an identification name. The first is sub1
and the second is sub2
.
Then we add the different subscription_id
properties to specify the subscription where the resource will be created.
In Step 2, in each azurerm_resource_group
resource, we add the provider
property with a value that corresponds to that of the alias
of the desired provider.
Each azurerm_resource_group
resource targets the subscription using the provider’s alias
.
Finally, we run the terraform
init
, plan
and apply
commands. The screenshot below shows the terraform
apply
command:
Figure 2.5: Running the apply command
We can see the two different subscriptions where the Azure resource group will be created.
See also
- The documentation of the provider
alias
is available at https://www.terraform.io/language/providers/configuration#alias-multiple-provider-configurations. - Here’s a great article on using the provider
alias
: https://build5nines.com/terraform-deploy-to-multiple-azure-subscriptions-in-single-project/.