author avatar

mahesh.bhosle

Tue Apr 16 2024

Terraform alias is a feature that allows you to manage resources across multiple regions more efficiently. It enables you to define different configurations for resources in various regions while using the same Terraform codebase. Here's a simple example to illustrate how to use Terraform alias for multiple regions:

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

provider "aws" {
  alias  = "us_west"
  region = "us-west-1"
}
resource "aws_instance" "example" {
  provider      = aws.us_east
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

resource "aws_instance" "example_west" {
  provider      = aws.us_west
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
} 

In this example, we define two different AWS providers with aliases us_east and us_west, representing the US East and US West regions, respectively. Then, we create instances using these providers, specifying the region-specific provider for each instance. This allows Terraform to manage resources in different regions using the same configuration file. #terraform #iac