diff --git a/blue-green/main.tf b/blue-green/main.tf index f122406..f702a52 100644 --- a/blue-green/main.tf +++ b/blue-green/main.tf @@ -1,51 +1,51 @@ module "blue" { - source = "../single-stack" - color = "blue" - max_size = "${var.blue_max_size}" - min_size = "${var.blue_min_size}" + source = "../single-stack" + color = "blue" + max_size = "${var.blue_max_size}" + min_size = "${var.blue_min_size}" desired_capacity = "${var.blue_desired_capacity}" - ami = "${var.blue_ami}" + ami = "${var.blue_ami}" - loadbalancers = "${var.loadbalancers}" - project = "${var.project}" - name = "${var.name}" - environment = "${var.environment}" - instance_type = "${var.instance_type}" - key_name = "${var.key_name}" - security_groups = "${var.security_groups}" - iam_instance_profile = "${var.iam_instance_profile}" + loadbalancers = "${var.loadbalancers}" + project = "${var.project}" + name = "${var.name}" + environment = "${var.environment}" + instance_type = "${var.instance_type}" + key_name = "${var.key_name}" + security_groups = "${var.security_groups}" + iam_instance_profile = "${var.iam_instance_profile}" associate_public_ip_address = "${var.associate_public_ip_address}" - user_data = "${var.user_data}" - disk_volume_size = "${var.disk_volume_size}" - disk_volume_type = "${var.disk_volume_type}" - subnets = "${var.subnets}" - health_check_grace_period = "${var.health_check_grace_period}" - termination_policies = ["${var.termination_policies}"] - target_group_arns = "${var.target_group_arns}" + user_data = "${var.user_data}" + disk_volume_size = "${var.disk_volume_size}" + disk_volume_type = "${var.disk_volume_type}" + subnets = "${var.subnets}" + health_check_grace_period = "${var.health_check_grace_period}" + termination_policies = ["${var.termination_policies}"] + target_group_arns = "${var.target_group_arns}" } module "green" { - source = "../single-stack" - color = "green" - max_size = "${var.green_max_size}" - min_size = "${var.green_min_size}" + source = "../single-stack" + color = "green" + max_size = "${var.green_max_size}" + min_size = "${var.green_min_size}" desired_capacity = "${var.green_desired_capacity}" - ami = "${var.green_ami}" + ami = "${var.green_ami}" - loadbalancers = "${var.loadbalancers}" - project = "${var.project}" - name = "${var.name}" - environment = "${var.environment}" - instance_type = "${var.instance_type}" - key_name = "${var.key_name}" - security_groups = "${var.security_groups}" - iam_instance_profile = "${var.iam_instance_profile}" + loadbalancers = "${var.loadbalancers}" + project = "${var.project}" + name = "${var.name}" + environment = "${var.environment}" + instance_type = "${var.instance_type}" + key_name = "${var.key_name}" + security_groups = "${var.security_groups}" + iam_instance_profile = "${var.iam_instance_profile}" associate_public_ip_address = "${var.associate_public_ip_address}" - user_data = "${var.user_data}" - disk_volume_size = "${var.disk_volume_size}" - disk_volume_type = "${var.disk_volume_type}" - subnets = "${var.subnets}" - health_check_grace_period = "${var.health_check_grace_period}" - termination_policies = ["${var.termination_policies}"] - target_group_arns = "${var.target_group_arns}" + user_data = "${var.user_data}" + disk_volume_size = "${var.disk_volume_size}" + disk_volume_type = "${var.disk_volume_type}" + subnets = "${var.subnets}" + health_check_grace_period = "${var.health_check_grace_period}" + termination_policies = ["${var.termination_policies}"] + target_group_arns = "${var.target_group_arns}" } diff --git a/blue-green/variables.tf b/blue-green/variables.tf index 53b7a3a..6d92539 100644 --- a/blue-green/variables.tf +++ b/blue-green/variables.tf @@ -1,92 +1,112 @@ variable "project" { description = "Project name to use" } + variable "name" { description = "Name of the stack" } + variable "environment" { description = "Environment to deploy on" } + variable "blue_ami" { description = "(Required) The EC2 image ID to launch in the blue autoscaling group" } + variable "green_ami" { description = "(Required) The EC2 image ID to launch in the green autoscaling group" } + variable "instance_type" { description = "(Required) The size of instance to launch" } + variable "key_name" { description = "(Optional) The key name that should be used for the instance" - default = "" + default = "" } + variable "loadbalancers" { - type = "list" + type = "list" description = "(Optional) A list of load balancer names to add to the autoscaling groups" - default = [] + default = [] } + variable "security_groups" { - type = "list" + type = "list" description = "(Optional) A list of associated security group IDS" - default = [] + default = [] } + variable "iam_instance_profile" { description = "(Optional) The IAM instance profile to associate with launched instances" - default = "" + default = "" } + variable "associate_public_ip_address" { description = "(Optional) Associate a public ip address with an instance in a VPC" - default = false + default = false } + variable "user_data" { description = "(Optional) The user data to provide when launching the instance" - default = "" + default = "" } + variable "disk_volume_size" { description = "(Optional) The size of the volume in gigabytes" - default = "8" + default = "8" } + variable "disk_volume_type" { description = "(Optional) The type of the volume. Default is standard" - default = "standard" + default = "standard" } variable "subnets" { description = "(Optional) A list of subnet IDs to launch resources in" - type = "list" - default = [] + type = "list" + default = [] } + variable "blue_max_size" { description = "(Required) The maximum size of the blue autoscaling group" } + variable "blue_min_size" { description = "(Required) The minimum size of the blue autoscaling group" } + variable "blue_desired_capacity" { description = "(Required) The number of Amazon EC2 instances that should be running in the blue autoscaling roup" } + variable "green_max_size" { description = "(Required) The maximum size of the green autoscaling group" } + variable "green_min_size" { description = "(Required) The minimum size of the green autoscaling group" } + variable "green_desired_capacity" { description = "(Required) The number of Amazon EC2 instances that should be running in the green autoscaling roup" } + variable "health_check_grace_period" { description = "(Optional, Default: 300) Time (in seconds) after instance comes into service before checking health" - default = "300" + default = "300" } variable "termination_policies" { description = "(Optional, Default: ['Default']) Order in termination policies to apply when choosing instances to terminate." - type = "list" - default = ["Default"] + type = "list" + default = ["Default"] } variable "target_group_arns" { - type = "list" + type = "list" description = "A list of aws_alb_target_group ARNs, for use with Application Load Balancing" - default = [] + default = [] } diff --git a/cpu-scaling/main.tf b/cpu-scaling/main.tf index 8016e1e..eb8b5af 100644 --- a/cpu-scaling/main.tf +++ b/cpu-scaling/main.tf @@ -1,53 +1,57 @@ resource "aws_cloudwatch_metric_alarm" "alarm-cpu-down" { - count = "${var.num_asg}" - alarm_name = "${var.environment}-${var.project}-${var.name}-cpu-down${count.index}" + count = "${var.num_asg}" + alarm_name = "${var.environment}-${var.project}-${var.name}-cpu-down${count.index}" comparison_operator = "LessThanOrEqualToThreshold" - evaluation_periods = "${var.evaluation_periods}" - metric_name = "CPUUtilization" - namespace = "${var.namespace}" - period = "${var.period_down}" - statistic = "${var.statistic}" - threshold = "${var.threshold_down}" + evaluation_periods = "${var.evaluation_periods}" + metric_name = "CPUUtilization" + namespace = "${var.namespace}" + period = "${var.period_down}" + statistic = "${var.statistic}" + threshold = "${var.threshold_down}" + dimensions { AutoScalingGroupName = "${var.autoscaling_group_name[count.index]}" } + alarm_description = "This metric monitors CPU utilization down" - alarm_actions = ["${aws_autoscaling_policy.down-cpu.*.arn}"] + alarm_actions = ["${aws_autoscaling_policy.down-cpu.*.arn}"] } resource "aws_cloudwatch_metric_alarm" "alarm-cpu-up" { - count = "${var.num_asg}" - alarm_name = "${var.environment}-${var.project}-${var.name}-cpu-up${count.index}" + count = "${var.num_asg}" + alarm_name = "${var.environment}-${var.project}-${var.name}-cpu-up${count.index}" comparison_operator = "GreaterThanOrEqualToThreshold" - evaluation_periods = "${var.evaluation_periods}" - metric_name = "CPUUtilization" - namespace = "${var.namespace}" - period = "${var.period_up}" - statistic = "${var.statistic}" - threshold = "${var.threshold_up}" + evaluation_periods = "${var.evaluation_periods}" + metric_name = "CPUUtilization" + namespace = "${var.namespace}" + period = "${var.period_up}" + statistic = "${var.statistic}" + threshold = "${var.threshold_up}" + dimensions { AutoScalingGroupName = "${var.autoscaling_group_name[count.index]}" } + alarm_description = "This metric monitors CPU utilization up" - alarm_actions = ["${aws_autoscaling_policy.up-cpu.*.arn}"] + alarm_actions = ["${aws_autoscaling_policy.up-cpu.*.arn}"] } resource "aws_autoscaling_policy" "up-cpu" { - count = "${var.num_asg}" - name = "${var.environment}-${var.project}-${var.name}-cpu-up${count.index}" + count = "${var.num_asg}" + name = "${var.environment}-${var.project}-${var.name}-cpu-up${count.index}" autoscaling_group_name = "${var.autoscaling_group_name[count.index]}" - adjustment_type = "ChangeInCapacity" - policy_type = "${var.policy_type}" - cooldown = "${var.cooldown_up}" - scaling_adjustment = "${var.adjustment_up}" + adjustment_type = "ChangeInCapacity" + policy_type = "${var.policy_type}" + cooldown = "${var.cooldown_up}" + scaling_adjustment = "${var.adjustment_up}" } resource "aws_autoscaling_policy" "down-cpu" { - count = "${var.num_asg}" - name = "${var.environment}-${var.project}-${var.name}-cpu-down${count.index}" + count = "${var.num_asg}" + name = "${var.environment}-${var.project}-${var.name}-cpu-down${count.index}" autoscaling_group_name = "${var.autoscaling_group_name[count.index]}" - adjustment_type = "ChangeInCapacity" - policy_type = "${var.policy_type}" - cooldown = "${var.cooldown_down}" - scaling_adjustment = "${var.adjustment_down}" + adjustment_type = "ChangeInCapacity" + policy_type = "${var.policy_type}" + cooldown = "${var.cooldown_down}" + scaling_adjustment = "${var.adjustment_down}" } diff --git a/cpu-scaling/variables.tf b/cpu-scaling/variables.tf index b8c5676..a118968 100644 --- a/cpu-scaling/variables.tf +++ b/cpu-scaling/variables.tf @@ -1,71 +1,81 @@ variable "evaluation_periods" { - default = "4" + default = "4" description = "the number of samples to evaluate" } + variable "namespace" { - default = "AWS/EC2" + default = "AWS/EC2" description = "the namespace of the cpu cloudwatch metric" } + variable "period_down" { - default = "120" + default = "120" description = "he period in seconds over which the CPUUtilization statistic is applied." } + variable "period_up" { - default = "60" + default = "60" description = "he period in seconds over which the CPUUtilization statistic is applied." } + variable "statistic" { - default = "Average" + default = "Average" description = "The statistic to apply to the alarm's associated metric. Either of the following is supported:" } variable "threshold_up" { - default = "80" + default = "80" description = "The cpu value to scale up" } variable "threshold_down" { - default = "30" + default = "30" description = "The cpu value to scale down" } + variable "policy_type" { - default = "SimpleScaling" + default = "SimpleScaling" description = "The policy type, either SimpleScaling or StepScaling" } variable "cooldown_up" { - default = "300" + default = "300" description = "The amount of time, in seconds, after a scaling activity completes and before the next scaling activity can start." } variable "cooldown_down" { - default = "600" + default = "600" description = "The amount of time, in seconds, after a scaling activity completes and before the next scaling activity can start." } variable "adjustment_up" { - default = "1" + default = "1" description = "The number of instances to add when the alarm is triggered" } variable "adjustment_down" { - default = "-1" + default = "-1" description = "The number of instances to remove when the alarm is triggered (the value has to be negative)" } + variable "num_asg" { description = "the number of autoscaling groups passed" - default = "2" + default = "2" } + variable "project" { description = "Project name to use" } + variable "name" { description = "Name of the stack" } + variable "environment" { description = "Environment to deploy on" } + variable "autoscaling_group_name" { description = "The name of the AS group this config needs to be applied" - type = "list" + type = "list" } diff --git a/single-stack/main.tf b/single-stack/main.tf index 27ebae7..a0502f7 100644 --- a/single-stack/main.tf +++ b/single-stack/main.tf @@ -1,15 +1,15 @@ resource "aws_launch_configuration" "bluegreen_launchconfig" { - image_id = "${var.ami}" - instance_type = "${var.instance_type}" - key_name = "${var.key_name}" - security_groups = ["${var.security_groups}"] - iam_instance_profile = "${var.iam_instance_profile}" - associate_public_ip_address = "${var.associate_public_ip_address}" - user_data = "${var.user_data}" + image_id = "${var.ami}" + instance_type = "${var.instance_type}" + key_name = "${var.key_name}" + security_groups = ["${var.security_groups}"] + iam_instance_profile = "${var.iam_instance_profile}" + associate_public_ip_address = "${var.associate_public_ip_address}" + user_data = "${var.user_data}" root_block_device { - volume_type = "${var.disk_volume_type}" - volume_size = "${var.disk_volume_size}" + volume_type = "${var.disk_volume_type}" + volume_size = "${var.disk_volume_size}" delete_on_termination = "true" } @@ -19,40 +19,45 @@ resource "aws_launch_configuration" "bluegreen_launchconfig" { } resource "aws_autoscaling_group" "bluegreen_asg" { - name = "asg-${var.project}-${var.name}-${var.environment}-${var.color}" - launch_configuration = "${aws_launch_configuration.bluegreen_launchconfig.id}" - vpc_zone_identifier = ["${var.subnets}"] - load_balancers = ["${var.loadbalancers}"] - max_size = "${var.max_size}" - min_size = "${var.min_size}" - desired_capacity = "${var.desired_capacity}" - health_check_type = "ELB" - health_check_grace_period = "${var.health_check_grace_period}" - termination_policies = ["${var.termination_policies}"] - target_group_arns = ["${var.target_group_arns}"] + name = "asg-${var.project}-${var.name}-${var.environment}-${var.color}" + launch_configuration = "${aws_launch_configuration.bluegreen_launchconfig.id}" + vpc_zone_identifier = ["${var.subnets}"] + load_balancers = ["${var.loadbalancers}"] + max_size = "${var.max_size}" + min_size = "${var.min_size}" + desired_capacity = "${var.desired_capacity}" + health_check_type = "ELB" + health_check_grace_period = "${var.health_check_grace_period}" + termination_policies = ["${var.termination_policies}"] + target_group_arns = ["${var.target_group_arns}"] + tag { - key = "Environment" - value = "${var.environment}" + key = "Environment" + value = "${var.environment}" propagate_at_launch = true } + tag { - key = "Project" - value = "${var.project}" + key = "Project" + value = "${var.project}" propagate_at_launch = true } + tag { - key = "Type" - value = "${var.name}" + key = "Type" + value = "${var.name}" propagate_at_launch = true } + tag { - key = "Name" - value = "${var.project}-${var.name}-${var.environment}-${var.color}" + key = "Name" + value = "${var.project}-${var.name}-${var.environment}-${var.color}" propagate_at_launch = true } + tag { - key = "Color" - value = "${var.color}" + key = "Color" + value = "${var.color}" propagate_at_launch = true } } diff --git a/single-stack/variables.tf b/single-stack/variables.tf index 43c42f6..e67b2b8 100644 --- a/single-stack/variables.tf +++ b/single-stack/variables.tf @@ -1,82 +1,100 @@ variable "color" { description = "" } + variable "project" { description = "Project name to use" } + variable "name" { description = "Name of the stack" } + variable "environment" { description = "Environment to deploy on" } + variable "ami" { description = "(Required) The EC2 image ID to launch" } + variable "instance_type" { description = "(Required) The size of instance to launch" } + variable "key_name" { description = "(Optional) The key name that should be used for the instance" - default = "" + default = "" } + variable "loadbalancers" { - type = "list" + type = "list" description = "(Optional) A list of load balancer names to add to the autoscaling group" - default = [] + default = [] } + variable "security_groups" { - type = "list" + type = "list" description = "(Optional) A list of associated security group IDS" - default = [] + default = [] } + variable "iam_instance_profile" { description = "(Optional) The IAM instance profile to associate with launched instances" - default = "" + default = "" } + variable "associate_public_ip_address" { description = "(Optional) Associate a public ip address with an instance in a VPC" - default = false + default = false } + variable "user_data" { description = "(Optional) The user data to provide when launching the instance" - default = "" + default = "" } + variable "disk_volume_size" { description = "(Optional) The size of the volume in gigabytes" - default = "8" + default = "8" } + variable "disk_volume_type" { description = "(Optional) The type of the volume. Default is standard" - default = "standard" + default = "standard" } + variable "subnets" { description = "(Optional) A list of subnet IDs to launch resources in" - type = "list" - default = [] + type = "list" + default = [] } + variable "max_size" { description = "(Required) The maximum size of the auto scale group" } + variable "min_size" { description = "(Required) The minimum size of the auto scale group" } + variable "desired_capacity" { description = "(Required) The number of Amazon EC2 instances that should be running in the group" } + variable "health_check_grace_period" { description = "(Optional, Default: 300) Time (in seconds) after instance comes into service before checking health" - default = "300" + default = "300" } variable "termination_policies" { description = "(Optional, Default: ['Default']) Order in termination policies to apply when choosing instances to terminate. Always end with 'Default'." - type = "list" - default = ["Default"] + type = "list" + default = ["Default"] } variable "target_group_arns" { - type = "list" + type = "list" description = "A list of aws_alb_target_group ARNs, for use with Application Load Balancing" - default = [] + default = [] }