【问题标题】:How to access the resource ids between two different terraform code如何访问两个不同 terraform 代码之间的资源 ID
【发布时间】:2021-06-14 13:20:28
【问题描述】:

我有 2 个带有相应 terraform 代码的 aws 帐户: 在 account_no_01 可以说,我有一个 tgw 模块

module "transit-gateway" {}

在account_no_02中,我想获取账户1中创建的tgw的id:

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
}

目录结构是这样的: /acount01/main.tf and /account02/main.tf

【问题讨论】:

标签: terraform terraform-provider-aws terraform0.12+ terraform-modules


【解决方案1】:

如果两个账户由一个statefile管理,可以使用module outputs

如果两个帐户是分开创建的,您可以使用 terraform 中的数据模块来引用不受 terraform 管理或由不同 statefile 管理的资源。

中转网关数据资源的关键选项记录在 here

最简单的方法是在您的帐户 2 构建的配置中添加 ID 值,并以这种方式引用它。如果这不可能,您可以在标签中添加一个友好名称,并使用过滤器在其他地方找到它:

data "aws_ec2_transit_gateway" "tgw" {
  filter {
    name   = "tag:Name"
    values = ["my-transit-gw"]
  }
}

【讨论】:

    猜你喜欢
    • 2018-06-01
    • 2022-08-22
    • 2021-09-26
    • 2019-03-02
    • 1970-01-01
    • 2011-03-10
    • 2017-12-14
    • 2020-03-16
    • 2021-12-11
    相关资源
    最近更新 更多