Skip to content

Commit 1d6e397

Browse files
committed
Uploading files
0 parents  commit 1d6e397

File tree

6 files changed

+280
-0
lines changed

6 files changed

+280
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Aplyca
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Terraform AWS ElasticSearch domian
2+
==================================
3+
4+
Create a ElasticSearch domain
5+
6+
7+
Example:
8+
9+
```
10+
module "search" {
11+
source = "Aplyca/elasticsearch/aws"
12+
13+
name = "My ES cluster"
14+
vpc_id = "vpc-bsasdsf"
15+
newbits = 10
16+
netnum = 16
17+
azs = ["us-east1"]
18+
rt_id = "rt-adfarwr"
19+
access_sg_ids = ["sg-rewr4sre"]
20+
access_cidrs = ["172.168.0.0/26"]
21+
storage = 25
22+
23+
tags {
24+
App = "my App"
25+
Environment = "Prod"
26+
}
27+
}
28+
```

data.tf

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
data "aws_vpc" "this" {
2+
id = "${var.vpc_id}"
3+
}

main.tf

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
locals {
2+
id = "${replace(var.name, " ", "-")}"
3+
}
4+
5+
# -----------------------------------------------
6+
# Create Private subnets
7+
# -----------------------------------------------
8+
resource "aws_subnet" "this" {
9+
count = "${length(var.azs)}"
10+
vpc_id = "${data.aws_vpc.this.id}"
11+
cidr_block = "${cidrsubnet(data.aws_vpc.this.cidr_block, var.newbits, var.netnum + count.index)}"
12+
availability_zone = "${element(var.azs, count.index)}"
13+
map_public_ip_on_launch = false
14+
tags = "${merge(var.tags, map("Name", "${var.name} ES ${count.index}"))}"
15+
}
16+
17+
resource "aws_route_table_association" "this" {
18+
count = "${length(aws_subnet.this.*.id)}"
19+
subnet_id = "${element(aws_subnet.this.*.id, count.index)}"
20+
route_table_id = "${var.rt_id}"
21+
}
22+
23+
# ---------------------------------------
24+
# Network ACL DB
25+
# ---------------------------------------
26+
resource "aws_network_acl" "this" {
27+
vpc_id = "${data.aws_vpc.this.id}"
28+
subnet_ids = ["${aws_subnet.this.*.id}"]
29+
tags = "${merge(var.tags, map("Name", "${var.name} ES"))}"
30+
}
31+
32+
# ---------------------------------------
33+
# Network ACL Inbound/Outbound DB
34+
# ---------------------------------------
35+
resource "aws_network_acl_rule" "inbound_https" {
36+
count = "${length(var.access_cidrs)}"
37+
network_acl_id = "${aws_network_acl.this.id}"
38+
rule_number = "${100+count.index}"
39+
egress = false
40+
protocol = "tcp"
41+
rule_action = "allow"
42+
cidr_block = "${element(var.access_cidrs, count.index)}"
43+
from_port = 443
44+
to_port = 443
45+
}
46+
47+
resource "aws_network_acl_rule" "inbound_http" {
48+
count = "${length(var.access_cidrs)}"
49+
network_acl_id = "${aws_network_acl.this.id}"
50+
rule_number = "${(200+count.index)}"
51+
egress = false
52+
protocol = "tcp"
53+
rule_action = "allow"
54+
cidr_block = "${element(var.access_cidrs, count.index)}"
55+
from_port = 80
56+
to_port = 80
57+
}
58+
59+
resource "aws_network_acl_rule" "outbound" {
60+
count = "${length(var.access_cidrs)}"
61+
network_acl_id = "${aws_network_acl.this.id}"
62+
rule_number = "${(count.index+1)*100}"
63+
egress = true
64+
protocol = "tcp"
65+
rule_action = "allow"
66+
cidr_block = "${element(var.access_cidrs, count.index)}"
67+
from_port = 1024
68+
to_port = 65535
69+
}
70+
71+
# Security group Database access
72+
resource "aws_security_group" "this" {
73+
name = "${local.id}-ES"
74+
description = "Access to ElasticSearch port"
75+
vpc_id = "${data.aws_vpc.this.id}"
76+
77+
tags = "${merge(var.tags, map("Name", "${var.name} ES"))}"
78+
}
79+
80+
resource "aws_security_group_rule" "egress" {
81+
type = "egress"
82+
security_group_id = "${aws_security_group.this.id}"
83+
from_port = 0
84+
to_port = 0
85+
protocol = "-1"
86+
cidr_blocks = ["0.0.0.0/0"]
87+
description = "Access to all egress targets"
88+
}
89+
90+
resource "aws_security_group_rule" "ingress_https" {
91+
count = "${length(var.access_sg_ids)}"
92+
type = "ingress"
93+
security_group_id = "${aws_security_group.this.id}"
94+
from_port = "443"
95+
to_port = "443"
96+
protocol = "tcp"
97+
source_security_group_id = "${element(var.access_sg_ids, count.index)}"
98+
description = "Access from Source"
99+
}
100+
101+
resource "aws_security_group_rule" "ingress_http" {
102+
count = "${length(var.access_sg_ids)}"
103+
type = "ingress"
104+
security_group_id = "${aws_security_group.this.id}"
105+
from_port = "80"
106+
to_port = "80"
107+
protocol = "tcp"
108+
source_security_group_id = "${element(var.access_sg_ids, count.index)}"
109+
description = "Access from Source"
110+
}
111+
112+
resource "aws_elasticsearch_domain" "this" {
113+
domain_name = "${lower(local.id)}"
114+
elasticsearch_version = "${var.es_version}"
115+
cluster_config {
116+
instance_type = "${var.type}"
117+
instance_count = "${var.instances}"
118+
}
119+
120+
vpc_options {
121+
security_group_ids = ["${aws_security_group.this.id}"]
122+
subnet_ids = ["${aws_subnet.this.*.id}"]
123+
}
124+
125+
ebs_options {
126+
ebs_enabled = true
127+
volume_size = "${var.storage}"
128+
}
129+
130+
snapshot_options {
131+
automated_snapshot_start_hour = 1
132+
}
133+
134+
tags = "${merge(var.tags, map("Name", var.name))}"
135+
}
136+
137+
138+
resource "aws_elasticsearch_domain_policy" "this" {
139+
domain_name = "${aws_elasticsearch_domain.this.domain_name}"
140+
141+
access_policies = <<POLICIES
142+
{
143+
"Version": "2012-10-17",
144+
"Statement": [
145+
{
146+
"Effect": "Allow",
147+
"Principal": {
148+
"AWS": [
149+
"*"
150+
]
151+
},
152+
"Action": [
153+
"es:*"
154+
],
155+
"Resource": "${aws_elasticsearch_domain.this.arn}/*"
156+
}
157+
]
158+
}
159+
POLICIES
160+
}

