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:



provider "aws" {
    region = "us-east-1"
}

provider "aws" {
    region = "us-east-2"
    alias = "ohio"
}

resource "aws_vpc" "n_virginia_vpc" {
    cidr_block = "10.0.0.0/16"
}

resource "aws_vpc" "ohio_vpc" {
    cidr_block = "10.1.0.0/16"
    provider = aws.ohio
}

AWS provider instances

As you can see, we are defining two instances of the AWS provider. One points at the region us-east-1 while the other points at the region us-east-2. For the one that is pointing at us-east-2, you will notice that we are setting the alias property to ohio. 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 n_virginia_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 us-east-1 (N. Virginia). The second VPC we have defined with the identifier ohio_vpc has a CIDR block of 10.1.0.0/16. This time we have set the provider property on the resource to aws.ohio. This means that Terraform will use our second AWS provider, which points to the region us-east-2. When we run the project by doing terraform apply, this VPC will be created in us-east-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 “Ohio” when we created the VPC in Ohio. Thus, we set the provider property to aws.ohio.

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 n_virginia_vpc:

Press + to interact
resource "aws_vpc" "n_virginia_vpc" {
cidr_block = "10.0.0.0/16"
provider = "aws"
}

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 us-east-1 and one in us-east-2. Log into the AWS Console and navigate to find the VPC section. Switch into the us-east-1 and us-east-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.

Get hands-on with 1300+ tech skills courses.