Skip to content

Commit 902acaf

Browse files
committed
feat(modules): rds sub-module
1 parent 50a8d1e commit 902acaf

File tree

5 files changed

+267
-0
lines changed

5 files changed

+267
-0
lines changed

modules/rds/.header.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# rds
2+
3+
This sub-module creates an RDS instance.

modules/rds/main.tf

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
resource "aws_db_instance" "this" {
2+
identifier = var.identifier
3+
4+
instance_class = var.instance_class
5+
storage_type = var.storage_type
6+
allocated_storage = var.allocated_storage
7+
8+
engine = var.engine
9+
engine_version = var.engine_version
10+
vpc_security_group_ids = var.vpc_security_group_ids
11+
12+
db_subnet_group_name = aws_db_subnet_group.this.name
13+
parameter_group_name = aws_db_parameter_group.this.name
14+
15+
db_name = var.db_name
16+
username = var.username
17+
manage_master_user_password = var.manage_master_user_password
18+
19+
skip_final_snapshot = var.skip_final_snapshot
20+
21+
tags = var.tags
22+
}
23+
24+
################################################################################
25+
# DB Subnet Group
26+
################################################################################
27+
28+
resource "aws_db_subnet_group" "this" {
29+
name = var.db_subnet_group_name
30+
description = var.db_subnet_group_description
31+
subnet_ids = var.db_subnet_group_subnet_ids
32+
33+
tags = var.db_subnet_group_tags
34+
}
35+
36+
################################################################################
37+
# DB Parameter Group
38+
################################################################################
39+
40+
resource "aws_db_parameter_group" "this" {
41+
name = var.db_parameter_group_name
42+
description = var.db_parameter_group_description
43+
family = var.db_parameter_group_family
44+
45+
dynamic "parameter" {
46+
for_each = var.db_parameter_group_parameters
47+
48+
content {
49+
name = parameter.value.name
50+
value = parameter.value.value
51+
apply_method = try(parameter.value.apply_method, null)
52+
}
53+
}
54+
55+
tags = var.db_parameter_group_tags
56+
}

modules/rds/outputs.tf

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
output "id" {
2+
description = "RDS DBI resource ID."
3+
value = aws_db_instance.this.id
4+
}
5+
6+
output "arn" {
7+
description = "The ARN of the RDS instance."
8+
value = aws_db_instance.this.arn
9+
}
10+
11+
output "endpoint" {
12+
description = "The connection endpoint in `address:port` format."
13+
value = aws_db_instance.this.endpoint
14+
}
15+
16+
output "master_user_secret" {
17+
description = "Details of the secret containing the database master password."
18+
value = aws_db_instance.this.master_user_secret[0]
19+
}
20+
21+
################################################################################
22+
# DB Subnet Group
23+
################################################################################
24+
25+
output "db_subnet_group_id" {
26+
description = "The db subnet group name."
27+
value = aws_db_subnet_group.this.id
28+
}
29+
30+
output "db_subnet_group_arn" {
31+
description = "The ARN of the db subnet group."
32+
value = aws_db_subnet_group.this.arn
33+
}
34+
35+
################################################################################
36+
# DB Parameter Group
37+
################################################################################
38+
39+
output "db_parameter_group_id" {
40+
description = "The db parameter group name."
41+
value = aws_db_parameter_group.this.id
42+
}
43+
44+
output "db_parameter_group_arn" {
45+
description = "The ARN of the db parameter group."
46+
value = aws_db_parameter_group.this.arn
47+
}

