-2

我有 Terraform 脚本,它将创建 VPC、负载均衡器和 ECS。使用模块(主应用程序文件夹内的文件夹)创建任务定义,但不允许访问在此模块之外创建的资源,例如子网、安全组。我想知道如何从此模块文件夹 tf 访问这些资源

4

1 回答 1

1

如果您想通过实例访问从名为 的模块创建的 VPC 的 ID create_vpc,您需要将其导出,在模块代码中添加类似这样的内容。

create_vpc/output.tf:

output "vpc_id" {
  value="${aws_vpc.my_vpc.id}"
}

注意:显然您需要my_vpc在模块内部创建一个称为 VPC,通常在一个名为 的文件中create_vpc/main.tf,但我认为您可以控制该部分。

然后,您只需要使用以下内容调用该vpc_id模块的输出:

站点/main.tf:

module "create_vpc" {
  source = "../create_vpc"
}

resource "aws_internet_gateway" "vpc_internet_gateway" {
  vpc_id = "${module.create_vpc.vpc_id}"
}

注意:此处创建的 VPC Internet 网关只是使用 VPC ID 的示例

以类似的方式,您可以将子网、安全组名称和 ID 等从一个模块导出到另一个模块。

于 2020-04-12T07:22:52.007 回答