This is my first post here, so Ignore if you find anything inconsistent with the forum.
Has anyone observed this issue where Terraform Created Duplicate EC2 instances with same name?
I just today observed i had created 15 terraform templates for 15 EC2 instances and I had 30 instances running with 2 instances having same name?
Can anyone point out why is this happening?
Terrraform Version:
Terraform v0.13.7
+ provider registry.terraform.io/hashicorp/aws v4.11.0
+ provider registry.terraform.io/hashicorp/null v3.1.1
+ provider registry.terraform.io/hashicorp/template v2.2.0
Sample Terraform Template
data "template_file" "instance" {
template = file("userdata_proxy.sh.tmpl")
vars = {
log_server = "<server>"
log_server_port = "<server_port>"
CLUSTER_NAME = "<cluster_name>"
CONTAINER_NAME = "<cluster_name>"
env = "<env>"
group_name = "<group>"
service_name = "<service_name>"
region = "us-east-1"
influx_url = "<influx_url>"
influx_app_url = "<influx_url>"
influx_pass = "<influx_pass"
influx_rp = "1y"
influx_app_rp = ""
influx_database = "<influx_db"
influx_username = "<influx_user"
role = "<role>"
CDEF = "<cdef>"
proxy_id = "<id>"
cluster_id = "<cluster_id>"
cloud_fqdn = "<cloud_fqdn>"
hostname = "<hostname>"
}
}
resource "aws_instance" "instance" {
ami = data.aws_ami.ami.image_id
instance_type = "c5.2xlarge"
key_name = "<key_name>"
subnet_id = "<subnet_id>"
vpc_security_group_ids = [data.aws_security_group.sg.id]
iam_instance_profile = "<iam_instance_profile>"
user_data = data.template_file.instance.rendered
root_block_device {
delete_on_termination = true
volume_type = "gp3"
encrypted = true
}
tags = {
< tags >
}
}
resource "null_resource" "create_tags_instance" {
triggers = {
instance_id = aws_instance.instance.id
}
provisioner "local-exec" {
command = <<EOT
ResourceId=$(aws ec2 describe-volumes --filters Name="attachment.instance-id",Values="${aws_instance.instance.id}" --profile <profile> --region us-east-1 | jq -r .Volumes[].VolumeId) && \
aws ec2 create-tags --profile <profile> --region us-east-1 --resources $${ResourceId} \
--tags Key="Name",Value="<value>"
EOT
}
}
data "aws_eip" "eip_instance" {
public_ip = "<eip>"
}
resource "aws_eip_association" "eip_association_instance" {
instance_id = aws_instance.instance.id
allocation_id = data.aws_eip.eip_instance.id
}```