2018-03-31 18:01:21 +00:00
|
|
|
variable "name" {}
|
2017-06-25 18:10:14 +00:00
|
|
|
variable "region" {}
|
|
|
|
variable "ami" {}
|
|
|
|
variable "instance_type" {}
|
|
|
|
variable "key_name" {}
|
|
|
|
variable "server_count" {}
|
|
|
|
variable "client_count" {}
|
2017-11-15 19:38:50 +00:00
|
|
|
variable "retry_join" {}
|
2018-03-20 17:46:32 +00:00
|
|
|
variable "nomad_binary" {}
|
2017-05-15 18:56:41 +00:00
|
|
|
|
|
|
|
data "aws_vpc" "default" {
|
|
|
|
default = true
|
|
|
|
}
|
|
|
|
|
2018-03-31 18:01:21 +00:00
|
|
|
resource "aws_security_group" "primary" {
|
|
|
|
name = "${var.name}"
|
2017-06-25 18:10:14 +00:00
|
|
|
vpc_id = "${data.aws_vpc.default.id}"
|
|
|
|
|
2017-05-15 18:56:41 +00:00
|
|
|
ingress {
|
|
|
|
from_port = 22
|
|
|
|
to_port = 22
|
|
|
|
protocol = "tcp"
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
|
|
}
|
|
|
|
|
2017-11-15 19:38:50 +00:00
|
|
|
# Nomad
|
|
|
|
ingress {
|
|
|
|
from_port = 4646
|
|
|
|
to_port = 4646
|
|
|
|
protocol = "tcp"
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
|
|
}
|
|
|
|
|
|
|
|
# Consul
|
2017-08-04 18:00:05 +00:00
|
|
|
ingress {
|
|
|
|
from_port = 8500
|
|
|
|
to_port = 8500
|
|
|
|
protocol = "tcp"
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
|
|
}
|
|
|
|
|
2017-06-25 18:10:14 +00:00
|
|
|
# HDFS NameNode UI
|
2017-06-24 23:24:24 +00:00
|
|
|
ingress {
|
|
|
|
from_port = 50070
|
|
|
|
to_port = 50070
|
|
|
|
protocol = "tcp"
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
|
|
}
|
|
|
|
|
2017-06-25 18:10:14 +00:00
|
|
|
# HDFS DataNode UI
|
2017-06-24 23:24:24 +00:00
|
|
|
ingress {
|
|
|
|
from_port = 50075
|
|
|
|
to_port = 50075
|
|
|
|
protocol = "tcp"
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
|
|
}
|
|
|
|
|
2017-06-25 18:10:14 +00:00
|
|
|
# Spark history server UI
|
2017-06-24 23:24:24 +00:00
|
|
|
ingress {
|
|
|
|
from_port = 18080
|
|
|
|
to_port = 18080
|
|
|
|
protocol = "tcp"
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
|
|
}
|
|
|
|
|
2017-05-15 18:56:41 +00:00
|
|
|
ingress {
|
2017-06-25 18:10:14 +00:00
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
protocol = "-1"
|
|
|
|
self = true
|
2017-05-15 18:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
egress {
|
2017-06-25 18:10:14 +00:00
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
protocol = "-1"
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
2017-05-15 18:56:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-15 19:38:50 +00:00
|
|
|
data "template_file" "user_data_server" {
|
2017-05-15 18:56:41 +00:00
|
|
|
template = "${file("${path.root}/user-data-server.sh")}"
|
|
|
|
|
|
|
|
vars {
|
2017-11-15 19:38:50 +00:00
|
|
|
server_count = "${var.server_count}"
|
|
|
|
region = "${var.region}"
|
|
|
|
retry_join = "${var.retry_join}"
|
2018-03-20 17:46:32 +00:00
|
|
|
nomad_binary = "${var.nomad_binary}"
|
2017-05-15 18:56:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data "template_file" "user_data_client" {
|
|
|
|
template = "${file("${path.root}/user-data-client.sh")}"
|
|
|
|
|
|
|
|
vars {
|
2017-11-15 19:38:50 +00:00
|
|
|
region = "${var.region}"
|
|
|
|
retry_join = "${var.retry_join}"
|
2018-03-20 17:46:32 +00:00
|
|
|
nomad_binary = "${var.nomad_binary}"
|
2017-05-15 18:56:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-15 19:38:50 +00:00
|
|
|
resource "aws_instance" "server" {
|
2017-06-25 18:10:14 +00:00
|
|
|
ami = "${var.ami}"
|
|
|
|
instance_type = "${var.instance_type}"
|
|
|
|
key_name = "${var.key_name}"
|
2018-03-31 18:01:21 +00:00
|
|
|
vpc_security_group_ids = ["${aws_security_group.primary.id}"]
|
2017-06-25 18:10:14 +00:00
|
|
|
count = "${var.server_count}"
|
|
|
|
|
|
|
|
#Instance tags
|
|
|
|
tags {
|
2018-03-31 18:01:21 +00:00
|
|
|
Name = "${var.name}-server-${count.index}"
|
2017-11-15 19:38:50 +00:00
|
|
|
ConsulAutoJoin = "auto-join"
|
2017-06-25 18:10:14 +00:00
|
|
|
}
|
2017-05-15 18:56:41 +00:00
|
|
|
|
2017-11-15 19:38:50 +00:00
|
|
|
user_data = "${data.template_file.user_data_server.rendered}"
|
2017-06-25 18:10:14 +00:00
|
|
|
iam_instance_profile = "${aws_iam_instance_profile.instance_profile.name}"
|
2017-05-15 18:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_instance" "client" {
|
2017-06-25 18:10:14 +00:00
|
|
|
ami = "${var.ami}"
|
|
|
|
instance_type = "${var.instance_type}"
|
|
|
|
key_name = "${var.key_name}"
|
2018-03-31 18:01:21 +00:00
|
|
|
vpc_security_group_ids = ["${aws_security_group.primary.id}"]
|
2017-06-25 18:10:14 +00:00
|
|
|
count = "${var.client_count}"
|
2017-11-15 19:38:50 +00:00
|
|
|
depends_on = ["aws_instance.server"]
|
2017-06-25 18:10:14 +00:00
|
|
|
|
|
|
|
#Instance tags
|
|
|
|
tags {
|
2018-03-31 18:01:21 +00:00
|
|
|
Name = "${var.name}-client-${count.index}"
|
2017-11-15 19:38:50 +00:00
|
|
|
ConsulAutoJoin = "auto-join"
|
2017-06-25 18:10:14 +00:00
|
|
|
}
|
2017-05-15 18:56:41 +00:00
|
|
|
|
2018-03-20 17:46:32 +00:00
|
|
|
ebs_block_device = {
|
|
|
|
device_name = "/dev/xvdd"
|
|
|
|
volume_type = "gp2"
|
|
|
|
volume_size = "50"
|
|
|
|
delete_on_termination = "true"
|
|
|
|
}
|
|
|
|
|
2017-06-25 18:10:14 +00:00
|
|
|
user_data = "${data.template_file.user_data_client.rendered}"
|
|
|
|
iam_instance_profile = "${aws_iam_instance_profile.instance_profile.name}"
|
2017-05-15 18:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_iam_instance_profile" "instance_profile" {
|
2018-03-31 18:01:21 +00:00
|
|
|
name_prefix = "${var.name}"
|
2017-06-25 18:10:14 +00:00
|
|
|
role = "${aws_iam_role.instance_role.name}"
|
2017-05-15 18:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_iam_role" "instance_role" {
|
2018-03-31 18:01:21 +00:00
|
|
|
name_prefix = "${var.name}"
|
2017-05-15 18:56:41 +00:00
|
|
|
assume_role_policy = "${data.aws_iam_policy_document.instance_role.json}"
|
|
|
|
}
|
|
|
|
|
|
|
|
data "aws_iam_policy_document" "instance_role" {
|
|
|
|
statement {
|
|
|
|
effect = "Allow"
|
|
|
|
actions = ["sts:AssumeRole"]
|
|
|
|
|
|
|
|
principals {
|
|
|
|
type = "Service"
|
|
|
|
identifiers = ["ec2.amazonaws.com"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_iam_role_policy" "auto_discover_cluster" {
|
|
|
|
name = "auto-discover-cluster"
|
|
|
|
role = "${aws_iam_role.instance_role.id}"
|
|
|
|
policy = "${data.aws_iam_policy_document.auto_discover_cluster.json}"
|
|
|
|
}
|
|
|
|
|
|
|
|
data "aws_iam_policy_document" "auto_discover_cluster" {
|
|
|
|
statement {
|
|
|
|
effect = "Allow"
|
|
|
|
|
|
|
|
actions = [
|
|
|
|
"ec2:DescribeInstances",
|
|
|
|
"ec2:DescribeTags",
|
|
|
|
"autoscaling:DescribeAutoScalingGroups",
|
|
|
|
]
|
|
|
|
|
|
|
|
resources = ["*"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-15 19:38:50 +00:00
|
|
|
output "server_public_ips" {
|
|
|
|
value = ["${aws_instance.server.*.public_ip}"]
|
2017-06-25 18:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
output "client_public_ips" {
|
|
|
|
value = ["${aws_instance.client.*.public_ip}"]
|
|
|
|
}
|