provider "aws" {
  region = "${var.region}"
}

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

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

  environment                              = "${var.Environment}"
  service_name                             = "${var.service_name}"
  aws_region                               = "${var.region}"
  commit_sha                               = "latest"
  container_port                           = "8080"
  task_type                                = "web_service"
  desired_task_count                       = 1
  task_cpu                                 = 512
  task_memory                              = 1024
  maximum_capacity                         = 4
  minimum_capacity                         = 1
  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
  target_deregistration_delay              = 5

  iam_managed_policy_attachments = [
    "arn:aws:iam::437795906767:policy/Dynamo-qa-ows-features-owsrequest",
  ]

  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"
    },
    {
      LOGGER_LEVEL = "${var.LOGGER_LEVEL}"
    },
    {
      REDIS_CACHE_TTL = "${var.REDIS_CACHE_TTL}"
    },
    {
      DATADOG_ENV = "${var.Environment}-${var.service_name}"
    },
    {
      DD_APM_ANALYZED_SPANS = "${var.DD_APM_ANALYZED_SPANS}"
    },
    {
      SPLITIO_ENABLED = ""
    },
  ]
}

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

  environment             = "qa"
  service_name            = "ows-features"
  cache_engine            = "redis"
  cache_subnet_group_name = "prod-vpc-orchard-subnet"
  vpc_id                  = "vpc-7f0e841a"
}

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