diff --git a/terraform/aws/README.md b/terraform/aws/README.md new file mode 100644 index 0000000000..441afe34c0 --- /dev/null +++ b/terraform/aws/README.md @@ -0,0 +1,17 @@ +## Running the aws templates to set up a consul cluster + +The platform variable defines the target OS, default is ubuntu, rhel6 is an option + +For AWS provider, set up your AWS environment as outlined in https://www.terraform.io/docs/providers/aws/index.html + +To set up ubuntu based, run like below, replace key_name and key_path with actual values + +terraform apply -var 'key_name=consul' -var 'key_path=/Users/xyz/consul.pem' + +or + +terraform apply -var 'key_name=consul' -var 'key_path=/Users/xyz/consul.pem' -var 'platform=ubuntu' + +To run rhel6, run like below + +terraform apply -var 'key_name=consul' -var 'key_path=/Users/xyz/consul.pem' -var 'platform=rhel6' \ No newline at end of file diff --git a/terraform/aws/consul.tf b/terraform/aws/consul.tf index 9a2c5fcf7e..d1c57d9bb8 100644 --- a/terraform/aws/consul.tf +++ b/terraform/aws/consul.tf @@ -1,12 +1,12 @@ resource "aws_instance" "server" { - ami = "${lookup(var.ami, var.region)}" + ami = "${lookup(var.ami, concat(var.region, "-", var.platform))}" instance_type = "${var.instance_type}" key_name = "${var.key_name}" count = "${var.servers}" security_groups = ["${aws_security_group.consul.name}"] connection { - user = "ubuntu" + user = "${lookup(var.user, var.platform)}" key_file = "${var.key_path}" } @@ -16,12 +16,12 @@ resource "aws_instance" "server" { } provisioner "file" { - source = "${path.module}/scripts/upstart.conf" + source = "${path.module}/scripts/${var.platform}/upstart.conf" destination = "/tmp/upstart.conf" } provisioner "file" { - source = "${path.module}/scripts/upstart-join.conf" + source = "${path.module}/scripts/${var.platform}/upstart-join.conf" destination = "/tmp/upstart-join.conf" } @@ -34,9 +34,9 @@ resource "aws_instance" "server" { provisioner "remote-exec" { scripts = [ - "${path.module}/scripts/install.sh", - "${path.module}/scripts/server.sh", - "${path.module}/scripts/service.sh", + "${path.module}/scripts/${var.platform}/install.sh", + "${path.module}/scripts/${var.platform}/server.sh", + "${path.module}/scripts/${var.platform}/service.sh", ] } } diff --git a/terraform/aws/scripts/rhel6/install.sh b/terraform/aws/scripts/rhel6/install.sh new file mode 100644 index 0000000000..da40412785 --- /dev/null +++ b/terraform/aws/scripts/rhel6/install.sh @@ -0,0 +1,34 @@ +#!/bin/bash +set -e + +# Read the address to join from the file we provisioned +JOIN_ADDRS=$(cat /tmp/consul-server-addr | tr -d '\n') + +echo "Installing dependencies..." +sudo yum update -y +sudo yum install -y unzip wget + +echo "Fetching Consul..." +cd /tmp +wget https://dl.bintray.com/mitchellh/consul/0.5.2_linux_amd64.zip -O consul.zip + +echo "Installing Consul..." +unzip consul.zip >/dev/null +sudo chmod +x consul +sudo mv consul /usr/local/bin/consul +sudo mkdir -p /etc/consul.d +sudo mkdir -p /mnt/consul +sudo mkdir -p /etc/service + +# Setup the join address +cat >/tmp/consul-join << EOF +export CONSUL_JOIN="${JOIN_ADDRS}" +EOF +sudo mv /tmp/consul-join /etc/service/consul-join +chmod 0644 /etc/service/consul-join + +echo "Installing Upstart service..." +sudo chown root:root /tmp/upstart.conf +sudo chown root:root /tmp/upstart-join.conf +sudo mv /tmp/upstart.conf /etc/init/consul.conf +sudo mv /tmp/upstart-join.conf /etc/init/consul-join.conf diff --git a/terraform/aws/scripts/rhel6/server.sh b/terraform/aws/scripts/rhel6/server.sh new file mode 100755 index 0000000000..80057cbf81 --- /dev/null +++ b/terraform/aws/scripts/rhel6/server.sh @@ -0,0 +1,15 @@ +#!/bin/bash +set -e + +# Read from the file we created +SERVER_COUNT=$(cat /tmp/consul-server-count | tr -d '\n') + +# Write the flags to a temporary file +cat >/tmp/consul_flags << EOF +export CONSUL_FLAGS="-server -bootstrap-expect=${SERVER_COUNT} -data-dir=/mnt/consul" +EOF + +# Write it to the full service file +sudo mv /tmp/consul_flags /etc/service/consul +sudo chown root:root /etc/service/consul +sudo chmod 0644 /etc/service/consul diff --git a/terraform/aws/scripts/service.sh b/terraform/aws/scripts/rhel6/service.sh similarity index 100% rename from terraform/aws/scripts/service.sh rename to terraform/aws/scripts/rhel6/service.sh diff --git a/terraform/aws/scripts/upstart-join.conf b/terraform/aws/scripts/rhel6/upstart-join.conf similarity index 100% rename from terraform/aws/scripts/upstart-join.conf rename to terraform/aws/scripts/rhel6/upstart-join.conf diff --git a/terraform/aws/scripts/rhel6/upstart.conf b/terraform/aws/scripts/rhel6/upstart.conf new file mode 100644 index 0000000000..7c050dc011 --- /dev/null +++ b/terraform/aws/scripts/rhel6/upstart.conf @@ -0,0 +1,26 @@ +description "Consul agent" + +start on runlevel [2345] +stop on runlevel [!2345] + +respawn +# This is to avoid Upstart re-spawning the process upon `consul leave` +normal exit 0 INT + +script + if [ -f "/etc/service/consul" ]; then + . /etc/service/consul + fi + + # Make sure to use all our CPUs, because Consul can block a scheduler thread + export GOMAXPROCS=`nproc` + + # Get the public IP + BIND=`ifconfig eth0 | grep "inet addr" | awk '{ print substr($2,6) }'` + + exec /usr/local/bin/consul agent \ + -config-dir="/etc/consul.d" \ + -bind=$BIND \ + ${CONSUL_FLAGS} \ + >>/var/log/consul.log 2>&1 +end script diff --git a/terraform/aws/scripts/install.sh b/terraform/aws/scripts/ubuntu/install.sh similarity index 100% rename from terraform/aws/scripts/install.sh rename to terraform/aws/scripts/ubuntu/install.sh diff --git a/terraform/aws/scripts/server.sh b/terraform/aws/scripts/ubuntu/server.sh similarity index 100% rename from terraform/aws/scripts/server.sh rename to terraform/aws/scripts/ubuntu/server.sh diff --git a/terraform/aws/scripts/ubuntu/service.sh b/terraform/aws/scripts/ubuntu/service.sh new file mode 100755 index 0000000000..7c186e97ac --- /dev/null +++ b/terraform/aws/scripts/ubuntu/service.sh @@ -0,0 +1,5 @@ +#!/bin/bash +set -e + +echo "Starting Consul..." +sudo start consul diff --git a/terraform/aws/scripts/ubuntu/upstart-join.conf b/terraform/aws/scripts/ubuntu/upstart-join.conf new file mode 100644 index 0000000000..16343d8759 --- /dev/null +++ b/terraform/aws/scripts/ubuntu/upstart-join.conf @@ -0,0 +1,25 @@ +description "Join the consul cluster" + +start on started consul +stop on stopped consul + +task + +script + if [ -f "/etc/service/consul-join" ]; then + . /etc/service/consul-join + fi + + # Keep trying to join until it succeeds + set +e + while :; do + logger -t "consul-join" "Attempting join: ${CONSUL_JOIN}" + /usr/local/bin/consul join \ + ${CONSUL_JOIN} \ + >>/var/log/consul-join.log 2>&1 + [ $? -eq 0 ] && break + sleep 5 + done + + logger -t "consul-join" "Join success!" +end script diff --git a/terraform/aws/scripts/upstart.conf b/terraform/aws/scripts/ubuntu/upstart.conf similarity index 100% rename from terraform/aws/scripts/upstart.conf rename to terraform/aws/scripts/ubuntu/upstart.conf diff --git a/terraform/aws/variables.tf b/terraform/aws/variables.tf index 2d92711f64..94c0e310f7 100644 --- a/terraform/aws/variables.tf +++ b/terraform/aws/variables.tf @@ -1,7 +1,22 @@ -variable "ami" { +variable "platform" { + default = "ubuntu" + description = "The region of AWS, for AMI lookups." +} + +variable "user" { default = { - us-east-1 = "ami-3acc7a52" - us-west-2 = "ami-37501207" + ubuntu = "ubuntu" + rhel6 = "ec2-user" + } +} + +variable "ami" { + description = "AWS AMI Id, if you change, make sure it is compatible with insatnce type, not all AMIs allow all insatnce types " + default = { + us-east-1-ubuntu = "ami-83c525e8" + us-west-2-ubuntu = "ami-57e8d767" + us-east-1-rhel6 = "ami-b0fed2d8" + us-west-2-rhel6 = "ami-2faa861f" } } @@ -24,10 +39,16 @@ variable "servers" { } variable "instance_type" { - default = "m1.small" - description = "The instance type to launch." + default = "t2.micro" + description = "AWS Instance type, if you change, make sure it is compatible with AMI, not all AMIs allow all insatnce types " } + variable "tagName" { default = "consul" description = "Name tag for the servers" -} \ No newline at end of file +} + +variable "platform" { + default = "ubuntu" + description = "The OS Platform" +}