outputs.tf

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "endpoint" {
2+
value = "${aws_elasticsearch_domain.this.endpoint}"
3+
}

variables.tf

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
variable "name" {
2+
description = "Name prefix for all EFS resources."
3+
default = "App"
4+
}
5+
6+
variable "azs" {
7+
description = "A list of availability zones to associate with."
8+
type = "list"
9+
default = []
10+
}
11+
12+
variable "access_sg_ids" {
13+
description = "A list of security groups Ids to grant access."
14+
type = "list"
15+
default = []
16+
}
17+
18+
variable "vpc_id" {
19+
description = "VPC Id where the EFS resources will be deployed."
20+
}
21+
22+
variable "newbits" {
23+
description = "newbits in the cidrsubnet function."
24+
default = 26
25+
}
26+
27+
variable "netnum" {
28+
description = "netnum in the cidrsubnet function."
29+
default = 0
30+
}
31+
32+
variable "rt_id" {
33+
description = "Route Table Id to assing to the EFS subnet."
34+
}
35+
36+
variable "access_cidrs" {
37+
description = "A list of Subnets CIDR Blocks to grant access"
38+
type = "list"
39+
default = []
40+
}
41+
42+
variable "tags" {
43+
description = "A mapping of tags to assign to the resource."
44+
default = {}
45+
}
46+
47+
variable "es_version" {
48+
description = "Version"
49+
default = "6.2"
50+
}
51+
52+
variable "storage" {
53+
description = "Storage size"
54+
default = 10
55+
}
56+
57+
variable "type" {
58+
description = "Instance type"
59+
default = "t2.small.elasticsearch"
60+
}
61+
62+
variable "instances" {
63+
description = "Instance count"
64+
default = 1
65+
}

0 commit comments

Comments
 (0)