author avatar

nisanth

Mon Jan 29 2024

If we want to create an instance in different regions within the same Terraform file, we need to use provider aliases. In Terraform, a single file typically contains one default provider configuration for ‘aws.’ To work with multiple regions, we use provider aliases.
Instead of having two separate provider blocks, we add aliases to them. For example:


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

resource "aws_instance" "example" {
  provider       = aws.us-east-2
  ami            = "ami-id"
  instance_type  = "t2.micro"
}

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

resource "aws_instance" "example1" {
  provider       = aws.us-east-1
  ami            = "ami-id"
  instance_type  = "t2.micro"
}


This way, we can create instances in different regions using a single Terraform file, and each instance is associated with its respective region through the use of provider aliases