简体   繁体   中英

How to access the resource ids between two different terraform code

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

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"]
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM