Skip to content

Commit 6e4d991

Browse files
authored
Merge pull request #4 from Aswin-Vijayan/master
[TEC-56][Add] - Added variables
2 parents ff7f772 + edf8e10 commit 6e4d991

File tree

10 files changed

+176
-1197
lines changed

10 files changed

+176
-1197
lines changed

README.md

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,17 @@ terraform destroy \
1515
-backend-config="region=us-west-2" \
1616
-backend-config="dynamodb_table=terraform-state-lock"
1717

18+
## RDS DB
1819

19-
## Requirements
20-
21-
1. Create VPC (10.0.0.0/16)
22-
2. Create Subnets
23-
- Public Subnets (For LB)
24-
- 10.0.1.0/24
25-
- 10.0.2.0/24
26-
- 10.0.3.0/24
27-
- App Subnetes (Private Subnet)
28-
- 10.0.4.0/24
29-
- 10.0.5.0/24
30-
- 10.0.6.0/24
31-
- DB Subnetes (Priavet Subnet)
32-
- 10.0.7.0/24
33-
- 10.0.8.0/24
34-
- 10.0.9.0/24
35-
3. Public subnets can talk to app subnets (only on specific ports using NAC) not DB subnets.
36-
4. App Subnets can talk to DB subnets (On specify ports using NACL)
20+
cd into environments/dev/rds directory and run the following commands:
3721

22+
terraform init
23+
24+
terraform plan -var-file=../../../vars/dev/rds.tfvars
25+
26+
terraform apply -var-file=../../../vars/dev/rds.tfvars
27+
28+
terraform destroy -var-file=../../../vars/dev/rds.tfvars
3829

3930
## Command Reference
4031

environments/dev/rds/main.tf

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,26 @@ provider "aws" {
33
}
44

55
module "rds" {
6-
source = "../../../modules/rds"
7-
region = var.region
8-
owner = var.owner
9-
cost_center = var.cost_center
10-
environment = var.environment
11-
db_username = var.db_username
6+
source = "../../../modules/rds"
7+
region = var.region
8+
db_username = var.db_username
129
set_secret_manager_password = var.set_secret_manager_password
13-
set_db_password = var.set_db_password
14-
db_password = var.db_password
15-
db_name = var.db_name
16-
db_instance_class = var.db_instance_class
17-
parameter_name = var.parameter_name
10+
set_db_password = var.set_db_password
11+
db_password = var.db_password
12+
db_name = var.db_name
13+
db_instance_class = var.db_instance_class
14+
db_storage_size = var.db_storage_size
15+
sg_name = var.sg_name
16+
cidr_block = var.cidr_block
17+
backup_retention_period = var.backup_retention_period
18+
multi_az = var.multi_az
19+
delete_automated_backups = var.delete_automated_backups
20+
copy_tags_to_snapshot = var.copy_tags_to_snapshot
21+
publicly_accessible = var.publicly_accessible
22+
skip_final_snapshot = var.skip_final_snapshot
23+
apply_immediately = var.apply_immediately
24+
owner = var.owner
25+
cost_center = var.cost_center
26+
environment = var.environment
27+
application = var.application
1828
}

environments/dev/rds/variables.tf

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ variable "owner" {
2323
description = "Name of the owner for this RDS"
2424
}
2525

26+
variable "application" {
27+
type = string
28+
description = "Name of the application"
29+
}
30+
31+
variable "sg_name" {
32+
type = string
33+
description = "RDS security group name"
34+
}
35+
36+
variable "cidr_block" {
37+
type = list(string)
38+
description = "CIDR block for RDS security group"
39+
}
40+
2641
variable "cost_center" {
2742
type = string
2843
description = "Name of cost-center for this RDS"
@@ -54,12 +69,47 @@ variable "db_instance_class" {
5469
type = string
5570
}
5671

57-
variable "parameter_name" {
58-
description = "The RDS instance class"
59-
type = string
60-
}
61-
6272
variable "set_db_password" {
6373
description = "Condition to check for custom password"
6474
type = string
65-
}
75+
}
76+
77+
variable "db_storage_size" {
78+
description = "The allocated storage size for the RDS instance."
79+
type = number
80+
}
81+
82+
variable "backup_retention_period" {
83+
description = "The number of days to retain automated backups."
84+
type = number
85+
}
86+
87+
variable "multi_az" {
88+
description = "Enable multi-AZ deployment for the RDS instance."
89+
type = bool
90+
}
91+
92+
variable "delete_automated_backups" {
93+
description = "Enable deletion of automated backups when the RDS instance is deleted."
94+
type = bool
95+
}
96+
97+
variable "copy_tags_to_snapshot" {
98+
description = "Copy tags to DB snapshots created from the RDS instance."
99+
type = bool
100+
}
101+
102+
variable "publicly_accessible" {
103+
description = "Allow the RDS instance to be publicly accessible."
104+
type = bool
105+
}
106+
107+
variable "skip_final_snapshot" {
108+
description = "Skip the creation of a final DB snapshot when the RDS instance is deleted."
109+
type = bool
110+
}
111+
112+
variable "apply_immediately" {
113+
description = "Apply changes immediately to the RDS instance."
114+
type = bool
115+
}

