在另一个模块中为 EC2 添加指定子网 - AWS Terraform -
Posted
技术标签:
【中文标题】在另一个模块中为 EC2 添加指定子网 - AWS Terraform -【英文标题】:Add specify Subnet for EC2 in another module - AWS Terraform - 【发布时间】:2021-12-29 22:30:05 【问题描述】:我是 Terraform 新手。 我有 vpc 模块和 ec2 模块。
VPC 模块创建 3 个公共子网。 Ec2 模块创建 2 个实例。-> 所以我想要第一个实例使用的公共子网 1,第二个实例使用的公共子网 3。我使用了输出变量,但我不知道如何使用输出。请帮帮我
提供者是 AWS
VPC 模块
#/module/vpc/main.tf
resource "aws_subnet" "pub_subnet"
count = var.create_vpc && length(var.public_subnet) > 0 ? length(var.public_subnet) : 0
vpc_id = local.vpc_id
availability_zone = element(var.azs, count.index)
cidr_block = element(var.public_subnet, count.index)
map_public_ip_on_launch = true
# network_acls = aws_network_acl.nacls
tags = merge (
Name = format("Pub_subnet %s", element(var.public_subnet, count.index))
,
var.tags
)
#module/vpc/output
output "public_subnet_id"
value = aws_subnet.pub_subnet[0].id
EC2 模块
#/module/ec2/variables
variable "ec2_instance"
type = map(object(
ec2_name = string
ec2_ami = string
ec2_instance_type = string
ec2_subnet_id = string
ec2_associate_public_ip_address = string
ec2_key_name = string
ec2_security_groups = list(string)
ec2_user_data = string
root_block_device_volume_type = string
root_block_device_volume_size = number
root_block_device_iops = number
root_block_device_throughput = number
))
#/module/vpc/main.tf
resource "aws_instance" "ec2"
for_each = var.ec2_instance
ami = each.value.ec2_ami
instance_type = each.value.ec2_instance_type
subnet_id = each.value.ec2_subnet_id
associate_public_ip_address = each.value.ec2_associate_public_ip_address
key_name = each.value.ec2_key_name
security_groups = each.value.ec2_security_groups
user_data = each.value.ec2_user_data
root_block_device
volume_type = each.value.root_block_device_volume_type
volume_size = each.value.root_block_device_volume_size
iops = each.value.root_block_device_iops
throughput = each.value.root_block_device_throughput
tags = merge (
Name = format("%s", each.value.ec2_name)
,
var.tags
)
根模块
#/main.tf
module "vpc"
source = "../../modules/ec2"
public_subnet = [ "13.9.1.0/24", "13.9.2.0/24", "13.9.7.0/24" ]
module "ec2"
source = "../../modules/ec2"
ec2_instance =
"bastion_pub_sub_1" =
ec2_name = "bastion_pub_sub_1"
ec2_ami = data.aws_ami.ubuntu_20_04.id
ec2_associate_public_ip_address = true
ec2_instance_type = "t2.micro"
ec2_key_name = #secret
ec2_subnet_id = module.vpc.public_subnet_id
ec2_user_data = "value"
root_block_device_iops = 3000
root_block_device_throughput = 125
root_block_device_volume_size = 8
root_block_device_volume_type = "gp3"
,
source = "../../qa_modules/ec2"
ec2_instance =
"bastion_pub_sub_2" =
ec2_name = "bastion_pub_sub_2"
ec2_ami = data.aws_ami.ubuntu_20_04.id
ec2_associate_public_ip_address = true
ec2_instance_type = "t2.micro"
ec2_key_name = #secret
ec2_subnet_id = module.vpc.public_subnet_id
ec2_user_data = "value"
root_block_device_iops = 3000
root_block_device_throughput = 125
root_block_device_volume_size = 8
root_block_device_volume_type = "gp3"
所以,在 ec2_subnet_id = module.vpc.public_subnet_id,我如何添加子网“13.9.1.0/24”,例如“bastion_pub_sub_1”,子网“13.9.7.0/24” “bastion_pub_sub_2”
【问题讨论】:
你必须展示你的代码,解释它有什么问题,提供任何错误,并准确描述你想要实现的目标。 @Marcin 对不起,我有更新。请帮帮我 您有多少个子网?var.public_subnet
是什么?
@Marcin 更新了子网。我有 3 个子网 [“13.9.1.0/24”、“13.9.2.0/24”、“13.9.7.0/24”]。和 2 个实例
【参考方案1】:
你的输出应该是:
output "public_subnet_id"
value = aws_subnet.pub_subnet[*].id
然后:
module "ec2"
source = "../../modules/ec2"
ec2_instance =
"bastion_pub_sub_1" =
ec2_name = "bastion_pub_sub_1"
ec2_ami = data.aws_ami.ubuntu_20_04.id
ec2_associate_public_ip_address = true
ec2_instance_type = "t2.micro"
ec2_key_name = #secret
ec2_subnet_id = module.vpc.public_subnet_id[0]
ec2_user_data = "value"
root_block_device_iops = 3000
root_block_device_throughput = 125
root_block_device_volume_size = 8
root_block_device_volume_type = "gp3"
,
source = "../../qa_modules/ec2"
ec2_instance =
"bastion_pub_sub_2" =
ec2_name = "bastion_pub_sub_2"
ec2_ami = data.aws_ami.ubuntu_20_04.id
ec2_associate_public_ip_address = true
ec2_instance_type = "t2.micro"
ec2_key_name = #secret
ec2_subnet_id = module.vpc.public_subnet_id[2]
ec2_user_data = "value"
root_block_device_iops = 3000
root_block_device_throughput = 125
root_block_device_volume_size = 8
root_block_device_volume_type = "gp3"
【讨论】:
好的,谢谢。让我试试 不错!!!谢谢你的帮助,我解决了。我花了 1 天时间解决。感谢上帝,你在这里 @LamboOP 没问题。如果有帮助,我们将不胜感激。 我认为这个答案很有用,但我是 *** 的新成员 :( @LamboOP 要接受必须按此处所示的勾号meta.stackexchange.com/a/86979以上是关于在另一个模块中为 EC2 添加指定子网 - AWS Terraform -的主要内容,如果未能解决你的问题,请参考以下文章