/terraform-bluegreen

Terraform module to setup blue / green deployments

Primary LanguageHCLMIT LicenseMIT

terraform-bluegreen

Terraform module to setup blue / green deployments

blue-green

Inputs

Name Description Type Default Required
associate_public_ip_address Associate a public ip address with an instance in a VPC bool false no
blue_ami The EC2 image ID to launch in the Blue autoscaling group string n/a yes
blue_desired_capacity The number of Amazon EC2 instances that should be running in the blue autoscaling roup number n/a yes
blue_disk_volume_size The size of the EBS volume in GB for the Blue instances number 8 no
blue_disk_volume_type The EBS volume type for the Blue instances string "gp2" no
blue_instance_type The Blue instance type to launch string n/a yes
blue_max_size The maximum size of the blue autoscaling group number n/a yes
blue_min_size The minimum size of the blue autoscaling group number n/a yes
blue_user_data The user data to provide when launching the Blue instances string "# Hello World" no
green_ami The EC2 image ID to launch in the Green autoscaling group string n/a yes
green_desired_capacity The number of Amazon EC2 instances that should be running in the green autoscaling roup number n/a yes
green_disk_volume_size The size of the EBS volume in GB for the Green instances number 8 no
green_disk_volume_type The EBS volume type for the Green instances string "gp2" no
green_instance_type The Green instance type to launch string n/a yes
green_max_size The maximum size of the green autoscaling group number n/a yes
green_min_size The minimum size of the green autoscaling group number n/a yes
green_user_data The user data to provide when launching the Green instances string "# Hello World" no
health_check_grace_period Time (in seconds) after instance comes into service before checking health number 300 no
health_check_type The health check type to apply to the Autoscaling groups. string "ELB" no
iam_instance_profile The IAM instance profile to associate with launched instances string null no
initial_lifecycle_hooks One or more Lifecycle Hooks to attach to the autoscaling group before instances are launched. The syntax is exactly the same as the separate aws_autoscaling_lifecycle_hook resource, without the autoscaling_group_name attribute list(map(string)) [] no
key_name The key name that should be used for the instance string null no
loadbalancers A list of load balancer names to add to the autoscaling groups list(string) [] no
name Name of the Auto Scaling Groups string n/a yes
security_groups A list of associated security group IDS list(string) [] no
spot_price Spot price you want to pay for your instances. By default this is empty and we will use on-demand instances string "" no
subnets A list of subnet IDs to launch resources in list(string) [] no
tags List as a map of additional tags list(map(string)) [] no
target_group_arns A list of aws_alb_target_group ARNs, for use with Application Load Balancing list(string) [] no
termination_policies Order in termination policies to apply when choosing instances to terminate. list(string) [] no
wait_for_capacity_timeout A maximum duration that Terraform should wait for ASG instances to be healthy before timing out. Setting this to 0 causes Terraform to skip all Capacity Waiting behavior. string "10m" no

Outputs

Name Description
blue_asg_id Blue autoscaling group id
green_asg_id Green autoscaling group id
nonbinding_blue_asg_name This is the raw blue autoscaling group name, without creating a dependency with the actual autoscaling group resource
nonbinding_green_asg_name This is the raw green autoscaling group name, without creating a dependency with the actual autoscaling group resource

Example

module "bluegreen" {
  source                 = "github.com/skyscrapers/terraform-bluegreen//blue-green"
  name                   = "app-${terraform.workspace}"
  blue_ami               = "ami-blabla"
  blue_instance_type     = "t3.micro"
  blue_max_size          = 5
  blue_min_size          = 2
  blue_desired_capacity  = 2
  green_ami              = "ami-blabla"
  green_instance_type    = "t3.micro"
  green_max_size         = 0
  green_min_size         = 0
  green_desired_capacity = 0
  loadbalancers          = ["myloadbalancers"]
  security_groups        = ["mysecuritygroups"]
}

scaling

Terraform module to setup alarms and autoscaling triggers for autoscaling

Inputs

Name Description Type Default Required
adjustment_down The number of instances to remove when the alarm is triggered (the value has to be negative) number -1 no
adjustment_type What typ of adjustment needs to happen string "ChangeInCapacity" no
adjustment_up The number of instances to add when the alarm is triggered number 1 no
autoscaling_group_names The names of the Auto Scaling Groups this config needs to be applied to list(string) n/a yes
cooldown_down The amount of time, in seconds, after a scaling activity completes and before the next scaling activity can start. number 600 no
cooldown_up The amount of time, in seconds, after a scaling activity completes and before the next scaling activity can start. number 300 no
datapoints_to_alarm_down The number of datapoints that must be breaching to trigger the scale DOWN alarm number n/a yes
datapoints_to_alarm_up The number of datapoints that must be breaching to trigger the scale UP alarm number n/a yes
dimension_name n/a string "AutoScalingGroupName" no
dimension_value n/a string n/a yes
evaluation_periods The number of samples to evaluate number 4 no
metric_name The metric the scaling is based upon string "CPUUtilization" no
name Name for the created resources string n/a yes
namespace The namespace of the cloudwatch metric string "AWS/EC2" no
num_asg The number of autoscaling groups passed number 2 no
period_down The period in seconds over which the selected metric statistic is applied. number 120 no
period_up The period in seconds over which the selected metric statistic is applied. number 60 no
policy_type The policy type, either SimpleScaling or StepScaling string "SimpleScaling" no
statistic The statistic to apply to the alarm's associated metric. Either of the following is supported: string "Average" no
threshold_down The metric value to scale down number 30 no
threshold_up The metric value to scale up number 80 no

Example

module "scaling" {
  source                  = "github.com/skyscrapers/terraform-bluegreen//scaling"
  name                    = "app-${terraform.workspace}"
  autoscaling_group_names = ["my_asg_name1","my_asg_name2"]
}

Blue-green deployments

The bluegreen.py script performs a bluegreen deployment of the selected terraform stack. It only works with Python 2.7. The blue-green deployment script expects certain inputs and outputs in the Terraform project you want to deploy in a blue-green fashion.

Required outputs

output "blue_asg_id" {
  value = "${module.<blue-green-module-name>.blue_asg_id}"
}

output "green_asg_id" {
  value = "${module.<blue-green-module-name>.green_asg_id}"
}

Required variables

variable "blue_max_size" {
  description = "max instances blue"
}

variable "blue_min_size" {
  description = "min instances blue"
}

variable "blue_desired_capacity" {
  description = "desired instances blue"
}

variable "green_max_size" {
  description = "max instances green"
}

variable "green_min_size" {
  description = "min instances green"
}

variable "green_desired_capacity" {
  description = "desired instances green"
}

variable "blue_ami" {
  description = "blue ami"
}

variable "green_ami" {
  description = "green ami"
}

These input variables need to be assigned to your module. Make sure you do a terraform refresh to be sure that the terraform outputs are saved in the Terraform state file.

Usage

First you need to install the requirements:

pip install -r requirements.txt

Run ./bluegreen.py --help to see the available options.

Example:

./bluegreen.py -f stacks/test/application -a ami-xxxx -c apply -t 500