Skip to content

Commit c4a7176

Browse files
authored
feat: initial version (#2)
* init * init * removed versions.tf * terraform block * replicate_source_db var type fix * vars * provider * db subnet group * db parameter group * db parameter group * db parameter group * simplified multi-replicas creation * role associations * outputs * outputs * fmt * custom replicas * custom replicas * restore_to_point_in_time * snapshot_identifier added to ignore_changes * timeouts * docs - replication * tags * examples * fmt
1 parent bd6b97f commit c4a7176

File tree

11 files changed

+783
-123
lines changed

11 files changed

+783
-123
lines changed

README.md

+150-12
Large diffs are not rendered by default.

examples/basic/main.tf

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module "example" {
2+
source = "github.com/opsd-io/terraform-module-aws-rds-postgres"
3+
4+
instance_name = "example"
5+
engine_version = "16.3"
6+
instance_class = "db.t4g.micro"
7+
8+
username = "dbadmin"
9+
password = "avoid-plaintext-passwords"
10+
11+
max_allocated_storage = 30
12+
13+
common_tags = {
14+
"Env" = "test"
15+
}
16+
17+
}

examples/basic/terraform.tf

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
provider "aws" {}

examples/example_of_use/.tool-versions

-1
This file was deleted.

examples/example_of_use/README.mkdn

-63
This file was deleted.

examples/example_of_use/main.tf

-4
This file was deleted.

examples/example_of_use/versions.tf

-13
This file was deleted.

main.tf

+184-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,184 @@
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+
}

outputs.tf

+54-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,54 @@
1-
# output "variable" {
2-
# description = "output variable description"
3-
# value = variable.main.name
4-
# }
1+
output "db_instance_address" {
2+
description = "The address of the RDS instance."
3+
value = aws_db_instance.main.address
4+
}
5+
6+
output "db_instance_arn" {
7+
description = "The ARN of the RDS instance."
8+
value = aws_db_instance.main.arn
9+
}
10+
11+
output "db_instance_availability_zone" {
12+
description = "The availability zone of the RDS instance."
13+
value = aws_db_instance.main.availability_zone
14+
}
15+
16+
output "db_instance_backup_retention_period" {
17+
description = "The backup window of the RDS instance."
18+
value = aws_db_instance.main.backup_retention_period
19+
}
20+
21+
output "db_instance_backup_window" {
22+
description = "The backup retention period of the RDS instance."
23+
value = aws_db_instance.main.backup_window
24+
}
25+
26+
output "db_instance_endpoint" {
27+
description = "The connection endpoint of the RDS instance."
28+
value = aws_db_instance.main.endpoint
29+
}
30+
31+
output "db_instance_identifier" {
32+
description = "The RDS instance identifier."
33+
value = aws_db_instance.main.identifier
34+
}
35+
36+
output "db_instance_engine_version_actual" {
37+
description = "The running version of the RDS instance."
38+
value = aws_db_instance.main.engine_version_actual
39+
}
40+
41+
output "db_instance_maintenance_window" {
42+
description = "The maintenance window of the RDS instance."
43+
value = aws_db_instance.main.maintenance_window
44+
}
45+
46+
output "db_instance_status" {
47+
description = "The status of the RDS instance."
48+
value = aws_db_instance.main.status
49+
}
50+
51+
output "db_instance_replica_availability_zone" {
52+
description = "The availability zone of the replica instance."
53+
value = length(aws_db_instance.replica) > 0 ? aws_db_instance.replica[0].availability_zone : null
54+
}

0 commit comments

Comments
 (0)