Skip to content

Instantly share code, notes, and snippets.

@stack72
Created January 12, 2016 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stack72/b1e8078e5cd8dc18552c to your computer and use it in GitHub Desktop.
Save stack72/b1e8078e5cd8dc18552c to your computer and use it in GitHub Desktop.
variable "vpc_id" {}
variable "ingress_cidr_blocks" {}
variable "key_name" {}
variable "ami" {}
variable "instance_type" {}
variable "private_subnets" {}
variable "public_subnets" {}
variable "availability_zones" {}
variable "cluster_name" {}
resource "aws_security_group" "consul_elb" {
name = "consul-ui-elb-sg"
description = "Security group for the Consul UI ELBs"
vpc_id = "${var.vpc_id}"
tags {
Name = "consul-ui-elb-sg"
}
# HTTP
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["${var.ingress_cidr_blocks}"]
}
# TCP All outbound traffic
egress {
from_port = 0
to_port = 65535
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "consul_server" {
name = "consul-server-sg"
description = "Security group for Consul Server instances"
vpc_id = "${var.vpc_id}"
tags {
Name = "consul-server-sg"
}
# SSH
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["${var.ingress_cidr_blocks}"]
}
# HTTP UI from ELB
ingress {
from_port = 8500
to_port = 8500
protocol = "tcp"
security_groups = ["${aws_security_group.consul_elb.id}"]
}
# DNS (TCP)
ingress {
from_port = 8600
to_port = 8600
protocol = "tcp"
cidr_blocks = ["${var.ingress_cidr_blocks}"]
}
# DNS (UDP)
ingress {
from_port = 8600
to_port = 8600
protocol = "udp"
cidr_blocks = ["${var.ingress_cidr_blocks}"]
}
# HTTP
ingress {
from_port = 8500
to_port = 8500
protocol = "tcp"
cidr_blocks = ["${var.ingress_cidr_blocks}"]
}
# Serf (TCP)
ingress {
from_port = 8301
to_port = 8302
protocol = "tcp"
cidr_blocks = ["${var.ingress_cidr_blocks}"]
}
# Serf (UDP)
ingress {
from_port = 8301
to_port = 8302
protocol = "udp"
cidr_blocks = ["${var.ingress_cidr_blocks}"]
}
# Server RPC
ingress {
from_port = 8300
to_port = 8300
protocol = "tcp"
cidr_blocks = ["${var.ingress_cidr_blocks}"]
}
# RPC
ingress {
from_port = 8400
to_port = 8400
protocol = "tcp"
cidr_blocks = ["${var.ingress_cidr_blocks}"]
}
# TCP All outbound traffic
egress {
from_port = 0
to_port = 65535
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# UDP All outbound traffic
egress {
from_port = 0
to_port = 65535
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_iam_role" "consul_server" {
name = "ConsulServer"
assume_role_policy = "${file("${path.module}/policies/assume-role-policy.json")}"
}
resource "aws_iam_role_policy" "consul_server" {
name = "ConsulServer"
role = "${aws_iam_role.consul_server.id}"
policy = "${file("${path.module}/policies/consul-server-policy.json")}"
}
resource "aws_iam_instance_profile" "consul_server" {
name = "ConsulServer"
roles = ["${aws_iam_role.consul_server.name}"]
}
resource "aws_launch_configuration" "consul_server" {
image_id = "${var.ami}"
instance_type = "${var.instance_type}"
security_groups = ["${aws_security_group.consul_server.id}"]
associate_public_ip_address = false
ebs_optimized = false
key_name = "${var.key_name}"
iam_instance_profile = "${aws_iam_instance_profile.consul_server.id}"
}
resource "aws_autoscaling_group" "consul_server" {
launch_configuration = "${aws_launch_configuration.consul_server.id}"
availability_zones = ["${split(",", var.availability_zones)}"]
vpc_zone_identifier = ["${split(",", var.private_subnets)}"]
load_balancers = ["${aws_elb.consul_ui.name}"]
name = "${var.cluster_name}"
max_size = 3
min_size = 3
desired_capacity = 3
default_cooldown = 30
force_delete = true
health_check_type = "ELB"
health_check_grace_period = 300
tag {
key = "Name"
value = "${format("%s-consul-server", var.cluster_name)}"
propagate_at_launch = true
}
}
resource "aws_elb" "consul_ui" {
name = "${format("%s-consul-ui", var.cluster_name)}"
subnets = ["${split(",", var.public_subnets)}"]
cross_zone_load_balancing = true
connection_draining = true
internal = false
security_groups = ["${aws_security_group.consul_elb.id}"]
listener {
instance_port = 8500
instance_protocol = "tcp"
lb_port = 80
lb_protocol = "tcp"
}
health_check {
healthy_threshold = 2
unhealthy_threshold = 5
interval = 60
timeout = 10
target = "HTTP:8500/v1/catalog/datacenters"
}
}
output "consul_ui_elb_dns" {
value = "${aws_elb.consul_ui.dns_name}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment