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}-${var.neo4j_source_folder_name}"
}

provider "aws" {
  region = var.aws_region

  default_tags {
    tags = module.default_tags.tags
  }
}

# Terraform backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/neo4j-cypher-scheduler/assert-data-quality/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=6.1.0"

  providers = {
    aws.dns = aws
  }

  environment                    = var.environment
  service_name                   = "${var.service_name}-${var.neo4j_source_folder_name}"
  application_family             = var.application_family
  aws_region                     = "us-east-1"
  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       = true
  cloudwatch_event_schedule      = "cron(0 3 * * ? *)" # Every day at 3am UTC
  autoscaling_cpu_policy_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
    },
    {
      ASSERT_RESULTS = "True"
    },
    {
      DISABLE_SENTRY = "True"
    },
    {
      SEND_QUERY_RESULTS_TO_DATADOG = "node-count,relationship-count"
    },
    # Force it to run all assert-data-quality queries even if some fail.
    {
      CONTINUE_AFTER_ERRORS = "true"
    }
  ]
}
