Hi,
I want to assign a single static public ip to an aws instance, using an elastic IP as its own public IP, but I’m seeing some weird behavior that I do not understand why it happens. I create the eip like this:
resource "aws_eip" "eip_manager" {
instance = aws_instance.manager.*.id[count.index]
vpc = true
}
And the aws_resource:
resource "aws_instance" "manager" {
count = var.manager_count
ami = var.manager_ami
instance_type = var.manager_instance_type
iam_instance_profile = aws_iam_instance_profile.ec2-storage-instance-profile.name
key_name = aws_key_pair.default.id
user_data = file(var.bootstrap_path)
vpc_security_group_ids = [aws_security_group.swarm.id]
subnet_id = element(tolist(data.aws_subnet_ids.subnet.ids), count.index)
root_block_device {
volume_size = var.manager_disk_size
volume_type = var.manager_disk_type
delete_on_termination = "true"
}
connection {
type = "ssh"
user = var.ssh_user
private_key = file(var.private_key_default)
host = self.public_ip
}
}
After running terraform appy
the instance is assigned two public IP addresses as expected, one to the instance and the eip. But if I run terraform apply
again (with no code changes) the instance public IP address is replaced with the eip address, leaving the instance with only one public IP. Why is this ??
And if I add associate_public_ip_address = false
to the instance resource then only the eip address is assigned as expected, but again, if I run again terraform apply the instance is proposed to be deleted and recreated. Why is this ?
How should I create an aws instance with only the eip address and avoid that behavior ?
Tested with both terraform 0.12.x and 0.13beta.
Thanks.