-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.tf
78 lines (69 loc) · 2.45 KB
/
db.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Generate a random string to be used as a suffix for the final snapshot identifier.
resource "random_string" "snapshot_suffix" {
length = 8
special = false
}
# Define an AWS RDS Cluster resource.
resource "aws_rds_cluster" "this" {
cluster_identifier = "${var.prefix}-${var.environment}"
engine = "aurora-mysql"
engine_mode = "serverless"
vpc_security_group_ids = [aws_security_group.db.id]
db_subnet_group_name = aws_db_subnet_group.this.name
engine_version = var.db_engine_version
availability_zones = [data.aws_availability_zones.this.names[0], data.aws_availability_zones.this.names[1], data.aws_availability_zones.this.names[2]]
database_name = "wordpress"
master_username = var.db_master_username
master_password = var.db_master_password
backup_retention_period = var.db_backup_retention_days
preferred_backup_window = var.db_backup_window
scaling_configuration {
auto_pause = var.db_auto_pause
seconds_until_auto_pause = var.db_seconds_until_auto_pause
max_capacity = var.db_max_capacity
min_capacity = var.db_min_capacity
}
final_snapshot_identifier = "${var.prefix}-${var.environment}-${random_string.snapshot_suffix.result}"
tags = var.tags
}
# Define an AWS DB Subnet Group resource.
resource "aws_db_subnet_group" "this" {
name = "${var.prefix}-${var.environment}"
subnet_ids = module.vpc.private_subnets
tags = var.tags
}
# Define an AWS security group for the RDS cluster.
resource "aws_security_group" "db" {
vpc_id = module.vpc.vpc_id
name = "${var.prefix}-db-${var.environment}"
ingress {
protocol = "tcp"
from_port = 3306
to_port = 3306
self = true
}
egress {
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
lifecycle {
create_before_destroy = true
}
tags = var.tags
}
# Store the DB master username in AWS SSM Parameter Store.
resource "aws_ssm_parameter" "db_master_user" {
name = "/${var.prefix}/${var.environment}/db_master_user"
type = "SecureString"
value = var.db_master_username
tags = var.tags
}
# Store the DB master password in AWS SSM Parameter Store.
resource "aws_ssm_parameter" "db_master_password" {
name = "/${var.prefix}/${var.environment}/db_master_password"
type = "SecureString"
value = var.db_master_password
tags = var.tags
}