provider "aws" {
  region = "us-east-1"
}

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

module "ows_service_owsrequest" {
  source             = "git@github.com:theorchard/terraform-owsrequest.git//?ref=1.0.0"
  policy_description = "Dynamo-${var.environment}-${var.service_name}-owsrequest"
  environment_name   = var.environment
  service_name       = var.service_name
}

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

  environment                              = var.environment
  service_name                             = var.service_name
  aws_region                               = var.aws_region
  commit_sha                               = "latest"
  container_port                           = "8080"
  task_type                                = "web_service"
  desired_task_count                       = 2
  task_cpu                                 = 512
  task_memory                              = 1024
  maximum_capacity                         = 4
  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
  load_balancer_is_internal                = false

  iam_managed_policy_attachments = [
    "arn:aws:iam::437795906767:policy/Dynamo-prod-ows-cloudwatch-logs-owsrequest",
  ]

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

  #Public, because it's a publically available service
  load_balancer_subnets = [
    "subnet-0a384837a4a30790a",
    "subnet-0862a9d380d77464f",
  ]

  prod_https_listener_allow_cidr_blocks = [
    "0.0.0.0/0",
  ]

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      DATADOG_ENV = "${var.environment}-${var.service_name}"
    },
    {
      LOGGER_DSN = "https://${var.environment}-fluentd-applications.theorchard.io:8888/application"
    },
    {
      TWILIO_LOG_GROUP = var.TWILIO_LOG_GROUP
    }
  ]
}

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