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.aws_region

  default_tags {
    tags = module.default_tags.tags
  }
}

terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/neo4j-cypher-scheduler/neo4j-housekeeping/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.1.0"

  environment = var.environment
}

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

  providers = {
    aws.dns = aws
  }

  environment               = var.environment
  service_name              = "${var.service_name}-${var.service_name_suffix}"
  application_family        = var.application_family
  aws_region                = var.aws_region
  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
  health_check_command      = "pgrep python"
  cloudwatch_event_enabled  = true
  cloudwatch_event_schedule = "cron(0 1 1 * ? *)" # Every month at 1am UTC
  vpc_id                    = module.vpc_info.vpc_id

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids

  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 = "False"
    },
  ]
}
