-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.tf
336 lines (293 loc) · 10.8 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
326
327
328
329
330
331
332
333
334
335
336
##------------------------------------------------------------------------------
## Labels module callled that will be used for naming and tags.
##------------------------------------------------------------------------------
module "labels" {
source = "clouddrove/labels/aws"
version = "1.3.0"
enabled = var.enabled
name = var.name
repository = var.repository
environment = var.environment
managedby = var.managedby
attributes = var.attributes
label_order = var.label_order
}
##------------------------------------------------------------------------------
## A log group is a group of log streams that share the same retention, monitoring, and access control settings.
##------------------------------------------------------------------------------
#tfsec:ignore:aws-cloudwatch-log-group-customer-key
resource "aws_cloudwatch_log_group" "cloudwatch" {
count = var.enabled && var.enable_logs ? 1 : 0
name = module.labels.id
tags = module.labels.tags
retention_in_days = var.retention_in_days
kms_key_id = var.cloudwatch_kms_key_id
}
resource "aws_cloudwatch_log_resource_policy" "cloudwatch_policy" {
count = var.enabled && var.enable_logs ? 1 : 0
policy_name = module.labels.id
policy_document = data.aws_iam_policy_document.elasticsearch-log-publishing-policy.json
}
##------------------------------------------------------------------------------
## Iam Service Linked Role.
## Terraform module to create Iam Service Linked Role resource on AWS.
##------------------------------------------------------------------------------
resource "aws_iam_service_linked_role" "default" {
count = var.enabled && var.enable_iam_service_linked_role ? 1 : 0
aws_service_name = "es.amazonaws.com"
description = "The description of the role."
tags = module.labels.tags
}
##------------------------------------------------------------------------------
## Terraform module to create Iam Role resource on AWS.
##------------------------------------------------------------------------------
resource "aws_iam_role" "default" {
count = var.enabled ? 1 : 0
name = format("%s-role", module.labels.id)
assume_role_policy = join("", data.aws_iam_policy_document.assume_role[*].json)
description = "IAM Role to assume to access the Elasticsearch cluster"
tags = module.labels.tags
}
data "aws_iam_policy_document" "assume_role" {
count = var.enabled ? 1 : 0
statement {
actions = [
"sts:AssumeRole"
]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
principals {
type = "AWS"
identifiers = ["*"]
}
effect = "Allow"
}
}
data "aws_iam_policy_document" "elasticsearch-log-publishing-policy" {
statement {
actions = [
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:PutLogEventsBatch",
]
resources = ["arn:aws:logs:*"]
principals {
identifiers = ["es.amazonaws.com"]
type = "Service"
}
}
}
data "aws_iam_policy_document" "cognito_es_policy" {
version = "2012-10-17"
statement {
effect = "Allow"
actions = [
"cognito-idp:DescribeUserPool",
"cognito-idp:CreateUserPoolClient",
"cognito-idp:DeleteUserPoolClient",
"cognito-idp:DescribeUserPoolClient",
"cognito-idp:AdminInitiateAuth",
"cognito-idp:AdminUserGlobalSignOut",
"cognito-idp:ListUserPoolClients",
"cognito-identity:DescribeIdentityPool",
"cognito-identity:UpdateIdentityPool",
"cognito-identity:SetIdentityPoolRoles",
"cognito-identity:GetIdentityPoolRoles"
]
resources = [
"*",
]
}
}
data "aws_iam_policy_document" "es_assume_policy" {
version = "2012-10-17"
statement {
effect = "Allow"
principals {
type = "Service"
identifiers = ["es.amazonaws.com"]
}
actions = ["sts:AssumeRole"]
}
}
module "cognito-role" {
source = "clouddrove/iam-role/aws"
version = "1.3.1"
name = format("%s-cognito-role", module.labels.id)
environment = var.environment
label_order = ["name"]
enabled = var.cognito_enabled
assume_role_policy = data.aws_iam_policy_document.es_assume_policy.json
policy_enabled = true
policy = data.aws_iam_policy_document.cognito_es_policy.json
}
##------------------------------------------------------------------------------
## Terraform resource for managing an AWS Elasticsearch Domain.
##------------------------------------------------------------------------------
#tfsec:ignore:aws-elastic-search-use-secure-tls-policy
resource "aws_elasticsearch_domain" "default" {
count = var.enabled ? 1 : 0
domain_name = var.domain_name != "" ? var.domain_name : module.labels.id
elasticsearch_version = var.elasticsearch_version
advanced_options = var.advanced_options
ebs_options {
ebs_enabled = var.volume_size > 0 ? true : false
volume_size = var.volume_size
volume_type = var.volume_type
iops = var.iops
}
auto_tune_options {
desired_state = var.auto_tune_desired_state
rollback_on_disable = var.rollback_on_disable
}
cognito_options {
enabled = var.cognito_enabled
user_pool_id = var.user_pool_id
identity_pool_id = var.identity_pool_id
role_arn = module.cognito-role.arn
}
encrypt_at_rest {
enabled = var.encrypt_at_rest_enabled
kms_key_id = var.kms_key_id
}
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 ? [true] : []
content {
availability_zone_count = var.availability_zone_count
}
}
}
node_to_node_encryption {
enabled = var.encryption_enabled
}
dynamic "vpc_options" {
for_each = var.vpc_enabled ? [true] : []
content {
security_group_ids = var.security_group_ids
subnet_ids = var.subnet_ids
}
}
snapshot_options {
automated_snapshot_start_hour = var.automated_snapshot_start_hour
}
log_publishing_options {
enabled = var.log_publishing_index_enabled
log_type = "INDEX_SLOW_LOGS"
cloudwatch_log_group_arn = format("%s:*", join("", aws_cloudwatch_log_group.cloudwatch[*].arn))
}
log_publishing_options {
enabled = var.log_publishing_search_enabled
log_type = "SEARCH_SLOW_LOGS"
cloudwatch_log_group_arn = format("%s:*", join("", aws_cloudwatch_log_group.cloudwatch[*].arn))
}
log_publishing_options {
enabled = var.log_publishing_application_enabled
log_type = "ES_APPLICATION_LOGS"
cloudwatch_log_group_arn = format("%s:*", join("", aws_cloudwatch_log_group.cloudwatch[*].arn))
}
log_publishing_options {
enabled = var.log_publishing_audit_enabled
log_type = "AUDIT_LOGS"
cloudwatch_log_group_arn = format("%s:*", join("", aws_cloudwatch_log_group.cloudwatch[*].arn))
}
domain_endpoint_options {
enforce_https = var.enforce_https
tls_security_policy = var.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
}
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
}
}
tags = module.labels.tags
}
##------------------------------------------------------------------------------
## Terraform module to create Elasticsearch Role Policy on AWS.
##------------------------------------------------------------------------------
data "aws_iam_policy_document" "default" {
count = var.enabled ? 1 : 0
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"
}
}
}
}
data "aws_iam_policy_document" "vpc" {
count = var.enabled ? 1 : 0
statement {
actions = distinct(compact(var.iam_actions))
effect = "Allow"
resources = [
join("", aws_elasticsearch_domain.default[*].arn),
format("%s/*", join("", aws_elasticsearch_domain.default[*].arn))
]
principals {
type = "AWS"
identifiers = ["*"]
}
}
}
#Module : Elasticsearch Policy
resource "aws_elasticsearch_domain_policy" "default" {
count = var.enabled ? 1 : 0
domain_name = var.domain_name != "" ? var.domain_name : module.labels.id
access_policies = var.vpc_enabled ? join("", data.aws_iam_policy_document.vpc[*].json) : join("", data.aws_iam_policy_document.default[*].json)
}
#Module : ROUTE53
#Description : Provides a Route53 record resource.
module "es_dns" {
source = "clouddrove/route53-record/aws"
version = "1.0.1"
record_enabled = var.dns_enabled
zone_id = var.dns_zone_id
name = var.es_hostname
type = var.type
ttl = var.ttl
values = join("", aws_elasticsearch_domain.default[*].endpoint)
}
#Module : ROUTE53
#Description : Provides a Route53 record resource.
module "kibana_dns" {
source = "clouddrove/route53-record/aws"
version = "1.0.1"
record_enabled = var.dns_enabled
zone_id = var.dns_zone_id
name = var.kibana_hostname
type = var.type
ttl = var.ttl
values = join("", aws_elasticsearch_domain.default[*].endpoint)
}