Multi Region Issue In Transit Gateway

Hi, I am trying to setup a transit gateway for multiple vpc in different region. But I am not able to add the VPC of different region . As in below code the vpc1 and vpc2 are in the same region while vpc3 is in another region. The error is shown below. How can I resolve this issue.

Error message:
Error: no matching VPC found
  on main.tf line 14, in data "aws_vpc" "vpc3":
  14: data "aws_vpc" "vpc3" {

main.tf

data "aws_vpc" "vpc1" {
  filter {
    name   = "tag:Name"
    values = ["vpc1"]
  }
}
data "aws_vpc" "vpc2" {
  filter {
    name   = "tag:Name"
    values = ["vpc2"]
  }
}
data "aws_vpc" "vpc3" {
  filter {
    name   = "tag:Name"
    values = ["vpc3"]
  }
}

data "aws_subnet_ids" "vpc1" {
  vpc_id  = data.aws_vpc.vpc1.id
}

data "aws_subnet_ids" "vpc2" {
  vpc_id  = data.aws_vpc.vpc2.id
}
data "aws_subnet_ids" "vpc3" {
  vpc_id  = data.aws_vpc.vpc3.id
}


module "tgw" {
  source  = "terraform-aws-modules/transit-gateway/aws"
  version = "~> 2.0"

  name        = "my-tgw"
  description = "My TGW shared with several other AWS accounts"

  enable_auto_accept_shared_attachments = true

  vpc_attachments = {
    vpc1 = {
      #vpc_id       = module.vpc.vpc_id
      vpc_id       = data.aws_vpc.vpc1.id
      subnet_ids   = data.aws_subnet_ids.vpc1.ids
      dns_support  = true
      ipv6_support = true

      tgw_routes = [
        {
          destination_cidr_block = "10.0.0.0/8"
        },
        {
          blackhole = true
          destination_cidr_block = "0.0.0.0/0"
        }
      ]
    },
   vpc2 = {
      vpc_id       = data.aws_vpc.vpc2.id
      subnet_ids   = data.aws_subnet_ids.vpc2.ids
      dns_support  = true
      ipv6_support = true

      tgw_routes = [
        {
          destination_cidr_block = "10.2.0.0/16"
        },
        {
          blackhole = true
          destination_cidr_block = "30.0.0.0/8"
        }
      ]
    },
    vpc3 = {
      vpc_id       = data.aws_vpc.vpc3.id
      subnet_ids   = data.aws_subnet_ids.vpc3.ids
      dns_support  = true
      ipv6_support = true

      tgw_routes = [
        {
          destination_cidr_block = "10.3.0.0/16"
        },
        {
          blackhole = true
          destination_cidr_block = "40.0.0.0/8"
        }
      ]
    }
  }

  ram_allow_external_principals = true
  ram_principals = [307990089504]

  tags = {
    Purpose = "tgw-complete-example"
  }
}

providers.tf

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

@nibatandukar your AWS terraform provider is configured for a particular AWS region. To access VPCs across multiple regions you need to configure additional AWS providers using aliases.
you can then refer to the dedicated VPCs using the used aliases (see also Provider Configuration - Configuration Language - Terraform by HashiCorp)

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

provider "aws" {
  region = "us-west-1"
  alias = "west"
}
data "aws_vpc" "vpc1" {
  filter {
    name   = "tag:Name"
    values = ["vpc1"]
  }
}
data "aws_vpc" "vpc2" {
  filter {
    name   = "tag:Name"
    values = ["vpc2"]
  }
}
data "aws_vpc" "vpc3" {
  provider = aws.west
  filter {
    name   = "tag:Name"
    values = ["vpc3"]
  }
}

...
1 Like