[英]Terraform to have a for_each and dynamic block in the same resource? Or can I have multiple for_each in a resource statement
Can I have a for_each and dynamic block in the same resource statement?我可以在同一个资源语句中有一个 for_each 和一个动态块吗? Or can I have multiple for_each in a resource statement /// Creating 2 EC2 instances ///或者我可以在资源语句中有多个 for_each /// 创建 2 个 EC2 实例 ///
resource "aws_instance" "ABC" {
count = 2
ami = var.AMIX
instance_type = "t3.small"
subnet_id = xxx
vpc_security_group_ids = [data.aws_security_group.SG.id]
key_name = var.AMIX-KEY
tags = {
Name = abc)
}
}
/// Creating local dict with subnet name and subnet id /// This has 2 subnets for ec2 instance 1 and 2 subnets for ec2 instance 2 /// 使用子网名称和子网 ID 创建本地字典 /// 这有 2 个子网用于 ec2 实例 1,2 个子网用于 ec2 实例 2
locals {
# With List
subnets = flatten([
for subnet_details in aws_subnet.SUBNET : {
subnet_name = subnet_details.tags.Name,
subnet_id = subnet_details.id
} if contains(["abc", "xyz"], subnet_details.tags.Name)
])
}
/// Local subnets output. /// 本地子网 output。
dev = [
+ {
+ subnet_id = "A"
+ subnet_name = "DEV1"
},
+ {
+ subnet_id = "B"
+ subnet_name = "DEV1"
},
+ {
+ subnet_id = "C"
+ subnet_name = "DEV2"
},
+ {
+ subnet_id = "D"
+ subnet_name = "DEV2"
},
]
/// How to attach EC2 instances with Network Interfaces??? /// 如何使用网络接口附加 EC2 实例??? Loop two times in attachment as I need to tie two ec2 instances depending on the subnet_name 2 subnets get ec2-1 and 2 subnets get ec2-2 and based on that they will get the device_index ///在附件中循环两次,因为我需要根据子网名称绑定两个 ec2 实例 2 个子网获得 ec2-1 和 2 个子网获得 ec2-2 并基于此他们将获得 device_index ///
resource "aws_network_interface" "NICS" {
for_each = {
for subnet_id, subnet_name in local.subnets : subnet_id => subnet_name
}
subnet_id = each.value.subnet_id
security_groups = [data.aws_security_group.SG.id]
tags = {
Name = each.value.subnet_name
}
attachment {
instance = ?
device_index = 1
}
}
What about the following version of your code.您的代码的以下版本怎么样。 I haven't run the code, but but you can consider the idea behind it:我没有运行代码,但是你可以考虑一下它背后的想法:
variable "instance_name" {
default = ["Y", "Z"]
}
resource "aws_instance" "ABC" {
for_each = var.instance_name
ami = var.AMIX
instance_type = "t3.small"
subnet_id = xxx
vpc_security_group_ids = [data.aws_security_group.SG.id]
key_name = var.AMIX-KEY
tags = {
Name = each.key
}
}
Then, you would create four NICs based on local.subnets
when you also specify instance name.然后,当您还指定实例名称时,您将基于local.subnets
创建四个 NIC。 Also I would separate creation of NICs from their attachments using aws_network_interface_attachment :此外,我会使用aws_network_interface_attachment将 NIC 的创建与其附件分开:
locals {
subnets = [
{
subnet_id = "A"
subnet_name = "DEV1"
instance_name = "Y"
},
{
subnet_id = "B"
subnet_name = "DEV1"
instance_name = "Y"
},
{
subnet_id = "C"
subnet_name = "DEV2"
instance_name = "Z"
},
{
subnet_id = "D"
subnet_name = "DEV2"
instance_name = "Z"
}
]
}
resource "aws_network_interface" "NICS" {
for_each = {for subnet in local.subnets : (subnet.subnet_name) => subnet}
subnet_id = each.value.subnet_id
security_groups = [data.aws_security_group.SG.id]
tags = {
Name = each.value.subnet_name
}
}
resource "aws_network_interface_attachment" "test" {
for_each = {for subnet in local.subnets : (subnet.subnet_name) => subnet}
instance_id = aws_instance.ABC[each.value.instance_name].id
network_interface_id = aws_network_interface.NICS[each.key].id
device_index = 0
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.