<commit_before>#
#  General cluster autoscale group configurations
#
resource "aws_autoscaling_group" "cluster" {
  vpc_zone_identifier = ["${split(",",var.cluster_vpc_zone_identifiers)}"]
  name = "${var.cluster_name}-${var.asg_name}"
  min_size = "${var.cluster_min_size}"
  max_size = "${var.cluster_max_size}"
  desired_capacity = "${var.cluster_desired_capacity}"
  
  health_check_type = "EC2"
  health_check_grace_period = 300
  force_delete = true
  metrics_granularity = "1Minute"

  launch_configuration = "${aws_launch_configuration.cluster.name}"
  
  tag {
    key = "Name"
    value = "${var.cluster_name}-${var.asg_name}"
    propagate_at_launch = true
  }
}

resource "aws_launch_configuration" "cluster" {
  # use system generated name to allow changes of launch_configuration
  name_prefix = "${var.cluster_name}-${var.asg_name}-"
  image_id = "${var.ami}"
  instance_type = "${var.image_type}"
  iam_instance_profile = "${aws_iam_instance_profile.cluster.name}"
  security_groups = ["${split(",",var.cluster_security_groups)}"]
  key_name = "${var.keypair}"  
  lifecycle { create_before_destroy = true }

  # /root
  root_block_device = {
    volume_type = "${var.root_volume_type}" 
    volume_size = "${var.root_volume_size}" 
  }

  # /var/lib/docker
  ebs_block_device = {
    device_name = "/dev/sdb"
    volume_type = "${var.docker_volume_type}" 
    volume_size = "${var.docker_volume_size}" 
  }

  # /opt/data
  ebs_block_device = {
    device_name = "/dev/sdc"
    volume_type = "${var.data_volume_type}"
    volume_size = "${var.data_volume_size}"
  }

  # instance store device, necessary for instance with ephemeral devices, e.g. m3.
  # no effect for instances without ephemeral disks. 
  ephemeral_block_device  {
    device_name = "/dev/sdd"
    virtual_name = "ephemeral0"
  }

  # cluser user_data
  user_data = "${var.user_data}"
}

# Define policy and role
resource "aws_iam_role_policy" "cluster" {
  name = "${var.asg_name}"
  role = "${aws_iam_role.cluster.id}"
  policy = "${var.iam_role_policy}"
  lifecycle { create_before_destroy = true }
}

# setup the cluster ec2 profile
resource "aws_iam_instance_profile" "cluster" {
  name = "${var.asg_name}"
  roles = ["${aws_iam_role.cluster.name}"]
  lifecycle { create_before_destroy = true }

  # Sleep a little to wait the IAM profile to be ready - 
  # This seems to fix:
  #     aws_launch_configuration.cluster: Error creating launch configuration: ValidationError: You are not authorized to #       perform this operation
  provisioner "local-exec" {
    command = "sleep ${var.wait_time}"
  }
}

resource "aws_iam_role" "cluster" {
  name = "${var.asg_name}"
  path = "/"
  lifecycle { create_before_destroy = true }
  assume_role_policy =  <<EOF
{<commit_msg>renamed module resource name from 'cluster' to 'instance_pool'.<commit_after>#
#  General cluster autoscale group configurations
#
resource "aws_autoscaling_group" "instance_pool" {
  vpc_zone_identifier = ["${split(",",var.cluster_vpc_zone_identifiers)}"]
  name = "${var.cluster_name}-${var.asg_name}"
  min_size = "${var.cluster_min_size}"
  max_size = "${var.cluster_max_size}"
  desired_capacity = "${var.cluster_desired_capacity}"
  
  health_check_type = "EC2"
  health_check_grace_period = 300
  force_delete = true
  metrics_granularity = "1Minute"

  launch_configuration = "${aws_launch_configuration.instance_pool.name}"
  
  tag {
    key = "Name"
    value = "${var.cluster_name}-${var.asg_name}"
    propagate_at_launch = true
  }
}

resource "aws_launch_configuration" "instance_pool" {
  # use system generated name to allow changes of launch_configuration
  name_prefix = "${var.cluster_name}-${var.asg_name}-"
  image_id = "${var.ami}"
  instance_type = "${var.image_type}"
  iam_instance_profile = "${aws_iam_instance_profile.instance_pool.name}"
  security_groups = ["${split(",",var.cluster_security_groups)}"]
  key_name = "${var.keypair}"  
  lifecycle { create_before_destroy = true }

  # /root
  root_block_device = {
    volume_type = "${var.root_volume_type}" 
    volume_size = "${var.root_volume_size}" 
  }

  # /var/lib/docker
  ebs_block_device = {
    device_name = "/dev/sdb"
    volume_type = "${var.docker_volume_type}" 
    volume_size = "${var.docker_volume_size}" 
  }

  # /opt/data
  ebs_block_device = {
    device_name = "/dev/sdc"
    volume_type = "${var.data_volume_type}"
    volume_size = "${var.data_volume_size}"
  }

  # instance store device, necessary for instance with ephemeral devices, e.g. m3.
  # no effect for instances without ephemeral disks. 
  ephemeral_block_device  {
    device_name = "/dev/sdd"
    virtual_name = "ephemeral0"
  }

  # cluser user_data
  user_data = "${var.user_data}"
}

# Define policy and role
resource "aws_iam_role_policy" "instance_pool" {
  name = "${var.asg_name}"
  role = "${aws_iam_role.instance_pool.id}"
  policy = "${var.iam_role_policy}"
  lifecycle { create_before_destroy = true }
}

# setup ec2 instance profile
resource "aws_iam_instance_profile" "instance_pool" {
  name = "${var.asg_name}"
  roles = ["${aws_iam_role.instance_pool.name}"]
  lifecycle { create_before_destroy = true }

  # Sleep a little to wait the IAM profile to be ready - 
  # This seems to fix:
  #     aws_launch_configuration.instance_pool: Error creating launch configuration: ValidationError: You are not authorized to #       perform this operation
  provisioner "local-exec" {
    command = "sleep ${var.wait_time}"
  }
}

resource "aws_iam_role" "instance_pool" {
  name = "${var.asg_name}"
  path = "/"
  lifecycle { create_before_destroy = true }
  assume_role_policy =  <<EOF
{
{'fraction_non_alphanumeric': 0.12929041436955363, 'fraction_numerical': 0.002845456162191001, 'mean_word_length': 4.226765799256506, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1849167', 'n_tokens_mistral': 1964, 'n_tokens_neox': 1909, 'n_words': 382}