-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathelasticsearch.tf
163 lines (134 loc) · 4.57 KB
/
elasticsearch.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
locals {
es_subnets = var.es_instance_count <= 2 ? [module.vpc.elasticache_subnets[0]] : module.vpc.elasticache_subnets
}
######################
# ElasticSearch Domain
######################
resource "aws_elasticsearch_domain" "this" {
count = var.use_elasticsearch == true ? 1 : 0
domain_name = var.namespace
elasticsearch_version = var.es_version
tags = var.tags
cluster_config {
instance_type = var.es_instance_type
instance_count = var.es_instance_count
dedicated_master_enabled = var.es_dedicated_master_enabled
dedicated_master_type = var.es_dedicated_master_type
dedicated_master_count = var.es_dedicated_master_count
zone_awareness_enabled = var.es_zone_awareness_enabled
dynamic "zone_awareness_config" {
for_each = var.es_zone_awareness_enabled == true ? [1] : []
content {
availability_zone_count = var.es_availability_zone_count
}
}
}
snapshot_options {
automated_snapshot_start_hour = var.es_snapshot_hour
}
ebs_options {
ebs_enabled = true
volume_type = var.es_volume_type
volume_size = var.es_volume_size
}
vpc_options {
subnet_ids = local.es_subnets
security_group_ids = [module.vpc.default_security_group_id, aws_security_group.elasticsearch[0].id]
}
log_publishing_options {
cloudwatch_log_group_arn = aws_cloudwatch_log_group.elasticsearch[0].arn
log_type = "INDEX_SLOW_LOGS"
}
log_publishing_options {
cloudwatch_log_group_arn = aws_cloudwatch_log_group.elasticsearch[0].arn
log_type = "SEARCH_SLOW_LOGS"
}
log_publishing_options {
cloudwatch_log_group_arn = aws_cloudwatch_log_group.elasticsearch[0].arn
log_type = "ES_APPLICATION_LOGS"
}
}
###############################################
# IAM Policy Document for ElasticSearch Logging
###############################################
data "aws_iam_policy_document" "elasticsearch_logs" {
statement {
sid = "PutCloudWatchLogs"
principals {
type = "Service"
identifiers = ["es.amazonaws.com"]
}
actions = [
"logs:PutLogEvents",
"logs:PutLogEventsBatch",
"logs:CreateLogStream",
]
resources = ["arn:aws:logs:*"]
}
}
#####################################
# CloudWatchLogPolicy - ElasticSearch
#####################################
resource "aws_cloudwatch_log_resource_policy" "elasticsearch" {
count = var.use_elasticsearch == true ? 1 : 0
policy_document = data.aws_iam_policy_document.elasticsearch_logs.json
policy_name = "elasticsearch-${var.namespace}-policy"
}
##########################
# LogGroup - ElasticSearch
##########################
resource "aws_cloudwatch_log_group" "elasticsearch" {
count = var.use_elasticsearch == true ? 1 : 0
name = "/${var.environment_name}/application/elasticsearch"
tags = var.tags
}
##############################################
# IAM Policy Document for ElasticSearch Access
##############################################
data "aws_iam_policy_document" "elasticsearch_access" {
statement {
sid = "AllowElasticSearchAccess"
principals {
type = "AWS"
identifiers = ["*"]
}
actions = [
"es:*"
]
# condition {
# test = "IpAddress"
# variable = "aws:SourceIp"
# values = [module.vpc.vpc_cidr_block]
# }
resources = var.use_elasticsearch == true ? ["${aws_elasticsearch_domain.this[0].arn}/*"] : []
}
}
resource "aws_elasticsearch_domain_policy" "this" {
count = var.use_elasticsearch == true ? 1 : 0
domain_name = aws_elasticsearch_domain.this[0].domain_name
access_policies = data.aws_iam_policy_document.elasticsearch_access.json
}
##########################################
# Route53 CNAME for ElasticSearch Endpoint
##########################################
resource "aws_route53_record" "elasticsearch" {
count = var.use_elasticsearch == true ? 1 : 0
zone_id = local.zone_id
name = "elasticsearch"
type = "CNAME"
ttl = "300"
records = [aws_elasticsearch_domain.this[0].endpoint]
}
# TODO set this up as a redirect
# https://stackoverflow.com/questions/10115799/set-up-dns-based-url-forwarding-in-amazon-route53/14289082#14289082
########################################
# Route53 CNAME for ElasticSearch Kibana
########################################
resource "aws_route53_record" "kibana" {
count = var.use_elasticsearch == true ? 1 : 0
zone_id = local.zone_id
name = "kibana"
type = "CNAME"
ttl = "300"
records = [aws_elasticsearch_domain.this[0].endpoint]
}