resource "aws_launch_template" "main" {
  name                    = var.name
  image_id                = var.ami
  instance_type           = var.instance_type
  disable_api_termination = var.termination_protection
  key_name                = var.key_name
  ebs_optimized           = true
  vpc_security_group_ids  = var.security_groups
  tags                    = var.common_tags

  block_device_mappings {
    device_name = var.device_name
    ebs {
      volume_size = var.root_volume_size
      encrypted   = true
      kms_key_id  = var.kms_key_id
    }
  }

  iam_instance_profile {
    name = var.iam_instance_profile
  }

  /*
  instance_market_options {
    count = "${ var.is_spot == "true" ? 1 : 0}"
    market_type = "spot"
    spot_options {
      spot_instance_type = "one-time"
    }
  }
*/

  monitoring {
    enabled = true
  }

  network_interfaces {
    associate_public_ip_address = var.is_public
    delete_on_termination       = true
  }

  tag_specifications {
    resource_type = "instance"
    tags = merge(
      var.instance_tags,
      var.common_tags,
      {
        "Name" = format("%v-%v", var.name_prefix, var.instance_layer)
      },
    )
  }

  tag_specifications {
    resource_type = "volume"
    tags = merge(
      var.common_tags,
      {
        "Name" = format("%v-%v", var.name_prefix, var.instance_layer)
      },
    )
  }
}

