gpt4 book ai didi

amazon-ec2 - 将 AWS 安全组附加到多个 EC2 实例

转载 作者:行者123 更新时间:2023-12-03 08:45:20 25 4
gpt4 key购买 nike

我正在启动多个 Amazon EC2 实例,并且需要附加一个安全组。我能够为一个 EC2 实例实现这一目标,但正在寻找针对多个 EC2 的解决方案。我正在使用 TerraForm 0.12。请让我知道如何使用数据资源:- 数据“aws_instances”。

以下是单个 EC2 的代码,我正在尝试将其转换为多个 EC2:

    resource "aws_instance" "ec2_instance" {
count = "${var.ec2_instance_count}"
ami = "${data.aws_ami.app_qrm_ami.id}"
...
}
data "aws_instances" "ec2_instances" {
count = "${var.ec2_instance_count}"
filter {
name = "instance-id"
values = ["${aws_instance.ec2_instance.*.id[count.index]}"]
}
}
resource "aws_network_interface_sg_attachment" "sg_attachment" {
security_group_id = "${data.aws_security_group.security_group.id}"
network_interface_id = "${data.aws_instance.ec2_instance[count.index].network_interface_id}" //facing issues here.
}

我想使用数据“aws_instances”来实现这一点#notice (s)。提前致谢。

最佳答案

要删除 ec2 AMI 的硬编码,您可以使用以下数据提供程序:-

  data "aws_ami" "amazon_linux" {
count = "${var.ec2_instance_count}"
most_recent = true
owners = ["amazon"]

filter {
name = "name"
values = [
"amzn-ami-hvm-*-x86_64-gp2",
]
}

filter {
name = "owner-alias"
values = [
"amazon",
]
}
}

用于渲染 ami id:-

resource "aws_instance" "ec2_instance" {
count = "${var.ec2_instance_count}"
ami = "${data.aws_ami.amazon_linux[count.index].id}"
network_interface =

获取network_interface_id:-

resource "aws_network_interface" "ec2_nic" {
count = "${var.ec2_instance_count}"
subnet_id = "${aws_subnet.public_a.id}"
private_ips = ["10.0.0.50"]
security_groups = ["${aws_security_group.web.id}"]

attachment {
instance = "${aws_instance.ec2_instance[count.index].id}"
}
}

resource "aws_network_interface_sg_attachment" "sg_attachment" {
security_group_id = "${data.aws_security_group.security_group.id}"
network_interface_id = "${aws_network_interface.ec2_ami[count.index].id}"
}

关于amazon-ec2 - 将 AWS 安全组附加到多个 EC2 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61707485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com