provider "aws" {
  region = var.aws_region
}

provider "aws" {
  alias  = "aws_us_west_2"
  region = "us-west-2"
}

terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "qa/frontend-insights/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

# Creating this to exclude the query string size restriction rule
# We can remove it when we fix this https://theorchard.atlassian.net/browse/IN-12806?focusedCommentId=1735286
# Until then we have some valid URLs that are being blocked by the WAF on the query string size
module "custom_waf" {
  source            = "git@github.com:theorchard/terraform-aws-waf.git//?ref=2.0.0"
  environment       = var.environment
  service_name      = "${var.service_name}-${var.service_name_suffix}"
  aws_region        = var.aws_region
  acl_scope         = "CLOUDFRONT"
  count_waf_enabled = false
  block_waf_enabled = true

  excluded_rules = [
    "SizeRestrictions_QueryString",
  ]
}

module "spa_environment" {
  # checkov:skip=CKV2_AWS_47:Rule requires the AWSManagedRulesAnonymousIpList to be enabled on the WAF, which blocks legitimate traffic.
  # checkov:skip=CKV_AWS_374:Ensure AWS CloudFront web distribution has geo restriction enabled.
  source = "git@github.com:theorchard/terraform-spa.git//?ref=1.4.1"
  providers = {
    aws.primary = aws
    aws.failover = aws.aws_us_west_2
  }

  environment           = var.environment
  service_name          = var.service_name_suffix
  application_family    = var.application_family
  brand                 = var.service_name
  domain_name           = var.domain_name
  default_root_object   = "index.html"
  additional_aliases    = ["${var.service_name_suffix}.${var.domain_name}"]
  custom_logging_bucket = "prod-orcd-cloudfront-logs"
  custom_waf_name       = module.custom_waf.waf_blocking_name_output

  depends_on = [ module.custom_waf ]
}
