我是Terraform的新手,想知道是否有办法做到这一点。我想用它自己的安全组创建多个EC2实例,并附加不同大小和类型的卷。
module "ec2_sg" {
source = "../../modules/sgs"
for_each = var.ec2_info
name = each.value.name
description = "Security group for ${each.value.name}"
vpc_id = each.value.vpc
ingress_cidr_blocks = each.value.ingress_cidr_blocks
ingress_rules = each.value.ingress_rules
egress_cidr_blocks = each.value.egress_cidr_blocks
egress_rules = each.value.egress_rules
}
module "ec2_instance" {
source = "../../modules/ec2"
for_each = var.ec2_info
name = each.value.name
ami = var.AMIS.linux_ami
instance_type = each.value.ec2_instance_type
vpc_security_group_ids = module.ec2_sg.security_group_id[each.key]
}
resource "aws_ebs_volume" "volume_disk" {
for_each = var.ec2_info
type = each.value.type
iops = each.value.iops
availability_zone = each.value.availability_zone
size = each.value.size
}
resource "aws_volume_attachment" "volume_disk" {
for_each = var.ec2_info
device_name = each.value.device_name
volume_id = aws_ebs_volume.data1[each.key].id
instance_id = module.ec2_instance.id[each.key]
}
这是我到目前为止已经尝试过的,但我无法让音量部分发挥作用。ec2_info包含关于我要创建的2个不同ec2实例的信息。我应该做什么样的数据操作来实现这一点?你认为对磁盘使用单独的变量会实现这一点吗?类似于:
resource "aws_ebs_volume" "volume_disk" {
for_each = var.disks_info
type = each.value.type
iops = each.value.iops
availability_zone = each.value.availability_zone
size = each.value.size
}
resource "aws_volume_attachment" "volume_disk" {
for_each = var.disks_info
device_name = each.value.device_name
volume_id = aws_ebs_volume.data1[each.key].id
instance_id = module.ec2_instance.id[each.key]
}
但如果我这样做?如何确保每个磁盘都连接到预期的EC2实例?非常感谢。
您可以尝试使用;计数";参数
https://www.terraform.io/docs/language/meta-arguments/count.html