【问题标题】:Modules and Variables carrying over tfvar values携带 tfvar 值的模块和变量
【发布时间】:2021-07-05 16:46:06
【问题描述】:

我是 TF 的新手,我已经编写了一些基本代码。足以让 vpc 启动并运行并添加一些子网并部署一个简单的 ec2。我开始想要使用模块。我在“保持通用”的事情上苦苦挣扎,这样我就可以一遍又一遍地重复使用它们。我不明白值是如何传递到模块中的。例如,我有一个部署 vpc 的模块,在同一个项目中,我有一个部署 vpc 端点的模块。问题变成了如何将使用 vpc 模块创建的 vpc_id 的值获取到 vpc 端点模块中?有人有这方面的例子吗?

main.tf

    provider "aws" {
    region = var.aws_region
    }

    /*Module for VPC creation*/
    module "vpc" {
    source                  = "./modules/vpc"
    vpc_cidr                = var.vpc_cidr
    environment             = var.environment
    tnt_public_subnets_cidr = var.tnt_public_subnets_cidr
    availability_zones      = var.availability_zones

    }

    /*Module for EC2 Webserver creation*/
    module "webserver" {
    source      = "./modules/ec2/webserver"
    count       = var.instance_count
    environment = var.environment
    subnet_id   = module.vpc.tnt_public_subnets_cidr.id
    }

    /*Module for VPC endpoint creation*/
    module "s3-vpce"{
    source = "git::https://github.com/tn-sts-cloudtn/sts-terraform- 
    modules.git//s3-vpce-module/modules//s3-vpce"
    vpc_id    = module.vpc.vpc_id
  
    }

VPC 模块 TF 文件:

/*==== The VPC ======*/
resource "aws_vpc" "vpc" {
  cidr_block           = var.vpc_cidr
  assign_generated_ipv6_cidr_block = true
  enable_dns_hostnames = true
  enable_dns_support   = true
  tags = {
    Name        = "${var.environment}_vpc"
    Environment = var.environment
  }
}

/*==== Internet Gateway for Public Subnets ======*/

/* Internet gateway for the public subnet */
resource "aws_internet_gateway" "igw" {
  vpc_id = aws_vpc.vpc.id
  tags = {
    Name        = "${var.environment}_igw"
    Environment = var.environment
  }
}


/* Elastic IP for NAT 
resource "aws_eip" "instance_eip" {
  count = 1
  vpc        = true
  depends_on = [aws_internet_gateway.tnt_igw]
    tags ={
      Name = "sts_net_infra-mgmt_eip${count.index + 1}"
      Environment = var.environment
    }
}*/

/* Public subnet */
resource "aws_subnet" "public_subnets_cidr" {
  vpc_id                  = aws_vpc.vpc.id
  count             = length(var.availability_zones)
  cidr_block        = var.public_subnets_cidr [count.index]
  availability_zone = element(var.availability_zones, count.index)
  map_public_ip_on_launch = true
   tags ={
      Name = "${var.environment}_mgmt_subnet_${count.index + 1}"
      Environment = var.environment
    }
}
/* Private subnet 
resource "aws_subnet" "tnt_private_subnet" {
  vpc_id                  = aws_vpc.tnt_vpc.id
  count                   = length(var.tnt_private_subnets_cidr)
  cidr_block        = var.tnt_public_subnets_cidr [count.index]
  availability_zone = element(var.availability_zones, count.index)
  map_public_ip_on_launch = false
  tags = {
    #Name        = var.environment-private-subnet
    Environment = var.environment
  }
*/

/* Routing table for private subnet 
resource "aws_route_table" "tnt_private_rtb" {
  vpc_id = aws_vpc.tnt_vpc.id
  tags = {
    Name        = var.environment_private_route_table
    Environment = var.environment
  }
}*/


/* Routing table for public subnet */
resource "aws_route_table" "public_rtb" {
  vpc_id = aws_vpc.vpc.id
  tags = {
    Name        = "${var.environment}_public_route_table"
    Environment = var.environment
  }
}

resource "aws_route" "public_internet_gateway" {
  route_table_id         = aws_route_table.public_rtb.id
  destination_cidr_block = "0.0.0.0/0"
  gateway_id             = aws_internet_gateway.igw.id
}

/* Route table associations */
resource "aws_route_table_association" "public" {
  count          = length(var.public_subnets_cidr)
  subnet_id      = element(aws_subnet.public_subnets_cidr.*.id, count.index)
  route_table_id = aws_route_table.public_rtb.id

}

所以我需要输出 VPC 端点的 VPC ID,所以我尝试使用输出。

输出.tf

    output "vpc_cidr" {
    value = aws_vpc.tnt_vpc.id
}

output "tnt_public_subnets_cidr"{
    value = aws_subnet.tnt_public_subnets_cidr.*.id
}
output "vpc_id" {
  description = "The ID of the VPC"
  value       = aws_vpc.vpc.id
}

output "vpc_arn" {
  description = "The ARN of the VPC"
  value       = concat(aws_vpc.tnt_vpc.*.arn, [""])[0]
}

我知道我做错了,但我很难理解输出如何从一个模块流向另一个模块。

【问题讨论】:

  • 如果您在问题中发布了示例代码,您就有更好的机会通过示例找到答案。也就是说,看看outputs,你可以在另一个模块中重用一个模块的输出。
  • 您在./modules/vpc 中有哪些.tf 文件?
  • 主、输出、变量
  • 查看项目结构和示例代码,似乎只需要在其他模块中使用module.vpc.vpc_id即可。

标签: terraform


【解决方案1】:

您可以通过在每个模块中定义输出来将一个模块输出到另一个模块中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-07
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多