|
| 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 | +} |
0 commit comments