0

I have 2 aws accounts with respective terraform code for it: In account_no_01 lets say, I have a tgw module

module "transit-gateway" {}

In account_no_02, I want to get the id of the created tgw in account 1:

resource "aws_ec2_transit_gateway_vpc_attachment" "tgw_nprod" {
  subnet_ids         = [module.vpc.private_subnets[0]]
  transit_gateway_id = "TGW ID HERE FROM ACCOUNT 01 CREATED WITH MODULE"
  vpc_id             = module.vpc.vpc_id
}

And the dir structure is like this: /acount01/main.tf and /account02/main.tf

4

1 回答 1

1

If the two accounts are managed by one statefile, you can use module outputs.

If both accounts are created separately, you can use a data module in terraform to reference a resource that is not managed by terraform or managed by a different statefile.

The key options for a transit gateway data resource are documented here.

The simplest way is to add the ID value in configuration for your account 2 build, and reference it that way. If that's not possible, you can add a friendly name in a tag, and use a filter to find it elsewhere:

data "aws_ec2_transit_gateway" "tgw" {
  filter {
    name   = "tag:Name"
    values = ["my-transit-gw"]
  }
}
于 2021-03-17T13:35:10.313 回答