module "default_tags" {
  source             = "git@github.com:theorchard/terraform-default-tags.git//?ref=1.0.0"
  environment        = var.environment
  application_family = var.application_family
  service_name       = var.service_name
}

provider "aws" {
  region = var.region

  default_tags {
    tags = module.default_tags.tags
  }
}

provider "aws" {
  region  = var.region
  alias   = "networking"
  profile = "networking"

  default_tags {
    tags = module.default_tags.tags
  }
}

# Terraform backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "qa/neo4j-cypher-scheduler/augment-metadata/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

data "aws_caller_identity" "current" {}

module "vpc_info" {
  source = "git@github.com:theorchard/terraform-vpc-info.git?ref=3.0.2"

  environment = var.environment
}


module "neo4j_cypher_scheduler_fargate_environment" {
  source = "git@github.com:theorchard/terraform-fargate.git//?ref=5.5.1"

  providers = {
    aws.dns = aws.networking
  }

  environment              = var.environment
  service_name             = "${var.service_name}-${var.service_name_suffix}"
  aws_region               = var.region
  application_family       = var.application_family
  commit_sha               = "latest"
  container_port           = "8080"
  task_type                = "worker"
  desired_task_count       = 0
  task_cpu                 = 512
  task_memory              = 1024
  maximum_capacity         = 1
  minimum_capacity         = 0
  vpc_id                   = module.vpc_info.vpc_id
  fargate_service_subnets  = module.vpc_info.default_private_subnet_ids
  health_check_command     = "pgrep python"
  cloudwatch_event_enabled = false

  iam_managed_policy_attachments = [
    "arn:aws:iam::${data.aws_caller_identity.current.account_id}:policy/SecretsManager-${var.environment}-neo4j-cypher-scheduler-policy",
  ]

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      LOGGER_DSN = "https://${var.environment}-fluentd-applications.theorchard.io:8888/application"
    },
    {
      NEO4J_SOURCE_FOLDER_NAME = var.neo4j_source_folder_name
    },
    {
      CONTINUE_AFTER_ERRORS = "True"
    },
    {
      RUN_IN_BATCHES = "True"
    },
    {
      BATCH_SIZE = 300000
    },
  ]
}
