【问题标题】:Terraform Subnet details not identified inside module模块内部未识别 Terraform 子网详细信息
【发布时间】:2020-07-24 18:41:07
【问题描述】:

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

【问题讨论】:

    标签: amazon-web-services module terraform terraform-template-file


    【解决方案1】:

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

    create_vpc/output.tf:

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

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

    然后,您只需调用该模块的vpc_id 输出,例如:

    site/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-08-17
      • 1970-01-01
      • 1970-01-01
      • 2010-11-27
      • 1970-01-01
      • 2012-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多