在将eip附加到ec2实例时遇到问题。我只能这样做,就是分配公共ip,然后在构建实例之后附加eip。我想在实例生成之前添加EIP。
resource "aws_instance" "third-WG" {
provider = aws.third
ami = data.aws_ami.ubuntu3.id
instance_type = "t2.micro"
key_name = aws_key_pair.third_WGkey.key_name
associate_public_ip_address = true
private_ip = "10.3.0.10"
subnet_id = aws_subnet.third-WG-Subnet.id
vpc_security_group_ids = [aws_security_group.third-SG.id]
depends_on = [aws_instance.WG_DL_SRV]
}
resource "aws_eip" "third_WG_EIP" {
provider = aws.third
vpc = true
instance = aws_instance.third-WG.id
depends_on = [aws_internet_gateway.third-IGW]
provisioner "local-exec" {
command = "echo ${aws_eip.third_WG_EIP.public_ip} > ~/Desktop/whole-
wash/var/3rd_PUB_IP.txt"
}
tags = {
Name = "third_WG_EIP"
}
}发布于 2021-05-15 21:04:34
您可以创建一个网络接口,而不是将弹性IP附加到实例,从而使该实例成为弹性IP的依赖关系,迫使首先构建它,并将弹性IP附加到该接口上。然后,您还将使实例使用该接口,因此,所有内容都将一起进行处理。
resource "aws_network_interface" "nic" {
private_ips = ["10.3.0.10"]
subnet_id = aws_subnet.third-WG-Subnet.id
#Todo: populate any other needed NIC parameters
}
resource "aws_eip" "third_WG_EIP" {
provider = aws.third
vpc = true
network_interface = aws_network_interface.nic
depends_on = [aws_internet_gateway.third-IGW]
provisioner "local-exec" {
command = "echo ${aws_eip.third_WG_EIP.public_ip} > ~/Desktop/whole-
wash/var/3rd_PUB_IP.txt"
}
tags = {
Name = "third_WG_EIP"
}
}
resource "aws_instance" "third-WG" {
provider = aws.third
ami = data.aws_ami.ubuntu3.id
instance_type = "t2.micro"
key_name = aws_key_pair.third_WGkey.key_name
associate_public_ip_address = true
vpc_security_group_ids = [aws_security_group.third-SG.id]
depends_on = [aws_instance.WG_DL_SRV]
network_interface {
network_interface_id = aws_network_interface.nic.id
device_index = 0
}
}如果您发现它的顺序仍然有点错误,那么还可以将EIP添加到实例的depends_on参数中,因为EIP不再依赖于实例本身。
注意,我已经很久没有这样做了,您可能会发现需要将其他一些网络类型参数(安全组等)从实例中移到网络接口块中。
https://stackoverflow.com/questions/67551132
复制相似问题