This repository has been archived by the owner on Jan 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.tf
241 lines (204 loc) · 7.1 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
/**
* # aws-terraform-elasticsearch
*
* This module creates an ElasticSearch cluster.
*
* ## Basic Usage
*
* ### Internet accessible endpoint
*
* ```HCL
* module "elasticsearch" {
* source = "[email protected]:rackspace-infrastructure-automation/aws-terraform-elasticsearch//?ref=v0.12.4"
*
* name = "es-internet-endpoint"
* ip_whitelist = ["1.2.3.4"]
* }
* ```
*
* ### VPC accessible endpoint
*
* ```HCL
* module "elasticsearch" {
* source = "[email protected]:rackspace-infrastructure-automation/aws-terraform-elasticsearch//?ref=v0.12.4"
*
* name = "es-vpc-endpoint"
* vpc_enabled = true
* security_groups = [module.sg.public_web_security_group_id]
* subnets = [module.vpc.private_subnets]
* }
* ```
*
* Full working references are available at [examples](examples)
*
* ## Limitation
*
* Terraform does not create the IAM Service Linked Role for ElasticSearch automatically. If this role is not present on an account, the `create_service_linked_role` parameter should be set to true for the first ElasticSearch instance. This will create the required role. This option should not be set to true on more than a single deployment per account, or it will result in a naming conflict. If the role is not present an error similar to the following would result:
* Error creating ElasticSearch domain: ValidationException: Before you can proceed, you must enable a service-linked role to give Amazon ES permissions to access your VPC.
* ```
* 1 error(s) occurred:
*
* * module.elasticsearch.aws_elasticsearch_domain.es: 1 error(s) occurred:
*
* * aws_elasticsearch_domain.es: Error reading IAM Role AWSServiceRoleForAmazonElasticsearchService: NoSuchEntity: The role with name AWSServiceRoleForAmazonElasticsearchService cannot be found.
* status code: 404, request id: 5a1614d2-1e64-11e9-a87e-3149d48d2026
* ```
*/
terraform {
required_version = ">= 0.12"
required_providers {
aws = ">= 2.2.0"
}
}
resource "random_string" "r_string" {
length = 5
upper = false
lower = true
number = true
special = false
}
locals {
vpc_lookup = var.vpc_enabled ? "vpc" : "standard"
enable_logging = var.logging_application_logs || var.logging_index_slow_logs || var.logging_search_slow_logs
za_subnet_count = length(var.subnets) >= 3 ? 3 : 2
tags = {
Environment = var.environment
Name = var.name
ServiceProvider = "Rackspace"
}
policy_condition = {
standard = [{
test = "IpAddress"
values = var.ip_whitelist
variable = "aws:SourceIp"
}]
vpc = []
}
vpc_configuration = {
standard = []
vpc = [
{
security_group_ids = var.security_groups
subnet_ids = var.subnets
},
]
}
}
data "aws_region" "current" {
}
data "aws_caller_identity" "current" {
}
data "aws_iam_policy_document" "policy" {
statement {
actions = ["es:*"]
effect = "Allow"
resources = ["arn:aws:es:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:domain/${var.name}/*"]
principals {
identifiers = ["*"]
type = "AWS"
}
dynamic "condition" {
for_each = local.policy_condition[local.vpc_lookup]
content {
test = condition.value.test
values = condition.value.values
variable = condition.value.variable
}
}
}
}
resource "aws_iam_service_linked_role" "slr" {
count = var.create_service_linked_role ? 1 : 0
aws_service_name = "es.amazonaws.com"
}
resource "aws_cloudwatch_log_group" "es" {
count = local.enable_logging ? 1 : 0
name_prefix = var.name
retention_in_days = var.logging_retention
tags = merge(var.tags, local.tags)
}
data "aws_iam_policy_document" "es_cloudwatch_policy" {
count = local.enable_logging ? 1 : 0
statement {
actions = ["logs:PutLogEvents", "logs:CreateLogStream", "logs:PutLogEventsBatch"]
effect = "Allow"
resources = ["${replace(aws_cloudwatch_log_group.es[0].arn, ":*", "")}:*"]
principals {
identifiers = ["es.amazonaws.com"]
type = "Service"
}
}
}
resource "aws_cloudwatch_log_resource_policy" "es_cloudwatch_policy" {
count = local.enable_logging ? 1 : 0
policy_document = element(data.aws_iam_policy_document.es_cloudwatch_policy.*.json, 0)
policy_name = "Elasticsearch-Log-Access-${random_string.r_string.result}"
}
resource "aws_elasticsearch_domain" "es" {
access_policies = var.use_custom_access_policy ? var.custom_access_policy : data.aws_iam_policy_document.policy.json
domain_name = lower(var.name)
elasticsearch_version = var.elasticsearch_version
tags = merge(var.tags, local.tags)
advanced_options = {
"rest.action.multi.allow_explicit_index" = "true",
"indices.query.bool.max_clause_count" = var.max_clause_count
}
cluster_config {
dedicated_master_count = var.master_node_count > 0 ? var.master_node_count : 0
dedicated_master_enabled = var.master_node_count > 0
dedicated_master_type = var.master_node_count > 0 ? var.master_node_instance_type : ""
instance_count = var.data_node_count
instance_type = var.data_node_instance_type
zone_awareness_enabled = var.zone_awareness_enabled
zone_awareness_config {
availability_zone_count = var.zone_awareness_enabled == "false" ? 2 : local.za_subnet_count
}
}
ebs_options {
ebs_enabled = true
iops = lower(var.ebs_type) == "io1" ? var.ebs_iops : 0
volume_size = var.ebs_size
volume_type = lower(var.ebs_type)
}
encrypt_at_rest {
enabled = var.encrypt_storage_enabled
kms_key_id = var.encryption_kms_key
}
log_publishing_options {
log_type = "INDEX_SLOW_LOGS"
cloudwatch_log_group_arn = element(concat(aws_cloudwatch_log_group.es.*.arn, [""]), 0)
enabled = var.logging_index_slow_logs
}
log_publishing_options {
log_type = "SEARCH_SLOW_LOGS"
cloudwatch_log_group_arn = element(concat(aws_cloudwatch_log_group.es.*.arn, [""]), 0)
enabled = var.logging_search_slow_logs
}
log_publishing_options {
log_type = "ES_APPLICATION_LOGS"
cloudwatch_log_group_arn = element(concat(aws_cloudwatch_log_group.es.*.arn, [""]), 0)
enabled = var.logging_application_logs
}
node_to_node_encryption {
enabled = var.encrypt_traffic_enabled
}
snapshot_options {
automated_snapshot_start_hour = var.snapshot_start_hour
}
dynamic "vpc_options" {
for_each = local.vpc_configuration[local.vpc_lookup]
content {
security_group_ids = lookup(vpc_options.value, "security_group_ids", null)
subnet_ids = lookup(vpc_options.value, "subnet_ids", null)
}
}
depends_on = [aws_iam_service_linked_role.slr]
}
resource "aws_route53_record" "zone_record_alias" {
count = var.internal_record_name != "" ? 1 : 0
name = "${var.internal_record_name}.${var.internal_zone_name}"
records = [aws_elasticsearch_domain.es.endpoint]
ttl = "300"
type = "CNAME"
zone_id = var.internal_zone_id
}