More Than One Instance of the Same Provider
We will learn to create multiple instances of the same provider and to add provider property.
Creating multiple instances of the same provider#
Since region
is a required parameter in the AWS provider, you may be wondering how to create
resources in different regions. Is that possible in a single Terraform project? Well, yes, it is. To do this, you simply create multiple instances of the same provider.
Project example#
Consider the following project:
/
- main.tf
AWS provider instances#
As you can see, we are defining two instances of the AWS provider. One points at the region
eu-west-1
while the other points at the region eu-west-2
. For the one that is pointing at eu-west-2
, you will notice
that we are setting the alias
property to london
. Alias is a property you can set on any provider block. It is in no way special to the AWS provider. What this gives us is a way to distinguish between the two providers. Once you define two or more instances of the same provider, every definition must have an alias set after the first.
VPC creation#
After we have defined the two AWS providers, we will create a VPC called ireland_vpc
with the
CIDR block 10.0.0.0/16
. As we have not told Terraform which provider instance to use for this
resource, Terraform will pick the instance of the AWS provider where you have not defined an alias.
This means that this VPC will be created in the region eu-west-1
(Ireland). The second VPC we
have defined with the identifier london_vpc has a CIDR block of 10.1.0.0/16
. This time we have
set the provider property on the resource to aws.london
. This means that Terraform will use our second AWS provider, which points to the region eu-west-2
. When we run the project by doing
terraform apply
, this VPC will be created in eu-west-2
.
Provider property#
Every resource has a provider property that you can set. The format of the value is set by <provider_- name>.<provider_alias>
. For our example, we were using the AWS provider with the alias “London” when we created the VPC in London. Thus, we set the provider property to aws.london
.
You can also explicitly tell Terraform to use the default provider. To do this, you can set the provider
property to the provider name. So, for example, we could have defined the ireland_vpc
:
Project output#
This would have given us the same output, thus adding extra code with no added value. No one in the community writes their HCL as a result. Less is more, and it is cleaner and easier to read if you omit the provider property when using the default provider instance.
Running the project#
After running the project, it will create two VPCs, one in eu-west-1
and one in eu-west-2
. Log into the AWS Console and navigate to find the VPC section.
Switch into the eu-west-1
and eu-west-2
regions and you will see the VPCs that Terraform created.
📝Note: Once you are done with the project, do not forget to run
terraform destroy
and then confirm with yes to delete all of the resources in this project.