# TF GENERAL

terraform {
  backend "s3" {
    bucket                  = "dev-dna-tfstate"
    key                     = "Apollo/Development/batch.tfstate"
    region                  = "us-east-1"
    shared_credentials_file = "~/.aws/credentials"
    profile                 = "gdb-delphi-dev"
    dynamodb_table          = "terraform-state-lock-dynamodb"
  }
}


provider "aws" {
  region  = var.aws_region_id
  profile = var.aws_profile
}


data "aws_caller_identity" "current" {}

data "aws_ami" "latest_ecs" {
  most_recent = true
  owners      = ["591542846629"] # AWS

  filter {
    name   = "name"
    values = ["*amazon-ecs-optimized"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }
}

locals {
  account_id = data.aws_caller_identity.current.account_id

  common_tags = {
    platform          = var.platform["name"]
    environment       = var.platform["environment"]
    platform_with_env = "${var.platform["name"]} ${var.platform["environment"]}"
  }
  //"
  image_id = var.ce_image == "" ? data.aws_ami.latest_ecs.id : var.ce_image
}


# AWS IAM role ApolloETLBatchRole

resource "aws_iam_role" "apolloetlbatch-r" {
  name        = "ApolloETLBatchRole"
  description = "Allows ECS tasks to call AWS services on your behalf"

  assume_role_policy = <<EOF
{
    "Version": "2012-10-17",
    "Statement": [
    {
        "Action": "sts:AssumeRole",
        "Effect": "Allow",
        "Principal": {
        "Service": "ecs-tasks.amazonaws.com"
        }
    }
    ]
}
EOF

  tags = {
    application = "apollo-etl"
    project     = "apollo"
  }
}


resource "aws_iam_role_policy_attachment" "apolloetlbatch-pa" {
  role       = aws_iam_role.apolloetlbatch-r.name
  policy_arn = "arn:aws:iam::aws:policy/AmazonS3FullAccess"
}

# AWS IAM role ApolloETLDBIngestBatchRole

resource "aws_iam_role" "apolloetldbingestbatch-r" {
  name = "ApolloETLDBIngestBatchRole"

  assume_role_policy = <<EOF
{
    "Version": "2012-10-17",
    "Statement": [
    {
        "Action": "sts:AssumeRole",
        "Effect": "Allow",
        "Principal": {
        "Service": "ecs-tasks.amazonaws.com"
        }
    }
    ]
}
EOF
}


resource "aws_iam_role_policy_attachment" "apolloetldbingestbatch-pa1" {
  role       = aws_iam_role.apolloetldbingestbatch-r.name
  policy_arn = "arn:aws:iam::aws:policy/AmazonS3FullAccess"
}


resource "aws_iam_role_policy_attachment" "apolloetldbingestbatch-pa2" {
  role       = aws_iam_role.apolloetldbingestbatch-r.name
  policy_arn = "arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess"
}

# AWS EC2 launch template

resource "aws_launch_template" "apollo-etl-batch-lt" {
  name = "${var.platform["prefix"]}-apollo-etl-batch-tmpl"

  block_device_mappings {
    device_name = "/dev/xvda"

    ebs {
      volume_size           = 10
      volume_type           = "gp2"
      encrypted             = "false"
      delete_on_termination = "true"
    }
  }

  block_device_mappings {
    device_name = "/dev/xvdcz"

    ebs {
      volume_size           = 80
      volume_type           = "io1"
      iops                  = 4000
      encrypted             = "false"
      delete_on_termination = "true"
    }
  }

  image_id = local.image_id

  tag_specifications {
    resource_type = "instance"
    tags = merge(
      local.common_tags,
      {
        SERVER_TYPE = "ECS"
      }
    )
  }

  tag_specifications {
    resource_type = "volume"
    tags          = local.common_tags
  }
}

# AWS Batch compute environments

resource "aws_batch_compute_environment" "apollo-etl-test1-ce" {
  compute_environment_name = "${var.platform["prefix"]}-apollo-etl-test1"

  state = "DISABLED"

  lifecycle {
    ignore_changes = [compute_resources[0].desired_vcpus]
  }

  compute_resources {
    instance_role = "arn:aws:iam::475275892927:instance-profile/ecsInstanceRole"

    instance_type = [
      #      "optimal"
      "c5.xlarge"
    ]

    launch_template {
      #      launch_template_id = "lt-0f6125855b2afdbb8"
      launch_template_name = aws_launch_template.apollo-etl-batch-lt.name
      version              = "$Latest"
      #      version = "1"
    }

    #    image_id = var.ce_image

    max_vcpus     = 100
    min_vcpus     = 0
    desired_vcpus = 100

    security_group_ids = var.security_groups

    ec2_key_pair = var.ec2_key_pair

    subnets = var.subnets

    type = "EC2"
  }

  service_role = "arn:aws:iam::475275892927:role/service-role/AWSBatchServiceRole"
  type         = "MANAGED"
}


resource "aws_batch_compute_environment" "apollo-etl-test2-ce" {
  compute_environment_name = "${var.platform["prefix"]}-apollo-etl-test2"

  state = "DISABLED"

  lifecycle {
    ignore_changes = [compute_resources[0].desired_vcpus]
  }

  compute_resources {
    instance_role = "arn:aws:iam::475275892927:instance-profile/ecsInstanceRole"

    instance_type = [
      #      "optimal"
      "c5.xlarge"
    ]

    launch_template {
      launch_template_name = aws_launch_template.apollo-etl-batch-lt.name
      version              = "$Latest"
    }

    max_vcpus     = 80
    min_vcpus     = 0
    desired_vcpus = 80

    security_group_ids = var.security_groups

    ec2_key_pair = var.ec2_key_pair

    subnets = var.subnets

    type = "EC2"
  }

  service_role = "arn:aws:iam::475275892927:role/service-role/AWSBatchServiceRole"
  type         = "MANAGED"
}


resource "aws_batch_compute_environment" "apollo-etl-test3-ce" {
  compute_environment_name = "${var.platform["prefix"]}-apollo-etl-test3"

  state = "DISABLED"

  lifecycle {
    ignore_changes = [compute_resources[0].desired_vcpus]
  }

  compute_resources {
    instance_role = "arn:aws:iam::475275892927:instance-profile/ecsInstanceRole"

    instance_type = [
      #      "optimal"
      "c5.xlarge"
    ]

    launch_template {
      launch_template_name = aws_launch_template.apollo-etl-batch-lt.name
      version              = "$Latest"
    }

    max_vcpus     = 100
    min_vcpus     = 0
    desired_vcpus = 100

    security_group_ids = var.security_groups

    ec2_key_pair = var.ec2_key_pair

    subnets = var.subnets

    type = "EC2"
  }

  service_role = "arn:aws:iam::475275892927:role/service-role/AWSBatchServiceRole"
  type         = "MANAGED"
}

# AWS Batch job queues

resource "aws_batch_job_queue" "apollo-etl-prepare-for-athena-q" {
  name     = "${var.platform["prefix"]}-apollo-etl-prepare-for-athena-8"
  state    = "DISABLED"
  priority = "1"
  compute_environments = [
    aws_batch_compute_environment.apollo-etl-test1-ce.arn,
  ]
}


resource "aws_batch_job_queue" "apollo-etl-agg-report-split-q" {
  name     = "${var.platform["prefix"]}-apollo-etl-agg-report-split"
  state    = "DISABLED"
  priority = "1"
  compute_environments = [
    aws_batch_compute_environment.apollo-etl-test2-ce.arn,
  ]
}


resource "aws_batch_job_queue" "apollo-dbingest-q" {
  name     = "${var.platform["prefix"]}-apollo-etl-dbingest-2"
  state    = "DISABLED"
  priority = "1"
  compute_environments = [
    aws_batch_compute_environment.apollo-etl-test3-ce.arn,
  ]
}

# AWS Batch job definitions

resource "aws_batch_job_definition" "apollo-start_prepare_for_athena-jd" {
  name = "${var.platform["prefix"]}-apollo-start_prepare_for_athena"
  type = "container"
  timeout { attempt_duration_seconds = 3600 }
  retry_strategy { attempts = 3 }

  #  lifecycle {
  #    ignore_changes = [ "container_properties" ]
  #  }

  container_properties = templatefile("${path.module}/container_props.tmpl", {
    account_id    = local.account_id
    aws_region_id = var.aws_region_id
    job_image     = var.job_image["prepare_for_athena"]
    memory        = 6144
    cpu           = 1
    job_role_arn  = aws_iam_role.apolloetlbatch-r.arn
  })
}


resource "aws_batch_job_definition" "apollo-agg_report_split-jd" {
  name = "${var.platform["prefix"]}-apollo-agg_report_split"
  type = "container"
  timeout { attempt_duration_seconds = 3600 }
  retry_strategy { attempts = 2 }

  #  lifecycle {
  #    ignore_changes = [ "container_properties" ]
  #  }

  container_properties = templatefile("${path.module}/container_props.tmpl", {
    account_id    = local.account_id
    aws_region_id = var.aws_region_id
    job_image     = var.job_image["agg_report_split"]
    memory        = 2048
    cpu           = 2
    job_role_arn  = aws_iam_role.apolloetlbatch-r.arn
  })
}


resource "aws_batch_job_definition" "apollo-dbingest-jd" {
  name = "${var.platform["prefix"]}-apollo-dbingest"
  type = "container"
  timeout { attempt_duration_seconds = 1800 }
  retry_strategy { attempts = 3 }

  #  lifecycle {
  #    ignore_changes = [ "container_properties" ]
  #  }

  container_properties = templatefile("${path.module}/container_props.tmpl", {
    account_id    = local.account_id
    aws_region_id = var.aws_region_id
    job_image     = var.job_image["apollo-dbingest"]
    memory        = 2048
    cpu           = 2
    job_role_arn  = aws_iam_role.apolloetldbingestbatch-r.arn
  })
}
