1
1
module "user_label" {
2
2
source = " git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.19.2"
3
3
4
- enabled = module. this . enabled
5
- attributes = concat (module. this . attributes , [" user" ])
4
+ attributes = compact (concat (module. this . attributes , [" user" ]))
6
5
7
6
context = module. this . context
8
7
}
9
8
10
9
module "kibana_label" {
11
10
source = " git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.19.2"
12
11
13
- enabled = module. this . enabled
14
- attributes = concat (module. this . attributes , [" kibana" ])
12
+ attributes = compact (concat (module. this . attributes , [" kibana" ]))
15
13
16
14
context = module. this . context
17
15
}
@@ -97,20 +95,6 @@ data "aws_iam_policy_document" "assume_role" {
97
95
}
98
96
}
99
97
100
- # inspired by https://github.com/hashicorp/terraform/issues/20692
101
- # I use 0.12 new "dynamic" block - https://www.terraform.io/docs/configuration/expressions.html
102
- # If we have 1 az - the count of this resource equals 0, hence no config
103
- # block appears in the `aws_elasticsearch_domain`
104
- # If we have more than 1 - we set the trigger to the actual value of
105
- # `availability_zone_count`
106
- # and `dynamic` block kicks in
107
- resource "null_resource" "azs" {
108
- count = var. availability_zone_count > 1 ? 1 : 0
109
- triggers = {
110
- availability_zone_count = var.availability_zone_count
111
- }
112
- }
113
-
114
98
resource "aws_elasticsearch_domain" "default" {
115
99
count = module. this . enabled ? 1 : 0
116
100
domain_name = module. this . id
@@ -153,13 +137,13 @@ resource "aws_elasticsearch_domain" "default" {
153
137
dedicated_master_type = var. dedicated_master_type
154
138
zone_awareness_enabled = var. zone_awareness_enabled
155
139
warm_enabled = var. warm_enabled
156
- warm_count = var. warm_count
157
- warm_type = var. warm_type
140
+ warm_count = var. warm_enabled ? var . warm_count : null
141
+ warm_type = var. warm_enabled ? var . warm_type : null
158
142
159
143
dynamic "zone_awareness_config" {
160
- for_each = null_resource . azs [ * ] . triggers
144
+ for_each = var . availability_zone_count > 1 ? [ true ] : []
161
145
content {
162
- availability_zone_count = zone_awareness_config . value . availability_zone_count
146
+ availability_zone_count = var . availability_zone_count
163
147
}
164
148
}
165
149
}
@@ -218,6 +202,9 @@ data "aws_iam_policy_document" "default" {
218
202
count = module. this . enabled && (length (var. iam_authorizing_role_arns ) > 0 || length (var. iam_role_arns ) > 0 ) ? 1 : 0
219
203
220
204
statement {
205
+ sid = " AllowEsAccessToSpecifiedRoles"
206
+ effect = " Allow"
207
+
221
208
actions = distinct (compact (var. iam_actions ))
222
209
223
210
resources = [
@@ -229,14 +216,30 @@ data "aws_iam_policy_document" "default" {
229
216
type = " AWS"
230
217
identifiers = distinct (compact (concat (var. iam_role_arns , aws_iam_role. elasticsearch_user . * . arn )))
231
218
}
219
+ }
232
220
233
- # This condition is for non VPC ES to allow anonymous access from whitelisted IP ranges without requests signing
234
- # https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-ac.html#es-ac-types-ip
235
- # https://aws.amazon.com/premiumsupport/knowledge-center/anonymous-not-authorized-elasticsearch/
236
- dynamic "condition" {
237
- for_each = ! var. vpc_enabled && length (var. allowed_cidr_blocks ) > 0 ? [true ] : []
221
+ # This statement is for non VPC ES to allow anonymous access from whitelisted IP ranges without requests signing
222
+ # https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-ac.html#es-ac-types-ip
223
+ # https://aws.amazon.com/premiumsupport/knowledge-center/anonymous-not-authorized-elasticsearch/
224
+ dynamic "statement" {
225
+ for_each = length (var. allowed_cidr_blocks ) > 0 && ! var. vpc_enabled ? [true ] : []
226
+ content {
227
+ sid = " AllowAnonymousEsAccessFromCIDR"
228
+ effect = " Allow"
238
229
239
- content {
230
+ actions = distinct (compact (var. iam_actions ))
231
+
232
+ resources = [
233
+ join (" " , aws_elasticsearch_domain. default . * . arn ),
234
+ " ${ join (" " , aws_elasticsearch_domain. default . * . arn )} /*"
235
+ ]
236
+
237
+ principals {
238
+ type = " AWS"
239
+ identifiers = [" *" ]
240
+ }
241
+
242
+ condition {
240
243
test = " IpAddress"
241
244
values = var. allowed_cidr_blocks
242
245
variable = " aws:SourceIp"
@@ -270,7 +273,10 @@ module "kibana_hostname" {
270
273
dns_name = var. kibana_subdomain_name == " " ? module. kibana_label . id : var. kibana_subdomain_name
271
274
ttl = 60
272
275
zone_id = var. dns_zone_id
273
- records = [join (" " , aws_elasticsearch_domain. default . * . kibana_endpoint )]
276
+ # Note: kibana_endpoint is not just a domain name, it includes a path component,
277
+ # and as such is not suitable for a DNS record. The plain endpoint is the
278
+ # hostname portion and should be used for DNS.
279
+ records = [join (" " , aws_elasticsearch_domain. default . * . endpoint )]
274
280
275
281
context = module. this . context
276
282
}
0 commit comments