rds.tf 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. data "aws_rds_certificate" "latest" {
  2. latest_valid_till = true
  3. id = "rds-ca-rsa4096-g1"
  4. }
  5. locals {
  6. # GovCloud and Commercial use different CA certs
  7. ca_cert_identifier = var.aws_partition == "aws" ? "rds-ca-2019" : "rds-ca-rsa4096-g1"
  8. }
  9. output "ca_cert_identifier" {
  10. value = {
  11. "current" : local.ca_cert_identifier,
  12. "latest" : data.aws_rds_certificate.latest.id
  13. }
  14. }
  15. resource "random_password" "password" {
  16. keepers = {
  17. "version" : 1 # increment to change the password
  18. # n.b. you could add other stuff to make this change automatically, e.g.
  19. # "instance_type": var.instance_type
  20. # Would then change this password every time the instance type changes.
  21. }
  22. length = 32
  23. special = true
  24. min_lower = 1
  25. min_numeric = 1
  26. min_upper = 1
  27. min_special = 1
  28. override_special = "~!%^()-_+"
  29. }
  30. module "rhsso_db" {
  31. source = "terraform-aws-modules/rds/aws"
  32. version = "v4.2.0"
  33. identifier = var.identifier # this is the RDS identifier, not the DB name
  34. db_name = "rhsso" # the DB name
  35. engine = "postgres"
  36. #engine_version = "12.7" # leave this disabled if you're doing auto_minor_version upgrades
  37. instance_class = "db.t3.micro"
  38. allocated_storage = var.rds_storage
  39. storage_encrypted = true
  40. kms_key_id = module.rhsso_key.key_arn
  41. apply_immediately = true # do not wait for maintenance window for changes
  42. ca_cert_identifier = local.ca_cert_identifier
  43. auto_minor_version_upgrade = true
  44. allow_major_version_upgrade = false
  45. # NOTE: Do NOT use 'user' as the value for 'username' as it throws:
  46. # "Error creating DB Instance: InvalidParameterValue: MasterUsername
  47. # user cannot be used as it is a reserved word used by the engine"
  48. username = "rhsso"
  49. password = random_password.password.result
  50. port = "5432"
  51. vpc_security_group_ids = [aws_security_group.rhsso_rds_sg.id]
  52. backup_window = "00:00-03:00"
  53. maintenance_window = "Mon:03:00-Mon:06:00"
  54. # disable backups to create DB faster
  55. backup_retention_period = var.environment == "test" ? 0 : 35
  56. tags = merge(local.standard_tags, var.tags)
  57. enabled_cloudwatch_logs_exports = ["postgresql", "upgrade"]
  58. # DB subnet group
  59. subnet_ids = var.private_subnets
  60. create_db_subnet_group = true
  61. # DB parameter group
  62. family = "postgres12"
  63. # DB option group
  64. major_engine_version = "12"
  65. # Snapshot name upon DB deletion
  66. final_snapshot_identifier_prefix = "${var.identifier}-final-snapshot"
  67. # Database Deletion Protection
  68. deletion_protection = var.instance_termination_protection
  69. }