Hi @solarflow99,
The issue you linked to is from eight years ago, from the very early days of Terraform. I would not suggest paying much attention to anything you see in that issue.
With that said, I’m a little unsure what exactly you are aiming to achieve from the information you shared. There’s not really anything special about the private_ip
argument of aws_instance
from Terraform’s own perspective, so the same techniques you might use for populating any argument based on count.index
could apply here. The main thing is deciding what the rule is for assigning a unique IP address to each of your instances.
If you want to assign consecutive IP addresses starting from a particular address then you could potentially do that with something like the following:
variable "instances" {
type = object({
count = number
cidr_prefix = string
first_host_num = optional(number, 1)
})
nullable = false
}
resource "aws_instance" "example" {
count = var.instances.count
# ...
private_ip = cidrhost(
var.instances.cidr_prefix,
var.instances.first_host_num + count.index,
)
}
The main interesting thing going on here is using the cidrhost
function to calculate an IP address to use. This requires both the CIDR block of the network to create the addresses in and the number to assign to the first instance. The others will then be assigned consecutive addresses after that, as long as there are enough unique addresses in the given subnet.
For example, if you set the input variable like this…
instances = {
count = 3
cidr_prefix = "192.168.1.0/24"
first_host_num = 5
}
…then that should generate the following IP addresses:
aws_instance.example[0]
has 192.168.1.5
aws_instance.example[1]
has 192.168.1.6
aws_instance.example[2]
has 192.168.1.7
Another possibility would be to have the user of your module specify exactly which IP addresses to use, but if that were the goal then I think it would make more sense to use for_each
using the IP addresses as the tracking keys, since count.index
wouldn’t mean anything for these.
variable "instances" {
type = object({
ip_addrs = set(string)
})
nullable = false
}
resource "aws_instance" "example" {
for_each = var.instances.ip_addrs
# ...
private_ip = each.value
}
In this case the user of the module might set the variable like this…
instances = {
ip_addrs = [
"192.168.3.4",
"192.168.3.56",
"192.168.3.129",
]
}
…which would declare instances like this:
aws_instance.example["192.168.3.4"]
has 192.168.3.4
aws_instance.example["192.168.3.56"]
has 192.168.3.56
aws_instance.example["192.168.3.129"]
has 192.168.3.129
Adding and removing elements from that ip_addrs
set would then represent adding and removing EC2 instances. The number of elements of the set is the desired number of instances, and the values specify the fixed IP addresses to use.