Skip to content

Commit

Permalink
Added Route53 DNS weighted blue green
Browse files Browse the repository at this point in the history
  • Loading branch information
bensojona committed Feb 12, 2016
1 parent 2c09919 commit a5bc5bd
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packer/config/consul/nodejs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"service": {
"name": "web",
"port": 8888,
"tags": ["nodejs"],
"tags": ["nodejs", "{{ deploy }}"],
"check": {
"id": "nodejs",
"name": "Running on port 8888",
Expand Down
13 changes: 10 additions & 3 deletions terraform/modules/aws/compute/compute.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ variable "haproxy_user_data" { }
variable "nodejs_blue_ami" { }
variable "nodejs_blue_node_count" { }
variable "nodejs_blue_instance_type" { }
variable "nodejs_blue_weight" { }
variable "nodejs_green_ami" { }
variable "nodejs_green_node_count" { }
variable "nodejs_green_instance_type" { }
variable "nodejs_green_weight" { }
variable "nodejs_user_data" { }

module "haproxy" {
Expand Down Expand Up @@ -71,12 +73,14 @@ module "nodejs" {
atlas_environment = "${var.atlas_environment}"
atlas_aws_global = "${var.atlas_aws_global}"
atlas_token = "${var.atlas_token}"
blue_weight = "${var.nodejs_blue_weight}"
blue_ami = "${var.nodejs_blue_ami}"
blue_nodes = "${var.nodejs_blue_node_count}"
blue_instance_type = "${var.nodejs_blue_instance_type}"
green_ami = "${var.nodejs_green_ami}"
green_nodes = "${var.nodejs_green_node_count}"
green_instance_type = "${var.nodejs_green_instance_type}"
green_weight = "${var.nodejs_green_weight}"
user_data = "${var.nodejs_user_data}"
sub_domain = "${var.sub_domain}"
route_zone_id = "${var.route_zone_id}"
Expand All @@ -88,6 +92,9 @@ output "haproxy_public_ips" { value = "${module.haproxy.public_ips}" }
output "haproxy_private_fqdn" { value = "${module.haproxy.private_fqdn}" }
output "haproxy_public_fqdn" { value = "${module.haproxy.public_fqdn}" }

output "nodejs_zone_id" { value = "${module.nodejs.zone_id}" }
output "nodejs_elb_dns" { value = "${module.nodejs.elb_dns}" }
output "nodejs_private_fqdn" { value = "${module.nodejs.private_fqdn}" }
output "nodejs_blue_elb_zone_id" { value = "${module.nodejs.blue_elb_zone_id}" }
output "nodejs_blue_private_fqdn" { value = "${module.nodejs.blue_private_fqdn}" }
output "nodejs_blue_elb_dns" { value = "${module.nodejs.blue_elb_dns}" }
output "nodejs_green_elb_zone_id" { value = "${module.nodejs.green_elb_zone_id}" }
output "nodejs_green_private_fqdn" { value = "${module.nodejs.green_private_fqdn}" }
output "nodejs_green_elb_dns" { value = "${module.nodejs.green_elb_dns}" }
82 changes: 70 additions & 12 deletions terraform/modules/aws/compute/nodejs/nodejs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ variable "atlas_token" { }
variable "blue_ami" { }
variable "blue_nodes" { }
variable "blue_instance_type" { }
variable "blue_weight" { }
variable "green_ami" { }
variable "green_nodes" { }
variable "green_instance_type" { }
variable "green_weight" { }
variable "user_data" { }
variable "sub_domain" { }
variable "route_zone_id" { }
Expand Down Expand Up @@ -76,8 +78,42 @@ EOF
}
}

resource "aws_elb" "nodejs" {
name = "${var.name}"
resource "aws_elb" "blue" {
name = "${var.name}-blue"
connection_draining = true
connection_draining_timeout = 400

subnets = ["${split(",", var.public_subnet_ids)}"]
security_groups = ["${aws_security_group.elb.id}"]

lifecycle { create_before_destroy = true }

listener {
lb_port = 80
lb_protocol = "http"
instance_port = 8888
instance_protocol = "http"
}

listener {
lb_port = 443
lb_protocol = "https"
instance_port = 8888
instance_protocol = "http"
ssl_certificate_id = "${aws_iam_server_certificate.nodejs.arn}"
}

health_check {
healthy_threshold = 2
unhealthy_threshold = 3
timeout = 10
interval = 15
target = "HTTP:8888/"
}
}

resource "aws_elb" "green" {
name = "${var.name}-green"
connection_draining = true
connection_draining_timeout = 400

Expand Down Expand Up @@ -130,6 +166,7 @@ resource "template_file" "blue_user_data" {
atlas_environment = "${var.atlas_environment}"
atlas_token = "${var.atlas_token}"
node_name = "${var.name}"
deploy = "blue"
site_ssl_cert = "${var.site_ssl_cert}"
vault_ssl_cert = "${var.vault_ssl_cert}"
vault_token = "${var.vault_token}"
Expand All @@ -150,6 +187,7 @@ resource "template_file" "green_user_data" {
atlas_environment = "${var.atlas_environment}"
atlas_token = "${var.atlas_token}"
node_name = "${var.name}"
deploy = "green"
site_ssl_cert = "${var.site_ssl_cert}"
vault_ssl_cert = "${var.vault_ssl_cert}"
vault_token = "${var.vault_token}"
Expand All @@ -169,29 +207,49 @@ module "deploy" {
key_name = "${var.key_name}"
azs = "${var.azs}"
private_subnet_ids = "${var.private_subnet_ids}"
elb_id = "${aws_elb.nodejs.id}"
blue_elb_id = "${aws_elb.blue.id}"
blue_ami = "${var.blue_ami}"
blue_nodes = "${var.blue_nodes}"
blue_instance_type = "${var.blue_instance_type}"
blue_user_data = "${template_file.blue_user_data.rendered}"
green_elb_id = "${aws_elb.green.id}"
green_ami = "${var.green_ami}"
green_nodes = "${var.green_nodes}"
green_instance_type = "${var.green_instance_type}"
green_user_data = "${template_file.green_user_data.rendered}"
}

resource "aws_route53_record" "nodejs" {
zone_id = "${var.route_zone_id}"
name = "nodejs.${var.sub_domain}"
type = "A"
resource "aws_route53_record" "blue" {
zone_id = "${var.route_zone_id}"
name = "nodejs.${var.sub_domain}"
type = "A"
weight = "${var.blue_weight}"
set_identifier = "blue"

alias {
name = "${aws_elb.blue.dns_name}"
zone_id = "${aws_elb.blue.zone_id}"
evaluate_target_health = true
}
}

resource "aws_route53_record" "green" {
zone_id = "${var.route_zone_id}"
name = "nodejs.${var.sub_domain}"
type = "A"
weight = "${var.green_weight}"
set_identifier = "green"

alias {
name = "${aws_elb.nodejs.dns_name}"
zone_id = "${aws_elb.nodejs.zone_id}"
name = "${aws_elb.green.dns_name}"
zone_id = "${aws_elb.green.zone_id}"
evaluate_target_health = true
}
}

output "zone_id" { value = "${aws_elb.nodejs.zone_id}" }
output "elb_dns" { value = "${aws_elb.nodejs.dns_name}" }
output "private_fqdn" { value = "${aws_route53_record.nodejs.fqdn}" }
output "blue_elb_zone_id" { value = "${aws_elb.blue.zone_id}" }
output "blue_private_fqdn" { value = "${aws_route53_record.blue.fqdn}" }
output "blue_elb_dns" { value = "${aws_elb.blue.dns_name}" }
output "green_elb_zone_id" { value = "${aws_elb.green.zone_id}" }
output "green_private_fqdn" { value = "${aws_route53_record.green.fqdn}" }
output "green_elb_dns" { value = "${aws_elb.green.dns_name}" }
7 changes: 4 additions & 3 deletions terraform/modules/aws/util/deploy/deploy.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ variable "vpc_cidr" { }
variable "key_name" { }
variable "azs" { }
variable "private_subnet_ids" { }
variable "elb_id" { }
variable "blue_elb_id" { }
variable "blue_ami" { }
variable "blue_nodes" { }
variable "blue_instance_type" { }
variable "blue_user_data" { }
variable "green_elb_id" { }
variable "green_ami" { }
variable "green_nodes" { }
variable "green_instance_type" { }
Expand Down Expand Up @@ -60,7 +61,7 @@ resource "aws_autoscaling_group" "blue" {
wait_for_elb_capacity = "${var.blue_nodes}"
availability_zones = ["${split(",", var.azs)}"]
vpc_zone_identifier = ["${split(",", var.private_subnet_ids)}"]
load_balancers = ["${var.elb_id}"]
load_balancers = ["${var.blue_elb_id}"]

lifecycle { create_before_destroy = true }

Expand Down Expand Up @@ -91,7 +92,7 @@ resource "aws_autoscaling_group" "green" {
wait_for_elb_capacity = "${var.green_nodes}"
availability_zones = ["${split(",", var.azs)}"]
vpc_zone_identifier = ["${split(",", var.private_subnet_ids)}"]
load_balancers = ["${var.elb_id}"]
load_balancers = ["${var.green_elb_id}"]

lifecycle { create_before_destroy = true }

Expand Down
1 change: 1 addition & 0 deletions terraform/modules/templates/nodejs.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ sed -i -- "s/{{ atlas_environment }}/${atlas_environment}/g" /etc/consul.d/base.
sed -i -- "s/{{ atlas_token }}/${atlas_token}/g" /etc/consul.d/base.json
sed -i -- "s/{{ datacenter }}/${atlas_environment}/g" /etc/consul.d/base.json
sed -i -- "s/{{ node_name }}/$NAME/g" /etc/consul.d/base.json
sed -i -- "s/{{ deploy }}/${deploy}/g" /etc/consul.d/nodejs.json

service consul restart

Expand Down
2 changes: 2 additions & 0 deletions terraform/providers/aws/us_east_1_prod/terraform.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ haproxy_artifacts = "latest" # Comma separated list of artifact version numb

nodejs_blue_node_count = "2"
nodejs_blue_instance_type = "t2.micro"
nodejs_blue_weight = "100"
nodejs_green_node_count = "0"
nodejs_green_instance_type = "t2.micro"
nodejs_green_weight = "0"
nodejs_artifact_name = "aws-us-east-1-ubuntu-nodejs"
nodejs_artifacts = "latest,latest" # Comma separated "blue,green" artifact version numbers, list length must be 2
19 changes: 13 additions & 6 deletions terraform/providers/aws/us_east_1_prod/us_east_1_prod.tf
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ variable "haproxy_instance_type" { }
variable "haproxy_artifact_name" { }
variable "haproxy_artifacts" { }

variable "nodejs_blue_node_count" { }
variable "nodejs_blue_instance_type" { }
variable "nodejs_blue_node_count" { }
variable "nodejs_blue_instance_type" { }
variable "nodejs_blue_weight" { }
variable "nodejs_green_node_count" { }
variable "nodejs_green_instance_type" { }
variable "nodejs_artifact_name" { }
variable "nodejs_artifacts" { }
variable "nodejs_green_weight" { }
variable "nodejs_artifact_name" { }
variable "nodejs_artifacts" { }

provider "aws" {
region = "${var.region}"
Expand Down Expand Up @@ -210,9 +212,11 @@ module "compute" {
nodejs_blue_ami = "${element(split(",", module.artifact_nodejs.amis), 0)}"
nodejs_blue_node_count = "${var.nodejs_blue_node_count}"
nodejs_blue_instance_type = "${var.nodejs_blue_instance_type}"
nodejs_blue_weight = "${var.nodejs_blue_weight}"
nodejs_green_ami = "${element(split(",", module.artifact_nodejs.amis), 1)}"
nodejs_green_node_count = "${var.nodejs_green_node_count}"
nodejs_green_instance_type = "${var.nodejs_green_instance_type}"
nodejs_green_weight = "${var.nodejs_green_weight}"
nodejs_user_data = "${module.templates.ubuntu_nodejs_user_data}"
}

Expand All @@ -238,8 +242,11 @@ Visit the static website hosted on S3:
${module.website.endpoint}

Visit the Node.js website:
Node.js: ${module.compute.nodejs_private_fqdn}
${module.compute.nodejs_elb_dns}
Node.js (blue): ${module.compute.nodejs_blue_private_fqdn}
${module.compute.nodejs_blue_elb_dns}

Node.js (green): ${module.compute.nodejs_green_private_fqdn}
${module.compute.nodejs_green_elb_dns}

HAProxy: ${module.compute.haproxy_public_fqdn}
${join("\n ", formatlist("http://%s/", split(",", module.compute.haproxy_public_ips)))}
Expand Down
2 changes: 2 additions & 0 deletions terraform/providers/aws/us_east_1_staging/terraform.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ haproxy_artifacts = "latest" # Comma separated list of artifact version numb

nodejs_blue_node_count = "2"
nodejs_blue_instance_type = "t2.micro"
nodejs_blue_weight = "100"
nodejs_green_node_count = "0"
nodejs_green_instance_type = "t2.micro"
nodejs_green_weight = "0"
nodejs_artifact_name = "aws-us-east-1-ubuntu-nodejs"
nodejs_artifacts = "latest,latest" # Comma separated "blue,green" artifact version numbers, list length must be 2
21 changes: 15 additions & 6 deletions terraform/providers/aws/us_east_1_staging/us_east_1_staging.tf
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ variable "haproxy_instance_type" { }
variable "haproxy_artifact_name" { }
variable "haproxy_artifacts" { }

variable "nodejs_blue_node_count" { }
variable "nodejs_blue_instance_type" { }
variable "nodejs_blue_node_count" { }
variable "nodejs_blue_instance_type" { }
variable "nodejs_blue_weight" { }
variable "nodejs_green_node_count" { }
variable "nodejs_green_instance_type" { }
variable "nodejs_artifact_name" { }
variable "nodejs_artifacts" { }
variable "nodejs_green_weight" { }
variable "nodejs_artifact_name" { }
variable "nodejs_artifacts" { }

provider "aws" {
region = "${var.region}"
Expand All @@ -72,6 +74,8 @@ resource "terraform_remote_state" "aws_global" {
config {
name = "${var.atlas_username}/${var.atlas_aws_global}"
}

lifecycle { create_before_destroy = true }
}

module "network" {
Expand Down Expand Up @@ -208,9 +212,11 @@ module "compute" {
nodejs_blue_ami = "${element(split(",", module.artifact_nodejs.amis), 0)}"
nodejs_blue_node_count = "${var.nodejs_blue_node_count}"
nodejs_blue_instance_type = "${var.nodejs_blue_instance_type}"
nodejs_blue_weight = "${var.nodejs_blue_weight}"
nodejs_green_ami = "${element(split(",", module.artifact_nodejs.amis), 1)}"
nodejs_green_node_count = "${var.nodejs_green_node_count}"
nodejs_green_instance_type = "${var.nodejs_green_instance_type}"
nodejs_green_weight = "${var.nodejs_green_weight}"
nodejs_user_data = "${module.templates.ubuntu_nodejs_user_data}"
}

Expand All @@ -236,8 +242,11 @@ Visit the static website hosted on S3:
${module.website.endpoint}

Visit the Node.js website:
Node.js: ${module.compute.nodejs_private_fqdn}
${module.compute.nodejs_elb_dns}
Node.js (blue): ${module.compute.nodejs_blue_private_fqdn}
${module.compute.nodejs_blue_elb_dns}

Node.js (green): ${module.compute.nodejs_green_private_fqdn}
${module.compute.nodejs_green_elb_dns}

HAProxy: ${module.compute.haproxy_public_fqdn}
${join("\n ", formatlist("http://%s/", split(",", module.compute.haproxy_public_ips)))}
Expand Down

0 comments on commit a5bc5bd

Please sign in to comment.