Using Terraform 0.13
I have 5 Elastic IPs that were provisioned and I’d like to assign each to an EC2 as I provision them. Each EIP
is given a predictable tag Name
. IE vpn-eip-1
, vpn-eip-2
, etc.
This allows for nice list creations for looking up these values.
[for i in range(1, var.num_of_hosts + 1) : format("vpn-eip-%s", i)]
(Assuming num_of_hosts <= 5
)
Each Elastic IP must remain intact as those have been given out to other teams.
The idea is that my team can scale up and down the number of VPN servers and fill up this Elastic IP allocation if/when there is a spike in demand.
I’m struggling with a looping concept for this using the aws_eip_association
resource in conjunction with aws_instance
resource.
For example, if I wanted to deploy 2 VPN servers.
I’d call something like.
module "my_vpn" {
source = "../../modules/vpn"
deploy_count = 2
}
Which is referencing something like.
resource "aws_instance" "this" {
count = var.deploy_count
ami = var.ami_id
instance_type = var.instance_type
key_name = var.key_name
user_data = file(var.user_data)
subnet_id = var.subnet_id
vpc_security_group_ids = var.security_groups
iam_instance_profile = aws_iam_instance_profile.vpn_profile.name
tags = {
Name = "vpn-host-${count.index + 1}"
}
}
My struggle is, how do I
- Grab each
eip_id
usingaws_eip
as a data source - Assign each
eip_id
to its corresponding EC2
Maybe I’m looking at this wrong, but my thinking is that I can use the deploy_count
to increment an aws_eip
data source in Terraform and pull in the next eip
by tag name based on the predictable naming scheme.
Would love some advice on this design. Maybe I’m over complicating this? Maybe I’m close to a solution and overlooking something?