module "default_tags" {
  source             = "git@github.com:theorchard/terraform-default-tags.git//?ref=2.0.0"
  environment        = var.environment
  application_family = var.application_family
  service_name       = var.service_name
  team_name          = var.team_name
}

provider "aws" {
  region = var.aws_region

  default_tags {
    tags = module.default_tags.tags
  }
}

# Terraform backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "prod-supply-chain-max-terraform-state"
    key     = "prod/reserved-instances/terraform.tfstate"
    region  = "eu-central-1"
    encrypt = "true"
  }
}

# the large instance normalization factor is 4
# the 12xlarge instance normalization factor is 96
# we still have an active reservation untile May 25, 2026, 22:56 (UTC+03:00) - 24 x db.r5.large instances (96)
# we currently have 2 r5.12xlarge instances, the existing reservation covers only half of the usage,
# so we need to purchase 48 x db.r5.large reserved instances to cover everything instead of 24.
# 2 x 96 = 48 x 4
data "aws_rds_reserved_instance_offering" "db_r5_large_reserved_instance_offering" {
  db_instance_class   = "db.r5.large"
  duration            = 1
  offering_type       = "No Upfront"
  product_description = "aurora-mysql"
  multi_az            = false
}

# Reserved RDS Instance 30 x db.r5.large
# 1-year No Upfront commitment for cost optimization
resource "aws_rds_reserved_instance" "rds_r5_large_reserved_instance" {
  offering_id    = data.aws_rds_reserved_instance_offering.db_r5_large_reserved_instance_offering.offering_id
  reservation_id = "aurora-mysql-r5-large-2026-05-26"
  instance_count = 48
}
