|
1 |
| -# Terraform code goes here |
| 1 | +terraform { |
| 2 | + required_version = ">= 1.5.5" |
| 3 | + |
| 4 | + required_providers { |
| 5 | + aws = { |
| 6 | + source = "hashicorp/aws" |
| 7 | + version = ">= 5.50.0" |
| 8 | + } |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +locals { |
| 13 | + db_subnet_group_name = var.db_subnet_group_name != null ? var.db_subnet_group_name : var.instance_name |
| 14 | + parameter_group_name = var.parameter_group_name != null ? var.parameter_group_name : (length(var.parameter_group_list) > 0 ? var.instance_name : null) |
| 15 | +} |
| 16 | + |
| 17 | +resource "aws_db_parameter_group" "main" { |
| 18 | + count = length(var.parameter_group_list) > 0 ? 1 : 0 |
| 19 | + |
| 20 | + name = local.parameter_group_name |
| 21 | + family = var.parameter_group_family |
| 22 | + tags = var.common_tags |
| 23 | + |
| 24 | + dynamic "parameter" { |
| 25 | + for_each = var.parameter_group_list |
| 26 | + content { |
| 27 | + name = parameter.value.name |
| 28 | + value = parameter.value.value |
| 29 | + apply_method = lookup(parameter.value, "apply_method", null) |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + lifecycle { |
| 34 | + create_before_destroy = true |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +resource "aws_db_subnet_group" "main" { |
| 39 | + count = var.subnet_ids != null && try(length(var.subnet_ids) > 0) ? 1 : 0 |
| 40 | + |
| 41 | + name = local.db_subnet_group_name |
| 42 | + subnet_ids = var.subnet_ids |
| 43 | + tags = var.common_tags |
| 44 | +} |
| 45 | + |
| 46 | +resource "aws_db_instance" "main" { |
| 47 | + allocated_storage = var.allocated_storage |
| 48 | + auto_minor_version_upgrade = var.auto_minor_version_upgrade |
| 49 | + availability_zone = var.availability_zone |
| 50 | + backup_retention_period = var.backup_retention_period |
| 51 | + backup_window = var.backup_window |
| 52 | + |
| 53 | + blue_green_update { |
| 54 | + enabled = var.blue_green_update_enabled |
| 55 | + } |
| 56 | + |
| 57 | + ca_cert_identifier = var.ca_cert_identifier |
| 58 | + copy_tags_to_snapshot = var.copy_tags_to_snapshot |
| 59 | + custom_iam_instance_profile = var.custom_iam_instance_profile |
| 60 | + db_name = var.db_name |
| 61 | + db_subnet_group_name = local.db_subnet_group_name |
| 62 | + dedicated_log_volume = var.dedicated_log_volume |
| 63 | + delete_automated_backups = var.delete_automated_backups |
| 64 | + deletion_protection = var.deletion_protection |
| 65 | + enabled_cloudwatch_logs_exports = var.enabled_cloudwatch_logs_exports |
| 66 | + engine = "postgres" |
| 67 | + engine_version = var.engine_version |
| 68 | + final_snapshot_identifier = var.final_snapshot_identifier |
| 69 | + iam_database_authentication_enabled = var.iam_database_authentication_enabled |
| 70 | + instance_class = var.instance_class |
| 71 | + identifier = var.instance_name |
| 72 | + iops = var.iops |
| 73 | + kms_key_id = var.kms_key_id |
| 74 | + maintenance_window = var.maintenance_window |
| 75 | + manage_master_user_password = var.manage_master_user_password |
| 76 | + max_allocated_storage = var.max_allocated_storage |
| 77 | + monitoring_interval = var.monitoring_interval |
| 78 | + monitoring_role_arn = var.monitoring_role_arn |
| 79 | + multi_az = var.multi_az |
| 80 | + network_type = var.network_type |
| 81 | + parameter_group_name = local.parameter_group_name |
| 82 | + password = var.password |
| 83 | + performance_insights_enabled = var.performance_insights_enabled |
| 84 | + performance_insights_kms_key_id = var.performance_insights_kms_key_id |
| 85 | + performance_insights_retention_period = var.performance_insights_retention_period |
| 86 | + port = var.port |
| 87 | + publicly_accessible = var.publicly_accessible |
| 88 | + skip_final_snapshot = var.skip_final_snapshot |
| 89 | + snapshot_identifier = var.snapshot_identifier |
| 90 | + storage_encrypted = var.storage_encrypted |
| 91 | + storage_throughput = var.storage_throughput |
| 92 | + storage_type = var.storage_type |
| 93 | + tags = merge( |
| 94 | + var.common_tags, |
| 95 | + var.instance_tags, |
| 96 | + { Name = var.instance_name }, |
| 97 | + var.tags |
| 98 | + ) |
| 99 | + username = var.username |
| 100 | + vpc_security_group_ids = var.vpc_security_group_ids |
| 101 | + |
| 102 | + dynamic "restore_to_point_in_time" { |
| 103 | + for_each = var.snapshot_identifier == null && length(var.restore_to_point_in_time) > 0 ? [1] : [] |
| 104 | + |
| 105 | + content { |
| 106 | + restore_time = lookup(var.restore_to_point_in_time, "restore_time", null) |
| 107 | + source_db_instance_identifier = lookup(var.restore_to_point_in_time, "source_db_instance_identifier", null) |
| 108 | + source_db_instance_automated_backups_arn = lookup(var.restore_to_point_in_time, "source_db_instance_automated_backups_arn", null) |
| 109 | + source_dbi_resource_id = lookup(var.restore_to_point_in_time, "source_dbi_resource_id", null) |
| 110 | + use_latest_restorable_time = lookup(var.restore_to_point_in_time, "use_latest_restorable_time", null) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + lifecycle { |
| 115 | + ignore_changes = [ |
| 116 | + snapshot_identifier |
| 117 | + ] |
| 118 | + } |
| 119 | + |
| 120 | + timeouts { |
| 121 | + create = var.timeouts.create |
| 122 | + update = var.timeouts.update |
| 123 | + delete = var.timeouts.delete |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +resource "aws_db_instance_role_association" "main" { |
| 128 | + for_each = var.role_associations |
| 129 | + |
| 130 | + db_instance_identifier = aws_db_instance.main.identifier |
| 131 | + feature_name = each.key |
| 132 | + role_arn = each.value |
| 133 | +} |
| 134 | + |
| 135 | +resource "aws_db_instance" "replica" { |
| 136 | + count = var.replica_enabled ? 1 : 0 |
| 137 | + |
| 138 | + replicate_source_db = aws_db_instance.main.identifier |
| 139 | + instance_class = var.instance_class |
| 140 | + availability_zone = var.replica_availability_zone |
| 141 | + identifier = var.replica_name != null ? var.replica_name : "${var.instance_name}-replica" |
| 142 | + auto_minor_version_upgrade = var.auto_minor_version_upgrade |
| 143 | + skip_final_snapshot = var.skip_final_snapshot |
| 144 | + tags = merge( |
| 145 | + var.common_tags, |
| 146 | + var.instance_tags, |
| 147 | + { Name = var.replica_name != null ? var.replica_name : "${var.instance_name}-replica" }, |
| 148 | + var.replica_tags |
| 149 | + ) |
| 150 | +} |
| 151 | + |
| 152 | +resource "aws_db_instance" "multi_replica" { |
| 153 | + count = var.number_of_replicas |
| 154 | + |
| 155 | + replicate_source_db = aws_db_instance.main.identifier |
| 156 | + instance_class = var.instance_class |
| 157 | + identifier = var.replica_name != null ? "${var.replica_name}-${count.index + 1}" : "${var.instance_name}-replica-${count.index + 1}" |
| 158 | + auto_minor_version_upgrade = var.auto_minor_version_upgrade |
| 159 | + skip_final_snapshot = var.skip_final_snapshot |
| 160 | + tags = merge( |
| 161 | + var.common_tags, |
| 162 | + var.instance_tags, |
| 163 | + { Name = var.replica_name != null ? "${var.replica_name}-${count.index + 1}" : "${var.instance_name}-replica-${count.index + 1}" }, |
| 164 | + var.replica_tags |
| 165 | + ) |
| 166 | +} |
| 167 | + |
| 168 | +resource "aws_db_instance" "custom_replica" { |
| 169 | + for_each = var.custom_replicas |
| 170 | + |
| 171 | + replicate_source_db = aws_db_instance.main.identifier |
| 172 | + instance_class = try(each.value.instance_class) |
| 173 | + availability_zone = try(each.value.availability_zone) |
| 174 | + identifier = each.key |
| 175 | + auto_minor_version_upgrade = var.auto_minor_version_upgrade |
| 176 | + skip_final_snapshot = var.skip_final_snapshot |
| 177 | + tags = merge( |
| 178 | + var.common_tags, |
| 179 | + var.instance_tags, |
| 180 | + { Name = each.key }, |
| 181 | + var.replica_tags, |
| 182 | + try(each.value.tags, {}) |
| 183 | + ) |
| 184 | +} |
0 commit comments