modules/rds/main.tf

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
# Create a DB security group
22
resource "aws_security_group" "rds_security_group" {
3-
name = "rds-security-group"
3+
name = var.sg_name
44
description = "Security group for RDS instance"
55

66
ingress {
77
from_port = 3306
88
to_port = 3306
99
protocol = "tcp"
10-
cidr_blocks = ["0.0.0.0/0"]
10+
cidr_blocks = var.cidr_block
1111
}
1212

1313
egress {
1414
from_port = 0
1515
to_port = 0
1616
protocol = "-1"
17-
cidr_blocks = ["0.0.0.0/0"]
17+
cidr_blocks = var.cidr_block
1818
}
1919

2020
tags = merge(
@@ -23,7 +23,7 @@ resource "aws_security_group" "rds_security_group" {
2323
Environment = var.environment,
2424
Owner = var.owner,
2525
CostCenter = var.cost_center,
26-
Application = "petclinic-rds-sg"
26+
Application = var.application,
2727
},
2828
var.tags
2929
)
@@ -33,28 +33,29 @@ resource "aws_db_instance" "rds_instance" {
3333
identifier = var.db_name
3434
engine = "mysql"
3535
instance_class = var.db_instance_class
36-
allocated_storage = 10
36+
allocated_storage = var.db_storage_size
3737
storage_type = "gp2"
3838
# manage_master_user_password = var.set_secret_manager_password ? true : false
3939
manage_master_user_password = var.set_secret_manager_password ? true : null
4040
username = var.db_username
4141
password = var.set_db_password ? var.db_password : null
4242
db_subnet_group_name = "default"
4343
vpc_security_group_ids = [aws_security_group.rds_security_group.id]
44-
backup_retention_period = 7
45-
delete_automated_backups = true
46-
copy_tags_to_snapshot = true
47-
publicly_accessible = true
48-
skip_final_snapshot = true
49-
apply_immediately = true
44+
backup_retention_period = var.backup_retention_period
45+
multi_az = var.multi_az
46+
delete_automated_backups = var.delete_automated_backups
47+
copy_tags_to_snapshot = var.copy_tags_to_snapshot
48+
publicly_accessible = var.publicly_accessible
49+
skip_final_snapshot = var.skip_final_snapshot
50+
apply_immediately = var.apply_immediately
5051

5152
tags = merge(
5253
{
5354
Name = "petclinic-rds"
5455
Environment = var.environment,
5556
Owner = var.owner,
5657
CostCenter = var.cost_center,
57-
Application = "pet-clinic"
58+
Application = var.application,
5859
},
5960
var.tags
6061
)
@@ -63,17 +64,4 @@ resource "aws_db_instance" "rds_instance" {
6364
# Data source to retrieve RDS endpoint
6465
data "aws_db_instance" "rds_instance" {
6566
db_instance_identifier = aws_db_instance.rds_instance.id
66-
}
67-
68-
69-
# resource "aws_ssm_parameter" "rds_endpoint" {
70-
# name = var.parameter_name
71-
# type = "String"
72-
# value = data.aws_db_instance.rds_instance.endpoint
73-
# }
74-
75-
# resource "local_file" "password_file" {
76-
# count = var.manage_master_user_password ? 0 : 1
77-
# filename = "password.txt"
78-
# content = var.db_password
79-
# }
67+
}

modules/rds/outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "rds_instance_endpoint" {
2+
value = data.aws_db_instance.rds_instance.endpoint
3+
description = "RDS endpoint"
4+
}

modules/rds/variables.tf

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,26 @@ variable "owner" {
2323
description = "Name of the owner for this RDS"
2424
}
2525

26+
variable "application" {
27+
type = string
28+
description = "Name of the application"
29+
}
30+
2631
variable "cost_center" {
2732
type = string
2833
description = "Name of cost-center for this RDS"
2934
}
3035

36+
variable "sg_name" {
37+
type = string
38+
description = "RDS security group name"
39+
}
40+
41+
variable "cidr_block" {
42+
type = list(string)
43+
description = "CIDR block for RDS security group"
44+
}
45+
3146
variable "db_username" {
3247
description = "The username for the RDS database"
3348
type = string
@@ -54,12 +69,47 @@ variable "db_instance_class" {
5469
type = string
5570
}
5671

57-
variable "parameter_name" {
58-
description = "The RDS instance class"
59-
type = string
60-
}
61-
6272
variable "set_db_password" {
6373
description = "Condition to check for custom password"
6474
type = string
65-
}
75+
}
76+
77+
variable "db_storage_size" {
78+
description = "The allocated storage size for the RDS instance."
79+
type = number
80+
}
81+
82+
variable "backup_retention_period" {
83+
description = "The number of days to retain automated backups."
84+
type = number
85+
}
86+
87+
variable "multi_az" {
88+
description = "Enable multi-AZ deployment for the RDS instance."
89+
type = bool
90+
}
91+
92+
variable "delete_automated_backups" {
93+
description = "Enable deletion of automated backups when the RDS instance is deleted."
94+
type = bool
95+
}
96+
97+
variable "copy_tags_to_snapshot" {
98+
description = "Copy tags to DB snapshots created from the RDS instance."
99+
type = bool
100+
}
101+
102+
variable "publicly_accessible" {
103+
description = "Allow the RDS instance to be publicly accessible."
104+
type = bool
105+
}
106+
107+
variable "skip_final_snapshot" {
108+
description = "Skip the creation of a final DB snapshot when the RDS instance is deleted."
109+
type = bool
110+
}
111+
112+
variable "apply_immediately" {
113+
description = "Apply changes immediately to the RDS instance."
114+
type = bool
115+
}

0 commit comments

Comments
 (0)