rds.tf 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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-2017"
  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 "keycloak_db" {
  30. source = "terraform-aws-modules/rds/aws"
  31. version = "~> v2.0"
  32. identifier = var.identifier # this is the RDS identifier, not the DB name
  33. name = "keycloak" # the DB name
  34. engine = "postgres"
  35. engine_version = "12.5"
  36. instance_class = var.db_instance_type
  37. allocated_storage = var.rds_storage
  38. storage_encrypted = true
  39. kms_key_id = module.keycloak_key.key_arn
  40. ca_cert_identifier = local.ca_cert_identifier
  41. # NOTE: Do NOT use 'user' as the value for 'username' as it throws:
  42. # "Error creating DB Instance: InvalidParameterValue: MasterUsername
  43. # user cannot be used as it is a reserved word used by the engine"
  44. username = "keycloak"
  45. password = random_password.password.result
  46. port = "5432"
  47. create_random_password = true
  48. random_password_length = 32
  49. vpc_security_group_ids = [ aws_security_group.keycloak_rds_sg.id ]
  50. backup_window = "00:00-03:00"
  51. maintenance_window = "Mon:03:00-Mon:06:00"
  52. # disable backups to create DB faster
  53. backup_retention_period = var.environment == "test" ? 0 : 35
  54. tags = merge(var.standard_tags, var.tags)
  55. enabled_cloudwatch_logs_exports = ["postgresql", "upgrade"]
  56. # DB subnet group
  57. subnet_ids = var.private_subnets
  58. # DB parameter group
  59. family = "postgres12"
  60. # DB option group
  61. major_engine_version = "12"
  62. # Snapshot name upon DB deletion
  63. final_snapshot_identifier = "${var.identifier}-final-snapshot"
  64. # Database Deletion Protection
  65. deletion_protection = var.instance_termination_protection
  66. }