This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
146 lines (129 loc) · 4.88 KB
/
main.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 4.61.0"
}
}
required_version = ">= 1.2"
}
locals {
engine_version_short = split(".", var.engine_version)[0]
}
module "aurora" {
source = "terraform-aws-modules/rds-aurora/aws"
version = "6.1.4"
name = var.name
engine = "aurora-postgresql"
engine_version = var.engine_version
auto_minor_version_upgrade = var.auto_minor_version_upgrade
ca_cert_identifier = var.ca_cert_identifier
instances = { for index in range(var.instance_count) : index + 1 =>
merge(
{},
var.instance_identifier_pseudoprefix != "" ? { identifier = "${var.instance_identifier_pseudoprefix}${index + 1}" } : {}
) }
publicly_accessible = false
instance_class = var.instance_class
deletion_protection = var.deletion_protection
endpoints = {
static = {
identifier = "static-custom-endpt-${var.name}"
type = "ANY"
}
}
vpc_id = var.vpc_id
subnets = var.subnets
create_db_subnet_group = var.create_db_subnet_group
db_subnet_group_name = var.subnet_group_name
create_security_group = true
allowed_security_groups = var.allowed_security_groups
allowed_cidr_blocks = var.security_group_allowed_cidrs
security_group_egress_rules = {
to_cidrs = {
cidr_blocks = ["0.0.0.0/0"]
description = "Egress to Internet"
}
}
iam_database_authentication_enabled = true
master_username = var.master_username
create_random_password = var.create_random_password
database_name = var.database_name
backup_retention_period = var.backup_retention_period
apply_immediately = true
skip_final_snapshot = var.skip_final_snapshot
snapshot_identifier = var.snapshot_identifier
db_parameter_group_name = aws_db_parameter_group.db_parameter_group.id
db_cluster_parameter_group_name = aws_rds_cluster_parameter_group.db_cluster_parameter_group.id
enabled_cloudwatch_logs_exports = ["postgresql"]
tags = var.tags
copy_tags_to_snapshot = true
}
resource "aws_db_parameter_group" "db_parameter_group" {
name = "${var.name}-aurora-db-postgres${local.engine_version_short}-parameter-group"
family = "aurora-postgresql${local.engine_version_short}"
description = "${var.name}-aurora-db-postgres${local.engine_version_short}-parameter-group"
dynamic "parameter" {
for_each = var.db_parameter_group_parameters
content {
name = parameter.value["name"]
value = parameter.value["value"]
}
}
tags = var.tags
}
resource "aws_rds_cluster_parameter_group" "db_cluster_parameter_group" {
name = "${var.name}-aurora-postgres${local.engine_version_short}-cluster-parameter-group"
family = "aurora-postgresql${local.engine_version_short}"
description = "${var.name}-aurora-postgres${local.engine_version_short}-cluster-parameter-group"
dynamic "parameter" {
for_each = var.db_cluster_parameter_group_parameters
content {
name = parameter.value["name"]
value = parameter.value["value"]
}
}
tags = var.tags
}
data "aws_route53_zone" "cms_zone" {
count = var.route53_zone_base_domain != "" ? 1 : 0
name = var.route53_zone_base_domain
private_zone = true
}
resource "aws_route53_record" "www" {
zone_id = coalesce(var.route53_zone_id, try(data.aws_route53_zone.cms_zone[0].zone_id, ""))
name = var.route53_record_name
type = "CNAME"
ttl = "60"
records = [module.aurora.cluster_endpoint]
}
# postgres egress rule for cluster_security_group
resource "aws_security_group_rule" "db-egress-cluster_security_group" {
type = "egress"
description = "postgres traffic"
from_port = 0
to_port = 0
protocol = "-1"
source_security_group_id = module.aurora.security_group_id
security_group_id = var.cluster_security_group_id
}
# postgres egress rule for worker_security_group
resource "aws_security_group_rule" "db-egress-worker_security_group" {
type = "egress"
description = "postgres traffic"
from_port = 0
to_port = 0
protocol = "-1"
source_security_group_id = module.aurora.security_group_id
security_group_id = var.worker_security_group_id
}
# postgres egress rule for cluster_primary_security_group
resource "aws_security_group_rule" "db-egress-cluster_primary_security_group" {
type = "egress"
description = "postgres traffic"
from_port = 0
to_port = 0
protocol = "-1"
source_security_group_id = module.aurora.security_group_id
security_group_id = var.cluster_primary_security_group_id
}