|
| 1 | +resource "aws_instance" "elasticsearch_instance" { |
| 2 | + count = "${var.instances_count}" |
| 3 | + depends_on = ["aws_ebs_volume.elasticsearch_volume"] |
| 4 | + ami = "${var.ami_id}" |
| 5 | + instance_type = "${var.instance_type}" |
| 6 | + subnet_id = "${element(var.vpc_subnets, count.index)}" |
| 7 | + key_name = "${var.instance_key_name}" |
| 8 | + iam_instance_profile = "${aws_iam_instance_profile.elasticsearch.name}" |
| 9 | + vpc_security_group_ids = ["${concat(var.security_groups, list(aws_security_group.elasticsearch.id))}"] |
| 10 | + associate_public_ip_address = false |
| 11 | + source_dest_check = false |
| 12 | + disable_api_termination = "${var.enable_termination_protection}" |
| 13 | + instance_initiated_shutdown_behavior = "stop" |
| 14 | + |
| 15 | + tags { |
| 16 | + Env = "${var.env_name}" |
| 17 | + Name = "${var.env_name}: ${var.verbose_name} Elasticsearch ${count.index}" |
| 18 | + } |
| 19 | + user_data = <<USER_DATA_END |
| 20 | +#cloud-config |
| 21 | +write_files: |
| 22 | +- path: /usr/bin/install-unix-tools |
| 23 | + encoding: b64 |
| 24 | + content: ${base64encode(file("${path.module}/../resources/install-unix-tools.sh"))} |
| 25 | + owner: root:root |
| 26 | + permissions: '0755' |
| 27 | +- path: /etc/dive-in-docker.conf |
| 28 | + content: elasticsearch |
| 29 | +- path: /etc/ecs/ecs.config |
| 30 | + content: | |
| 31 | + ECS_CLUSTER=${var.ecs_cluster_name} |
| 32 | + ECS_AVAILABLE_LOGGING_DRIVERS=["json-file","syslog","journald","gelf","awslogs"] |
| 33 | +- path: /etc/sysctl.d/01-elasticsearch.conf |
| 34 | + content: | |
| 35 | + vm.max_map_count = 262144 |
| 36 | +runcmd: |
| 37 | + - [ cloud-init-per, once, "install-unix-tools", "install-unix-tools", "-t", "1.0", "full"] |
| 38 | + - [ cloud-init-per, once, "set-hostname", "aws-set-hostname", "${lower(var.verbose_name)}graylog-elasticsearch-{count.index}", "-s"] |
| 39 | + - [ cloud-init-per, once, "read-custom-syslog", "sysctl", "-p", "/etc/sysctl.d/01-elasticsearch.conf"] |
| 40 | + - [ cloud-init-per, once, "docker-stop", "service", "docker", "stop"] |
| 41 | + - [ cloud-init-per, once, "mount-ebs", "mount-ebs", "${var.data_volume_device}", "${var.data_volume_path}", "0777" ] |
| 42 | + - [ cloud-init-per, once, "docker-start", "service", "docker", "start"] |
| 43 | + - [ cloud-init-per, once, "start-ecs", "start", "ecs"] |
| 44 | +USER_DATA_END |
| 45 | +} |
| 46 | + |
| 47 | +resource "aws_ebs_volume" "elasticsearch_volume" { |
| 48 | + count = "${length(var.instances_count)}" |
| 49 | + availability_zone = "${element(var.availability_zones, count.index)}" |
| 50 | + size = "${var.storage_size}" |
| 51 | + |
| 52 | + tags { |
| 53 | + Env = "${var.env_name}" |
| 54 | + Name = "${var.env_name}: ${var.verbose_name} Elasticseach Volume ${count.index}" |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +resource "aws_volume_attachment" "elasticsearch_volume_attachement" { |
| 59 | + count = "${length(var.instances_count)}" |
| 60 | + device_name = "${var.data_volume_device}" |
| 61 | + force_detach = true |
| 62 | + volume_id = "${element(aws_ebs_volume.elasticsearch_volume.*.id, count.index)}" |
| 63 | + instance_id = "${element(aws_instance.elasticsearch_instance.*.id, count.index)}" |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | +resource "aws_security_group" "elasticsearch" { |
| 68 | + name = "${lower(var.env_name)}-${lower(var.verbose_name)}-elasticsearch" |
| 69 | + vpc_id = "${var.vpc_id}" |
| 70 | + |
| 71 | + # Elasticsearch native transport protocol |
| 72 | + ingress { |
| 73 | + from_port = 9300 |
| 74 | + to_port = 9300 |
| 75 | + protocol = "tcp" |
| 76 | + cidr_blocks = ["${var.native_trusted_networks}"] |
| 77 | + } |
| 78 | + |
| 79 | + # Elasticsearch HTTP service |
| 80 | + ingress { |
| 81 | + from_port = 9200 |
| 82 | + to_port = 9200 |
| 83 | + protocol = "tcp" |
| 84 | + cidr_blocks = ["${var.http_trusted_networks}"] |
| 85 | + } |
| 86 | + |
| 87 | + # Elasticsearch native transport protocol |
| 88 | + egress { |
| 89 | + from_port = 9300 |
| 90 | + to_port = 9300 |
| 91 | + protocol = "tcp" |
| 92 | + cidr_blocks = ["${var.native_trusted_networks}"] |
| 93 | + } |
| 94 | + |
| 95 | + # Elasticsearch HTTP service |
| 96 | + egress { |
| 97 | + from_port = 9200 |
| 98 | + to_port = 9200 |
| 99 | + protocol = "tcp" |
| 100 | + cidr_blocks = ["${var.http_trusted_networks}"] |
| 101 | + } |
| 102 | + |
| 103 | + tags { |
| 104 | + Env = "${var.env_name}" |
| 105 | + Name = "${var.env_name}: ${var.verbose_name} Elasticsearch" |
| 106 | + } |
| 107 | +} |
0 commit comments