github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/terraform/aws/modules/hashistack/hashistack.tf (about)

     1  variable "region" {}
     2  variable "ami" {}
     3  variable "instance_type" {}
     4  variable "key_name" {}
     5  variable "server_count" {}
     6  variable "client_count" {}
     7  variable "cluster_tag_value" {}
     8  
     9  data "aws_vpc" "default" {
    10    default = true
    11  }
    12  
    13  resource "aws_security_group" "primary" {
    14    name   = "hashistack"
    15    vpc_id = "${data.aws_vpc.default.id}"
    16  
    17    ingress {
    18      from_port   = 22
    19      to_port     = 22
    20      protocol    = "tcp"
    21      cidr_blocks = ["0.0.0.0/0"]
    22    }
    23  
    24    # HDFS NameNode UI
    25    ingress {
    26      from_port   = 50070
    27      to_port     = 50070
    28      protocol    = "tcp"
    29      cidr_blocks = ["0.0.0.0/0"]
    30    }
    31  
    32    # HDFS DataNode UI
    33    ingress {
    34      from_port   = 50075
    35      to_port     = 50075
    36      protocol    = "tcp"
    37      cidr_blocks = ["0.0.0.0/0"]
    38    }
    39  
    40    # Spark history server UI
    41    ingress {
    42      from_port   = 18080
    43      to_port     = 18080
    44      protocol    = "tcp"
    45      cidr_blocks = ["0.0.0.0/0"]
    46    }
    47  
    48    ingress {
    49      from_port = 0
    50      to_port   = 0
    51      protocol  = "-1"
    52      self      = true
    53    }
    54  
    55    egress {
    56      from_port   = 0
    57      to_port     = 0
    58      protocol    = "-1"
    59      cidr_blocks = ["0.0.0.0/0"]
    60    }
    61  }
    62  
    63  data "template_file" "user_data_server_primary" {
    64    template = "${file("${path.root}/user-data-server.sh")}"
    65  
    66    vars {
    67      server_count      = "${var.server_count}"
    68      region            = "${var.region}"
    69      cluster_tag_value = "${var.cluster_tag_value}"
    70    }
    71  }
    72  
    73  data "template_file" "user_data_client" {
    74    template = "${file("${path.root}/user-data-client.sh")}"
    75  
    76    vars {
    77      region            = "${var.region}"
    78      cluster_tag_value = "${var.cluster_tag_value}"
    79    }
    80  }
    81  
    82  resource "aws_instance" "primary" {
    83    ami                    = "${var.ami}"
    84    instance_type          = "${var.instance_type}"
    85    key_name               = "${var.key_name}"
    86    vpc_security_group_ids = ["${aws_security_group.primary.id}"]
    87    count                  = "${var.server_count}"
    88  
    89    #Instance tags
    90    tags {
    91      Name           = "hashistack-server-${count.index}"
    92      ConsulAutoJoin = "${var.cluster_tag_value}"
    93    }
    94  
    95    user_data            = "${data.template_file.user_data_server_primary.rendered}"
    96    iam_instance_profile = "${aws_iam_instance_profile.instance_profile.name}"
    97  }
    98  
    99  resource "aws_instance" "client" {
   100    ami                    = "${var.ami}"
   101    instance_type          = "${var.instance_type}"
   102    key_name               = "${var.key_name}"
   103    vpc_security_group_ids = ["${aws_security_group.primary.id}"]
   104    count                  = "${var.client_count}"
   105    depends_on             = ["aws_instance.primary"]
   106  
   107    #Instance tags
   108    tags {
   109      Name           = "hashistack-client-${count.index}"
   110      ConsulAutoJoin = "${var.cluster_tag_value}"
   111    }
   112  
   113    user_data            = "${data.template_file.user_data_client.rendered}"
   114    iam_instance_profile = "${aws_iam_instance_profile.instance_profile.name}"
   115  }
   116  
   117  resource "aws_iam_instance_profile" "instance_profile" {
   118    name_prefix = "hashistack"
   119    role        = "${aws_iam_role.instance_role.name}"
   120  }
   121  
   122  resource "aws_iam_role" "instance_role" {
   123    name_prefix        = "hashistack"
   124    assume_role_policy = "${data.aws_iam_policy_document.instance_role.json}"
   125  }
   126  
   127  data "aws_iam_policy_document" "instance_role" {
   128    statement {
   129      effect  = "Allow"
   130      actions = ["sts:AssumeRole"]
   131  
   132      principals {
   133        type        = "Service"
   134        identifiers = ["ec2.amazonaws.com"]
   135      }
   136    }
   137  }
   138  
   139  resource "aws_iam_role_policy" "auto_discover_cluster" {
   140    name   = "auto-discover-cluster"
   141    role   = "${aws_iam_role.instance_role.id}"
   142    policy = "${data.aws_iam_policy_document.auto_discover_cluster.json}"
   143  }
   144  
   145  data "aws_iam_policy_document" "auto_discover_cluster" {
   146    statement {
   147      effect = "Allow"
   148  
   149      actions = [
   150        "ec2:DescribeInstances",
   151        "ec2:DescribeTags",
   152        "autoscaling:DescribeAutoScalingGroups",
   153      ]
   154  
   155      resources = ["*"]
   156    }
   157  }
   158  
   159  output "primary_server_private_ips" {
   160    value = ["${aws_instance.primary.*.private_ip}"]
   161  }
   162  
   163  output "primary_server_public_ips" {
   164    value = ["${aws_instance.primary.*.public_ip}"]
   165  }
   166  
   167  output "client_private_ips" {
   168    value = ["${aws_instance.client.*.private_ip}"]
   169  }
   170  
   171  output "client_public_ips" {
   172    value = ["${aws_instance.client.*.public_ip}"]
   173  }