<aside>

resource "aws_eip" "mb_private_ec2" {
  count = length(aws_instance.mb_ec2)

  instance = aws_instance.mb_ec2[count.index].id

  tags = {
    Name = "mb_private_ec2_eip_${count.index}"
  }
}

resource "aws_eip_association" "mb_private_ec2_eip" {
  count = length(aws_instance.mb_ec2)

  instance_id = aws_instance.mb_ec2[count.index].id
  allocation_id = aws_eip.mb_private_ec2[count.index].id
}

resource "aws_launch_template" "mb_ec2_launch_template" {
  name_prefix = "mb_ec2_launch_template"
  image_id = data.aws_ami.ubuntu.id
  key_name = "myblog"
  instance_type = "t2.micro"

  network_interfaces {
    security_groups = [var.private_security_group_id]
  }

  iam_instance_profile {
    name = aws_iam_instance_profile.eks_worker_node_profile.name
  }

  block_device_mappings {
    device_name = "/dev/sda1"

    ebs {
      volume_size = "30"
      volume_type = "gp3"
    }
  }

  tag_specifications {
    resource_type = "instance"

    tags = {
      Name = "mb_private_ec2"
    }
  }

  user_data = base64encode(<<EOF
    #!/bin/bash
    set -o xtrace
    /etc/eks/bootstrap.sh ${var.eks_cluster_name}
  EOF
  )
}

resource "aws_iam_instance_profile" "eks_worker_node_profile" {
  name = "eks_worker_node_profile"
  role = var.eks_workernode_role
}

</aside>