rds.tf 2.5 KB

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