-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
316 lines (252 loc) · 9.27 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
data "aws_region" "current" {}
data "aws_partition" "current" {}
data "aws_caller_identity" "current" {}
######################
# Neptune cluster
######################
resource "aws_neptune_cluster" "this" {
count = var.create_neptune_cluster ? 1 : 0
# Core configuration
cluster_identifier = try(var.cluster_identifier, null)
cluster_identifier_prefix = try(var.cluster_identifier_prefix, null)
engine = "neptune"
engine_version = var.engine_version
port = try(var.port, 8182)
storage_encrypted = try(var.storage_encrypted, null)
storage_type = try(var.storage_type, "standard")
deletion_protection = try(var.deletion_protection, null)
apply_immediately = try(var.apply_immediately, null)
allow_major_version_upgrade = try(var.allow_major_version_upgrade, null)
backup_retention_period = try(var.backup_retention_period, null)
# Optional references
neptune_cluster_parameter_group_name = try(aws_neptune_cluster_parameter_group.this[0].name, null)
neptune_subnet_group_name = try(aws_neptune_subnet_group.this[0].name, null)
kms_key_arn = try(var.kms_key_arn, null)
iam_database_authentication_enabled = try(var.iam_database_authentication_enabled, null)
iam_roles = try([aws_iam_role.this[0].arn], var.iam_roles)
availability_zones = try(var.availability_zones, null)
copy_tags_to_snapshot = try(var.copy_tags_to_snapshot, null)
final_snapshot_identifier = try(var.final_snapshot_identifier, null)
global_cluster_identifier = try(var.global_cluster_identifier, null)
replication_source_identifier = try(var.replication_source_identifier, null)
snapshot_identifier = try(var.snapshot_identifier, null)
preferred_backup_window = try(var.preferred_backup_window, null)
preferred_maintenance_window = try(var.preferred_maintenance_window, null)
# CloudWatch logs
enable_cloudwatch_logs_exports = try(var.enable_cloudwatch_logs_exports, null)
# Serverless configuration
dynamic "serverless_v2_scaling_configuration" {
for_each = var.enable_serverless ? [1] : []
content {
min_capacity = var.min_capacity
max_capacity = var.max_capacity
}
}
# Skipping final snapshot if needed
skip_final_snapshot = try(var.skip_final_snapshot, null)
# Security groups
vpc_security_group_ids = try([aws_security_group.this[0].id], var.vpc_security_group_ids)
tags = try(var.tags, null)
}
######################
# Neptune Global Cluster
######################
resource "aws_neptune_global_cluster" "this" {
count = var.create_neptune_global_cluster ? 1 : 0
global_cluster_identifier = var.global_cluster_identifier
engine = try(var.global_cluster_engine, null)
engine_version = try(var.global_cluster_engine_version, null)
deletion_protection = try(var.global_cluster_deletion_protection, null)
source_db_cluster_identifier = try(var.global_cluster_source_db_cluster_identifier, null)
storage_encrypted = try(var.global_cluster_storage_encrypted, null)
}
######################
# Primary Cluster instance
######################
resource "aws_neptune_cluster_instance" "primary" {
count = var.create_neptune_instance ? 1 : 0
cluster_identifier = aws_neptune_cluster.this[0].cluster_identifier
instance_class = var.instance_class
neptune_parameter_group_name = try(aws_neptune_parameter_group.this[0].name, null)
neptune_subnet_group_name = try(aws_neptune_subnet_group.this[0].name, null)
tags = merge(
try(var.tags, {}),
try(var.neptune_cluster_instance_tags, {})
)
}
######################
# Read Replica Instances
######################
resource "aws_neptune_cluster_instance" "read_replicas" {
count = var.read_replica_count
cluster_identifier = aws_neptune_cluster.this[0].cluster_identifier
instance_class = var.instance_class
neptune_parameter_group_name = try(aws_neptune_parameter_group.this[0].name, null)
neptune_subnet_group_name = try(aws_neptune_subnet_group.this[0].name, null)
tags = merge(
try(var.tags, {}),
try(var.neptune_cluster_instance_tags, {})
)
}
######################
# Cluster snapshot
######################
resource "aws_neptune_cluster_snapshot" "this" {
count = var.create_neptune_cluster_snapshot ? 1 : 0
db_cluster_identifier = try(aws_neptune_cluster.this[0].id, var.db_cluster_identifier)
db_cluster_snapshot_identifier = coalesce(
var.db_cluster_snapshot_identifier,
format("%s-%s", aws_neptune_cluster.this[0].id, random_id.snapshot_suffix.hex)
)
dynamic "timeouts" {
for_each = var.db_cluster_identifier != null ? [1] : []
content {
create = var.create_timeout
}
}
}
######################
# Parameter groups
######################
resource "aws_neptune_cluster_parameter_group" "this" {
count = var.create_neptune_cluster_parameter_group ? 1 : 0
name = "cluster-parameter-group-${var.cluster_identifier}"
description = "Neptune Cluster Parameter Group"
family = var.neptune_family
dynamic "parameter" {
for_each = var.neptune_cluster_parameters
content {
name = parameter.value.key
value = parameter.value.value
}
}
tags = merge(
try(var.tags, {}),
try(var.neptune_cluster_parameter_group_tags, {})
)
}
resource "aws_neptune_parameter_group" "this" {
count = var.create_neptune_parameter_group ? 1 : 0
name = "parameter-group-${var.cluster_identifier}"
description = "Neptune DB Parameter Group"
family = var.neptune_family
dynamic "parameter" {
for_each = var.neptune_db_parameters
content {
name = parameter.value.key
value = parameter.value.value
}
}
tags = merge(
try(var.tags, {}),
try(var.neptune_parameter_group_tags, {})
)
}
######################
# Subnet groups
######################
resource "aws_neptune_subnet_group" "this" {
count = var.create_neptune_subnet_group ? 1 : 0
name = "subnet-group-${var.cluster_identifier}"
description = "Neptune Subnet Group"
subnet_ids = var.subnet_ids
tags = merge(
try(var.tags, {}),
try(var.neptune_subnet_group_tags, {})
)
}
######################
# Event subscriptions
######################
resource "aws_neptune_event_subscription" "this" {
for_each = var.event_subscriptions != null ? var.event_subscriptions : {}
name = each.key
sns_topic_arn = each.value
source_type = var.event_subscriptions != null ? "db-instance" : null
source_ids = var.create_neptune_instance ? [aws_neptune_cluster_instance.primary[0].id] : []
tags = merge(
try(var.tags, {}),
try(var.neptune_event_subscription_tags, {})
)
}
######################
# Endpoints
######################
resource "aws_neptune_cluster_endpoint" "this" {
for_each = var.create_neptune_cluster_endpoint ? { for idx, endpoint in var.neptune_cluster_endpoints : idx => endpoint } : {}
cluster_identifier = aws_neptune_cluster.this[0].cluster_identifier
cluster_endpoint_identifier = each.key
endpoint_type = each.value.endpoint_type
static_members = each.value.static_members
excluded_members = each.value.excluded_members
tags = each.value.tags
}
######################
# Security group
######################
resource "aws_security_group" "this" {
count = var.create_neptune_security_group ? 1 : 0
name = "neptune-sg-${var.cluster_identifier}"
description = "Neptune security group"
vpc_id = var.vpc_id
ingress {
description = "Inbound Neptune Traffic"
from_port = var.neptune_port
to_port = var.neptune_port
protocol = "tcp"
cidr_blocks = var.neptune_subnet_cidrs
}
egress {
description = "Outbound Neptune Traffic"
from_port = var.neptune_port
to_port = var.neptune_port
protocol = "tcp"
cidr_blocks = var.neptune_subnet_cidrs
}
tags = merge(
try(var.tags, {}),
try(var.neptune_security_group_tags, {})
)
}
######################
# IAM role
######################
data "aws_iam_policy_document" "this" {
count = var.create_neptune_iam_role ? 1 : 0
statement {
actions = [
"sts:AssumeRole",
]
principals {
type = "Service"
identifiers = ["rds.amazonaws.com"]
}
}
}
resource "aws_iam_role" "this" {
count = var.create_neptune_iam_role ? 1 : 0
name = var.neptune_role_name
assume_role_policy = data.aws_iam_policy_document.this[0].json
description = var.neptune_role_description
permissions_boundary = var.neptune_role_permissions_boundary
tags = merge(
{
"Name" = format("%s", var.neptune_role_name)
},
try(var.tags, {}),
)
}
resource "aws_iam_role_policy_attachment" "this" {
count = var.create_neptune_iam_role ? 1 : 0
role = aws_iam_role.this[0].name
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/ROSAKMSProviderPolicy"
}
######################
# Random ID
######################
resource "random_id" "snapshot_suffix" {
keepers = {
cluster_identifier = aws_neptune_cluster.this[0].id
}
byte_length = 4
}