modules/rds/variables.tf

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
variable "identifier" {
2+
description = "(Required) The name of the RDS instance."
3+
type = string
4+
nullable = false
5+
}
6+
7+
variable "instance_class" {
8+
description = "(Required) The instance type of the RDS instance."
9+
type = string
10+
nullable = false
11+
}
12+
13+
variable "storage_type" {
14+
description = "(Optional, Default:\"gp2\") One of \"standard\", \"gp2\", \"gp3\" or \"io1\"."
15+
type = string
16+
nullable = false
17+
default = "gp2"
18+
}
19+
20+
variable "allocated_storage" {
21+
description = "(Required) The allocated storage in gibibytes."
22+
type = number
23+
nullable = false
24+
}
25+
26+
variable "engine" {
27+
description = "(Optional, Default:null) The database engine."
28+
type = string
29+
default = null
30+
}
31+
32+
variable "engine_version" {
33+
description = "(Optional, Default:null) The engine version to use."
34+
type = string
35+
default = null
36+
}
37+
38+
variable "db_name" {
39+
description = "(Optional, Default:default_db) The database name."
40+
type = string
41+
nullable = false
42+
default = "default_db"
43+
}
44+
45+
variable "username" {
46+
description = "(Optional, Default:admin_user) Username for the master DB user."
47+
type = string
48+
nullable = false
49+
default = "admin_user"
50+
}
51+
52+
variable "manage_master_user_password" {
53+
description = "(Optional, Default:true) Set to true to allow RDS to manage the master user password in Secrets Manager."
54+
type = bool
55+
nullable = false
56+
default = true
57+
}
58+
59+
variable "vpc_security_group_ids" {
60+
description = "(Optional) List of VPC security groups to associate."
61+
type = list(string)
62+
nullable = false
63+
default = []
64+
}
65+
66+
variable "skip_final_snapshot" {
67+
description = "(Optional, Default:false) Determines whether a final DB snapshot is created before the DB instance is deleted."
68+
type = bool
69+
nullable = false
70+
default = false
71+
}
72+
73+
variable "tags" {
74+
description = "(Optional) A map of tags to assign to the resource."
75+
type = map(string)
76+
nullable = false
77+
default = {}
78+
}
79+
80+
################################################################################
81+
# DB Subnet Group
82+
################################################################################
83+
84+
variable "db_subnet_group_name" {
85+
description = "(Required, Forces new resource) The name of the DB subnet group."
86+
type = string
87+
nullable = false
88+
}
89+
90+
variable "db_subnet_group_description" {
91+
description = "(Optional, Default:\"Managed By Terraform\", Forces new resource) The description of the DB subnet group."
92+
type = string
93+
nullable = false
94+
default = "Managed By Terraform"
95+
}
96+
97+
variable "db_subnet_group_subnet_ids" {
98+
description = "(Required) A list of VPC subnet IDs."
99+
type = list(string)
100+
nullable = false
101+
}
102+
103+
variable "db_subnet_group_tags" {
104+
description = "(Optional, Default:{}) A map of tags to assign to the resource."
105+
type = map(string)
106+
nullable = false
107+
default = {}
108+
}
109+
110+
################################################################################
111+
# DB Parameter Group
112+
################################################################################
113+
114+
variable "db_parameter_group_name" {
115+
description = "(Required, Forces new resource) The name of the DB parameter group."
116+
type = string
117+
nullable = false
118+
}
119+
120+
variable "db_parameter_group_description" {
121+
description = "(Optional, Default:\"Managed By Terraform\", Forces new resource) The description of the DB parameter group."
122+
type = string
123+
nullable = false
124+
default = "Managed By Terraform"
125+
}
126+
127+
variable "db_parameter_group_family" {
128+
description = "(Required, Forces new resource) The family of the DB parameter group."
129+
type = string
130+
nullable = false
131+
}
132+
133+
variable "db_parameter_group_parameters" {
134+
description = "(Optional, Default:[]) The DB parameters to apply."
135+
type = list(
136+
object({
137+
name = string
138+
value = string
139+
apply_method = optional(string)
140+
})
141+
)
142+
nullable = false
143+
default = []
144+
}
145+
146+
variable "db_parameter_group_tags" {
147+
description = "(Optional, Default:{}) A map of tags to assign to the resource."
148+
type = map(string)
149+
nullable = false
150+
default = {}
151+
}

modules/rds/version.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = ">= 1.8.4"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = "~> 5.0"
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)