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

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

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

  environment_name = "${var.environment}"
  service_name     = "${var.service_name}"
}

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

  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
  route53_zone_id                          = "Z2RSKZ9M7TJ9Z"
  load_balancer_access_logs_s3_bucket_name = "orch-elb-logs"
  vpc_id                                   = "vpc-7f0e841a"
  https_listener_certificate_id            = "a76e4883-28aa-4062-a4cc-c06b8012ae28"
  http_listener_enabled                    = false

  iam_managed_policy_attachments = [
    "arn:aws:iam::437795906767:policy/Dynamo-qa-ows-product-configuration-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"
    },
    {
      DATADOG_ENV = "${var.environment}-${var.service_name}"
    },
    {
      DATADOG_PATCH_MODULES = "requests:true"
    },
    {
      DATADOG_TRACE_DEBUG = "true"
    },
    {
      DD_APM_ANALYZED_SPANS = "flask|flask.request=1,requests|requests.request=1,aws.dynamodb|dynamodb.command=1,aws.secretsmanager|secretsmanager.command=1"
    },
    {
      DD_LOGS_INJECTION = true
    },
  ]
}

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