forked from cloudposse/terraform-aws-elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
325 lines (268 loc) · 10.5 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
317
318
319
320
321
322
323
324
325
module "user_label" {
source = "cloudposse/label/null"
version = "0.25.0"
attributes = ["user"]
context = module.this.context
}
module "kibana_label" {
source = "cloudposse/label/null"
version = "0.25.0"
attributes = ["kibana"]
context = module.this.context
}
resource "aws_security_group" "default" {
count = module.this.enabled && var.vpc_enabled ? 1 : 0
vpc_id = var.vpc_id
name = module.this.id
description = "Allow inbound traffic from Security Groups and CIDRs. Allow all outbound traffic"
tags = module.this.tags
lifecycle {
create_before_destroy = true
}
}
resource "aws_security_group_rule" "ingress_security_groups" {
count = module.this.enabled && var.vpc_enabled ? length(var.security_groups) : 0
description = "Allow inbound traffic from Security Groups"
type = "ingress"
from_port = var.ingress_port_range_start
to_port = var.ingress_port_range_end
protocol = "tcp"
source_security_group_id = var.security_groups[count.index]
security_group_id = join("", aws_security_group.default.*.id)
}
resource "aws_security_group_rule" "ingress_cidr_blocks" {
count = module.this.enabled && var.vpc_enabled && length(var.allowed_cidr_blocks) > 0 ? 1 : 0
description = "Allow inbound traffic from CIDR blocks"
type = "ingress"
from_port = var.ingress_port_range_start
to_port = var.ingress_port_range_end
protocol = "tcp"
cidr_blocks = var.allowed_cidr_blocks
security_group_id = join("", aws_security_group.default.*.id)
}
resource "aws_security_group_rule" "egress" {
count = module.this.enabled && var.vpc_enabled ? 1 : 0
description = "Allow all egress traffic"
type = "egress"
from_port = 0
to_port = 65535
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = join("", aws_security_group.default.*.id)
}
# https://github.com/terraform-providers/terraform-provider-aws/issues/5218
resource "aws_iam_service_linked_role" "default" {
count = module.this.enabled && var.create_iam_service_linked_role ? 1 : 0
aws_service_name = "es.amazonaws.com"
description = "AWSServiceRoleForAmazonElasticsearchService Service-Linked Role"
}
# Role that pods can assume for access to elasticsearch and kibana
resource "aws_iam_role" "elasticsearch_user" {
count = module.this.enabled && (length(var.iam_authorizing_role_arns) > 0 || length(var.iam_role_arns) > 0) ? 1 : 0
name = module.user_label.id
assume_role_policy = join("", data.aws_iam_policy_document.assume_role.*.json)
description = "IAM Role to assume to access the Elasticsearch ${module.this.id} cluster"
tags = module.user_label.tags
max_session_duration = var.iam_role_max_session_duration
permissions_boundary = var.iam_role_permissions_boundary
}
data "aws_iam_policy_document" "assume_role" {
count = module.this.enabled && (length(var.iam_authorizing_role_arns) > 0 || length(var.iam_role_arns) > 0) ? 1 : 0
statement {
actions = [
"sts:AssumeRole"
]
principals {
type = "Service"
identifiers = var.aws_ec2_service_name
}
principals {
type = "AWS"
identifiers = compact(concat(var.iam_authorizing_role_arns, var.iam_role_arns))
}
effect = "Allow"
}
}
resource "aws_elasticsearch_domain" "default" {
count = module.this.enabled ? 1 : 0
domain_name = module.this.id
elasticsearch_version = var.elasticsearch_version
advanced_options = var.advanced_options
advanced_security_options {
enabled = var.advanced_security_options_enabled
internal_user_database_enabled = var.advanced_security_options_internal_user_database_enabled
master_user_options {
master_user_arn = var.advanced_security_options_master_user_arn
master_user_name = var.advanced_security_options_master_user_name
master_user_password = var.advanced_security_options_master_user_password
}
}
ebs_options {
ebs_enabled = var.ebs_volume_size > 0 ? true : false
volume_size = var.ebs_volume_size
volume_type = var.ebs_volume_type
iops = var.ebs_iops
}
encrypt_at_rest {
enabled = var.encrypt_at_rest_enabled
kms_key_id = var.encrypt_at_rest_kms_key_id
}
domain_endpoint_options {
enforce_https = var.domain_endpoint_options_enforce_https
tls_security_policy = var.domain_endpoint_options_tls_security_policy
custom_endpoint_enabled = var.custom_endpoint_enabled
custom_endpoint = var.custom_endpoint_enabled ? var.custom_endpoint : null
custom_endpoint_certificate_arn = var.custom_endpoint_enabled ? var.custom_endpoint_certificate_arn : null
}
cluster_config {
instance_count = var.instance_count
instance_type = var.instance_type
dedicated_master_enabled = var.dedicated_master_enabled
dedicated_master_count = var.dedicated_master_count
dedicated_master_type = var.dedicated_master_type
zone_awareness_enabled = var.zone_awareness_enabled
warm_enabled = var.warm_enabled
warm_count = var.warm_enabled ? var.warm_count : null
warm_type = var.warm_enabled ? var.warm_type : null
dynamic "zone_awareness_config" {
for_each = var.availability_zone_count > 1 && var.zone_awareness_enabled ? [true] : []
content {
availability_zone_count = var.availability_zone_count
}
}
}
node_to_node_encryption {
enabled = var.node_to_node_encryption_enabled
}
dynamic "vpc_options" {
for_each = var.vpc_enabled ? [true] : []
content {
security_group_ids = [join("", aws_security_group.default.*.id)]
subnet_ids = var.subnet_ids
}
}
snapshot_options {
automated_snapshot_start_hour = var.automated_snapshot_start_hour
}
dynamic "cognito_options" {
for_each = var.cognito_authentication_enabled ? [true] : []
content {
enabled = true
user_pool_id = var.cognito_user_pool_id
identity_pool_id = var.cognito_identity_pool_id
role_arn = var.cognito_iam_role_arn
}
}
log_publishing_options {
enabled = var.log_publishing_index_enabled
log_type = "INDEX_SLOW_LOGS"
cloudwatch_log_group_arn = var.log_publishing_index_cloudwatch_log_group_arn
}
log_publishing_options {
enabled = var.log_publishing_search_enabled
log_type = "SEARCH_SLOW_LOGS"
cloudwatch_log_group_arn = var.log_publishing_search_cloudwatch_log_group_arn
}
log_publishing_options {
enabled = var.log_publishing_audit_enabled
log_type = "AUDIT_LOGS"
cloudwatch_log_group_arn = var.log_publishing_audit_cloudwatch_log_group_arn
}
log_publishing_options {
enabled = var.log_publishing_application_enabled
log_type = "ES_APPLICATION_LOGS"
cloudwatch_log_group_arn = var.log_publishing_application_cloudwatch_log_group_arn
}
tags = module.this.tags
depends_on = [aws_iam_service_linked_role.default]
}
data "aws_iam_policy_document" "default" {
count = module.this.enabled && (length(var.iam_authorizing_role_arns) > 0 || length(var.iam_role_arns) > 0) ? 1 : 0
statement {
effect = "Allow"
actions = distinct(compact(var.iam_actions))
resources = [
join("", aws_elasticsearch_domain.default.*.arn),
"${join("", aws_elasticsearch_domain.default.*.arn)}/*"
]
principals {
type = "AWS"
identifiers = distinct(compact(concat(var.iam_role_arns, aws_iam_role.elasticsearch_user.*.arn)))
}
}
# This statement is for non VPC ES to allow anonymous access from whitelisted IP ranges without requests signing
# https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-ac.html#es-ac-types-ip
# https://aws.amazon.com/premiumsupport/knowledge-center/anonymous-not-authorized-elasticsearch/
dynamic "statement" {
for_each = length(var.allowed_cidr_blocks) > 0 && ! var.vpc_enabled ? [true] : []
content {
effect = "Allow"
actions = distinct(compact(var.iam_actions))
resources = [
join("", aws_elasticsearch_domain.default.*.arn),
"${join("", aws_elasticsearch_domain.default.*.arn)}/*"
]
principals {
type = "AWS"
identifiers = ["*"]
}
condition {
test = "IpAddress"
values = var.allowed_cidr_blocks
variable = "aws:SourceIp"
}
}
}
}
# resource "aws_elasticsearch_domain_policy" "default" {
# count = module.this.enabled && (length(var.iam_authorizing_role_arns) > 0 || length(var.iam_role_arns) > 0) ? 1 : 0
# domain_name = module.this.id
# access_policies = join("", data.aws_iam_policy_document.default.*.json)
# }
data "aws_iam_policy_document" "opensearch" {
statement {
actions = [
"es:*"
]
effect = "Allow"
principals {
type = "AWS"
identifiers = [
"*"
]
}
resources = [
"${aws_elasticsearch_domain.default[0].arn}/*"
]
}
}
resource "aws_elasticsearch_domain_policy" "this" {
domain_name = module.this.id
access_policies = data.aws_iam_policy_document.opensearch.json
depends_on = [
aws_elasticsearch_domain.default[0]
]
}
module "domain_hostname" {
source = "cloudposse/route53-cluster-hostname/aws"
version = "0.12.2"
enabled = module.this.enabled && var.domain_hostname_enabled
dns_name = var.elasticsearch_subdomain_name == "" ? module.this.id : var.elasticsearch_subdomain_name
ttl = 60
zone_id = var.dns_zone_id
records = [join("", aws_elasticsearch_domain.default.*.endpoint)]
context = module.this.context
}
module "kibana_hostname" {
source = "cloudposse/route53-cluster-hostname/aws"
version = "0.12.2"
enabled = module.this.enabled && var.kibana_hostname_enabled
dns_name = var.kibana_subdomain_name == "" ? module.kibana_label.id : var.kibana_subdomain_name
ttl = 60
zone_id = var.dns_zone_id
# Note: kibana_endpoint is not just a domain name, it includes a path component,
# and as such is not suitable for a DNS record. The plain endpoint is the
# hostname portion and should be used for DNS.
records = [join("", aws_elasticsearch_domain.default.*.endpoint)]
context = module.this.context
}