forked from johntran-fastly/tf_aws_elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
156 lines (126 loc) · 4.72 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
locals = {
log_groups = ["${var.index_slow_log_cloudwatch_log_group}", "${var.search_slow_log_cloudwatch_log_group}", "${var.es_app_log_cloudwatch_log_group}"]
}
# Elasticsearch domain
data "aws_iam_policy_document" "es_management_access" {
count = "${length(var.vpc_options["subnet_ids"]) > 0 ? 0 : 1}"
statement {
actions = [
"es:ESHttpGet",
"es:ESHttpHead",
"es:ESHttpPost",
"es:ESHttpPut",
]
resources = [
"${aws_elasticsearch_domain.es.arn}",
"${aws_elasticsearch_domain.es.arn}/*",
]
principals {
type = "AWS"
identifiers = ["${distinct(compact(var.management_iam_roles))}"]
}
condition {
test = "IpAddress"
variable = "aws:SourceIp"
values = ["${distinct(compact(var.management_public_ip_addresses))}"]
}
}
statement {
actions = [
"es:ESHttpDelete",
]
resources = ["${formatlist("${aws_elasticsearch_domain.es.arn}/%s-*/*/*", var.deny_del_indices_prefixes)}"]
principals {
type = "AWS"
identifiers = ["${distinct(compact(var.management_iam_roles))}"]
}
condition {
test = "IpAddress"
variable = "aws:SourceIp"
values = ["${distinct(compact(var.management_public_ip_addresses))}"]
}
}
}
resource "aws_cloudwatch_log_group" "index_slow_log" {
name = "${var.index_slow_log_cloudwatch_log_group}"
}
resource "aws_cloudwatch_log_group" "search_slow_log" {
name = "${var.search_slow_log_cloudwatch_log_group}"
}
resource "aws_cloudwatch_log_group" "es_app_log" {
name = "${var.es_app_log_cloudwatch_log_group}"
}
data "aws_iam_policy_document" "elasticsearch-log-publishing-policy" {
count = "${(var.index_slow_log_enabled || var.search_slow_log_enabled || var.es_app_log_enable) ? 1 : 0}"
statement {
actions = [
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:PutLogEventsBatch",
]
resources = ["arn:aws:logs:*"]
principals {
identifiers = ["es.amazonaws.com"]
type = "Service"
}
}
}
resource "aws_cloudwatch_log_resource_policy" "elasticsearch-log-publishing-policy" {
count = "${(var.index_slow_log_enabled || var.search_slow_log_enabled || var.es_app_log_enable) ? 1 : 0}"
policy_document = "${data.aws_iam_policy_document.elasticsearch-log-publishing-policy.json}"
policy_name = "elasticsearch-log-publishing-policy-${local.domain_name}"
}
resource "aws_elasticsearch_domain" "es" {
count = "${length(var.vpc_options["subnet_ids"]) > 0 ? 0 : 1}"
domain_name = "${local.domain_name}"
elasticsearch_version = "${var.es_version}"
depends_on = ["aws_cloudwatch_log_resource_policy.elasticsearch-log-publishing-policy"]
lifecycle {
ignore_changes = ["elasticsearch_version", "instance_type", "instance_count"]
}
log_publishing_options = [{
log_type = "INDEX_SLOW_LOGS"
cloudwatch_log_group_arn = "${aws_cloudwatch_log_group.index_slow_log.arn}"
enabled = "${var.index_slow_log_enabled}"
},
{
log_type = "SEARCH_SLOW_LOGS"
cloudwatch_log_group_arn = "${aws_cloudwatch_log_group.search_slow_log.arn}"
enabled = "${var.search_slow_log_enabled}"
},
{
log_type = "ES_APPLICATION_LOGS"
cloudwatch_log_group_arn = "${aws_cloudwatch_log_group.es_app_log.arn}"
enabled = "${var.es_app_log_enable}"
},
]
cluster_config {
instance_type = "${var.instance_type}"
instance_count = "${var.instance_count}"
dedicated_master_enabled = "${var.instance_count >= var.dedicated_master_threshold ? true : false}"
dedicated_master_count = "${var.instance_count >= var.dedicated_master_threshold ? 3 : 0}"
dedicated_master_type = "${var.instance_count >= var.dedicated_master_threshold ? (var.dedicated_master_type != "false" ? var.dedicated_master_type : var.instance_type) : ""}"
zone_awareness_config {
availability_zone_count = "${var.es_availability_zone_count}"
}
zone_awareness_enabled = "${var.es_zone_awareness}"
}
# advanced_options {
# }
ebs_options {
ebs_enabled = "${var.ebs_volume_size > 0 ? true : false}"
volume_size = "${var.ebs_volume_size}"
volume_type = "${var.ebs_volume_type}"
}
snapshot_options {
automated_snapshot_start_hour = "${var.snapshot_start_hour}"
}
tags = "${merge(var.tags, map(
"Domain", "${local.domain_name}"
))}"
}
resource "aws_elasticsearch_domain_policy" "es_management_access" {
count = "${length(var.vpc_options["subnet_ids"]) > 0 ? 0 : 1}"
domain_name = "${local.domain_name}"
access_policies = "${data.aws_iam_policy_document.es_management_access.json}"
}