provider "aws" {
  region = var.aws_region
}

# Terraform backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/ows-participant/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

module "ows_service_owsrequest" {
  source = "git@github.com:theorchard/terraform-owsrequest.git//"

  environment_name = var.environment
  service_name     = var.ows_service_name
}

module "ows_participant_fargate_environment" {
  source = "git@github.com:theorchard/terraform-fargate.git//"

  environment                              = var.environment
  service_name                             = var.ows_service_name
  aws_region                               = var.aws_region
  commit_sha                               = "latest"
  container_port                           = "8080"
  task_type                                = "web_service"
  desired_task_count                       = 2
  task_cpu                                 = 1024
  task_memory                              = 2048
  maximum_capacity                         = 6
  minimum_capacity                         = 2
  scale_up_adjustment                      = 1
  scale_down_adjustment                    = -1
  route53_zone_id                          = "Z2RSKZ9M7TJ9Z"
  load_balancer_access_logs_s3_bucket_name = "orch-elb-logs"
  vpc_id                                   = "vpc-7f0e841a"
  datadog_function_destination_arn         = "arn:aws:lambda:us-east-1:437795906767:function:DatadogLambdaFunction"
  https_listener_certificate_id            = "a76e4883-28aa-4062-a4cc-c06b8012ae28"
  http_listener_enabled                    = false

  iam_managed_policy_attachments = [
    "arn:aws:iam::437795906767:policy/Dynamo-prod-ows-participant-owsrequest",
    "arn:aws:iam::437795906767:policy/SecretsManager-prod-ows-machine-to-machine-policy"
  ]

  fargate_service_subnets = [
    "subnet-067a6b45b079d7296",
    "subnet-043d235081c966332",
  ]

  load_balancer_subnets = [
    "subnet-067a6b45b079d7296",
    "subnet-043d235081c966332",
  ]

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      LOGGER_DSN = "https://${var.environment}-fluentd-applications.theorchard.io:8888/application"
    },
    {
      DATADOG_ENV = "${var.environment}-${var.ows_service_name}"
    },
    {
      NEO4J_URL = "bolt+routing://${var.environment}-neo4j-cluster.theorchard.io:7687"
    },
    {
      NEO4J_USERNAME = var.ows_service_name
    },
  ]
}

module "fargate_service_dashboard" {
  source           = "git@github.com:theorchard/terraform-datadog.git//modules/service"
  environment      = var.environment
  environment_type = "fargate"
  service_name     = var.ows_service_name
}

