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
  }
}

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

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

  default_tags {
    tags = module.default_tags.tags
  }
}

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_refresh_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.neo4j_source_folder_name}"
  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"

  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
    },
    {
      NEO4J_SOURCE_FOLDER_NAME = var.neo4j_source_folder_name
    },
    {
      NEO4J_URL = "neo4j+s://qa-neo4j-cluster.theorchard.io:7687"
    },
  ]
}
