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

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

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

  environment_name   = "${var.Environment}"
  service_name       = "ows-product"
  policy_description = "Dynamo-prod-ows-product-owsrequest"
}

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

  environment_name = "${var.Environment}"
  service_name     = "ows-product-public"
}

# Setup fargate environment.
module "ows_product_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                                 = 2048
  task_memory                              = 4096
  maximum_capacity                         = 4
  minimum_capacity                         = 2
  scale_up_adjustment                      = 1
  scale_down_adjustment                    = -1
  health_check_path                        = "/hello/"
  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"
  # overriding here to add in the graphql_switchboard_vpc (10.120.0.0/16) private subnets 
  prod_https_listener_allow_cidr_blocks = [
    "192.168.31.0/24",
    "10.40.0.0/22",
    "10.10.40.0/22",
    "10.120.0.0/24",
    "10.120.1.0/24",
  ]


  iam_managed_policy_attachments = [
    "arn:aws:iam::437795906767:policy/Dynamo-prod-product_localization-RW",
    "arn:aws:iam::437795906767:policy/SQS-prod-upc_repository-minimal-policy",
    module.ows_product_owsrequest.policy_arn_output,
    module.ows_product_public_owsrequest.policy_arn_output,
  ]

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

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

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

# Setup datadog dashboards.
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}"
  service_4xx_monitor_enabled = false
}
