diff --git a/README.md b/README.md index b97f4c6..568787f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # terraform-bluegreen + Terraform module to setup blue / green deployments ## blue-green @@ -7,33 +8,36 @@ Terraform module to setup blue / green deployments | Name | Description | Type | Default | Required | |------|-------------|:----:|:-----:|:-----:| -| associate\_public\_ip\_address | (Optional) Associate a public ip address with an instance in a VPC | string | `"false"` | no | -| blue\_ami | (Required) The EC2 image ID to launch in the blue autoscaling group | string | n/a | yes | -| blue\_desired\_capacity | (Required) The number of Amazon EC2 instances that should be running in the blue autoscaling roup | string | n/a | yes | -| blue\_max\_size | (Required) The maximum size of the blue autoscaling group | string | n/a | yes | -| blue\_min\_size | (Required) The minimum size of the blue autoscaling group | string | n/a | yes | -| disk\_volume\_size | (Optional) The size of the volume in gigabytes | string | `"8"` | no | -| disk\_volume\_type | (Optional) The type of the volume. Default is standard | string | `"standard"` | no | -| environment | Environment to deploy on | string | n/a | yes | -| green\_ami | (Required) The EC2 image ID to launch in the green autoscaling group | string | n/a | yes | -| green\_desired\_capacity | (Required) The number of Amazon EC2 instances that should be running in the green autoscaling roup | string | n/a | yes | -| green\_max\_size | (Required) The maximum size of the green autoscaling group | string | n/a | yes | -| green\_min\_size | (Required) The minimum size of the green autoscaling group | string | n/a | yes | -| health\_check\_grace\_period | (Optional, Default: 300) Time (in seconds) after instance comes into service before checking health | string | `"300"` | no | +| associate\_public\_ip\_address | Associate a public ip address with an instance in a VPC | string | `"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 | string | n/a | yes | +| blue\_disk\_volume\_size | The size of the EBS volume in GB for the Blue instances | string | `"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 | string | n/a | yes | +| blue\_min\_size | The minimum size of the blue autoscaling group | string | 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 | string | n/a | yes | +| green\_disk\_volume\_size | The size of the EBS volume in GB for the Green instances | string | `"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 | string | n/a | yes | +| green\_min\_size | The minimum size of the green autoscaling group | string | 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 | string | `"300"` | no | | health\_check\_type | The health check type to apply to the Autoscaling groups. | string | `"ELB"` | no | -| iam\_instance\_profile | (Optional) The IAM instance profile to associate with launched instances | string | `""` | no | -| instance\_type | (Required) The size of instance to launch | string | n/a | yes | -| key\_name | (Optional) The key name that should be used for the instance | string | `""` | no | -| loadbalancers | (Optional) A list of load balancer names to add to the autoscaling groups | list | `` | no | -| name | Name of the stack | string | n/a | yes | -| project | Project name to use | string | n/a | yes | -| security\_groups | (Optional) A list of associated security group IDS | list | `` | no | +| iam\_instance\_profile | The IAM instance profile to associate with launched instances | string | `""` | no | +| initial\_lifecycle\_hooks | One or more [Lifecycle Hooks](http://docs.aws.amazon.com/autoscaling/latest/userguide/lifecycle-hooks.html) to attach to the autoscaling group before instances are launched. The syntax is exactly the same as the separate [`aws_autoscaling_lifecycle_hook`](https://www.terraform.io/docs/providers/aws/r/autoscaling_lifecycle_hooks.html) resource, without the autoscaling_group_name attribute | list | `` | no | +| key\_name | The key name that should be used for the instance | string | `""` | no | +| loadbalancers | A list of load balancer names to add to the autoscaling groups | list | `` | no | +| name | Name of the Auto Scaling Groups | string | n/a | yes | +| security\_groups | A list of associated security group IDS | list | `` | 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 | (Optional) A list of subnet IDs to launch resources in | list | `` | no | -| tags | (Optional, Default: []) List of map of additional tags | list | `` | no | +| subnets | A list of subnet IDs to launch resources in | list | `` | no | +| tags | List as a map of additional tags | list | `` | no | | target\_group\_arns | A list of aws_alb_target_group ARNs, for use with Application Load Balancing | list | `` | no | -| termination\_policies | (Optional, Default: ['Default']) Order in termination policies to apply when choosing instances to terminate. | list | `` | no | -| user\_data | (Optional) The user data to provide when launching the instance | string | `"# Hello World"` | no | +| termination\_policies | Order in termination policies to apply when choosing instances to terminate. | list | `` | 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 @@ -42,26 +46,27 @@ Terraform module to setup blue / green deployments |------|-------------| | 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 ```terraform module "bluegreen" { - source = "github.com/skyscrapers/terraform-bluegreen//blue-green" - project = "example" - name = "app" - environment = "production" - blue_ami = "ami-blabla" - green_ami = "ami-blabla" - instance_type = "t2.micro" - loadbalancers = [] - blue_max_size = "5" - blue_min_size = "2" - blue_desired_capacity = "2" - green_max_size = "0" - green_min_size = "0" - green_desired_capacity = "0" - security_groups = [] + 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"] } ``` @@ -76,21 +81,19 @@ Terraform module to setup alarms and autoscaling triggers for autoscaling | adjustment\_down | The number of instances to remove when the alarm is triggered (the value has to be negative) | string | `"-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 | string | `"1"` | no | -| autoscaling\_group\_name | The name of the AS group this config needs to be applied | list | n/a | yes | +| autoscaling\_group\_names | The names of the Auto Scaling Groups this config needs to be applied to | list | n/a | yes | | cooldown\_down | The amount of time, in seconds, after a scaling activity completes and before the next scaling activity can start. | string | `"600"` | no | | cooldown\_up | The amount of time, in seconds, after a scaling activity completes and before the next scaling activity can start. | string | `"300"` | no | | dimension\_name | | string | `"AutoScalingGroupName"` | no | | dimension\_value | | string | `"false"` | no | -| environment | Environment to deploy on | string | n/a | yes | -| evaluation\_periods | the number of samples to evaluate | string | `"4"` | no | +| evaluation\_periods | The number of samples to evaluate | string | `"4"` | no | | metric\_name | The metric the scaling is based upon | string | `"CPUUtilization"` | no | -| name | Name of the stack | string | n/a | yes | -| namespace | the namespace of the cloudwatch metric | string | `"AWS/EC2"` | no | -| num\_asg | the number of autoscaling groups passed | string | `"2"` | no | -| period\_down | he period in seconds over which the selected metric statistic is applied. | string | `"120"` | no | -| period\_up | he period in seconds over which the selected metric statistic is applied. | string | `"60"` | no | +| name | Name of the Auto Scaling Groups | string | n/a | yes | +| namespace | The namespace of the cloudwatch metric | string | `"AWS/EC2"` | no | +| num\_asg | The number of autoscaling groups passed | string | `"2"` | no | +| period\_down | The period in seconds over which the selected metric statistic is applied. | string | `"120"` | no | +| period\_up | The period in seconds over which the selected metric statistic is applied. | string | `"60"` | no | | policy\_type | The policy type, either SimpleScaling or StepScaling | string | `"SimpleScaling"` | no | -| project | Project name to use | string | n/a | yes | | 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 | string | `"30"` | no | | threshold\_up | The metric value to scale up | string | `"80"` | no | @@ -99,11 +102,9 @@ Terraform module to setup alarms and autoscaling triggers for autoscaling ```terraform module "scaling" { - source = "github.com/skyscrapers/terraform-bluegreen//scaling" - project = "example" - name = "app" - environment = "production" - autoscaling_group_name = ["my_asg_name1","my_asg_name2"] + source = "github.com/skyscrapers/terraform-bluegreen//scaling" + name = "app-${terraform.workspace}" + autoscaling_group_names = ["my_asg_name1","my_asg_name2"] } ``` diff --git a/blue-green/main.tf b/blue-green/main.tf index 69a7f7a..9839145 100644 --- a/blue-green/main.tf +++ b/blue-green/main.tf @@ -1,57 +1,55 @@ module "blue" { source = "../single-stack" color = "blue" + name = "${var.name}" max_size = "${var.blue_max_size}" min_size = "${var.blue_min_size}" desired_capacity = "${var.blue_desired_capacity}" ami = "${var.blue_ami}" + user_data = "${var.blue_user_data}" + instance_type = "${var.blue_instance_type}" + disk_volume_size = "${var.blue_disk_volume_size}" + disk_volume_type = "${var.blue_disk_volume_type}" spot_price = "${var.spot_price}" - loadbalancers = "${var.loadbalancers}" - project = "${var.project}" - name = "${var.name}" - environment = "${var.environment}" - instance_type = "${var.instance_type}" + loadbalancers = ["${var.loadbalancers}"] key_name = "${var.key_name}" - security_groups = "${var.security_groups}" + 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}" + target_group_arns = ["${var.target_group_arns}"] health_check_type = "${var.health_check_type}" - tags = "${var.tags}" wait_for_capacity_timeout = "${var.wait_for_capacity_timeout}" + tags = "${var.tags}" + initial_lifecycle_hooks = ["${var.initial_lifecycle_hooks}"] } module "green" { source = "../single-stack" color = "green" + name = "${var.name}" max_size = "${var.green_max_size}" min_size = "${var.green_min_size}" desired_capacity = "${var.green_desired_capacity}" ami = "${var.green_ami}" + user_data = "${var.green_user_data}" + instance_type = "${var.green_instance_type}" + disk_volume_size = "${var.green_disk_volume_size}" + disk_volume_type = "${var.green_disk_volume_type}" spot_price = "${var.spot_price}" - loadbalancers = "${var.loadbalancers}" - project = "${var.project}" - name = "${var.name}" - environment = "${var.environment}" - instance_type = "${var.instance_type}" + loadbalancers = ["${var.loadbalancers}"] key_name = "${var.key_name}" - security_groups = "${var.security_groups}" + 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}" + target_group_arns = ["${var.target_group_arns}"] health_check_type = "${var.health_check_type}" - tags = "${var.tags}" wait_for_capacity_timeout = "${var.wait_for_capacity_timeout}" + tags = "${var.tags}" + initial_lifecycle_hooks = ["${var.initial_lifecycle_hooks}"] } diff --git a/blue-green/outputs.tf b/blue-green/outputs.tf index c71ced9..b98eb15 100644 --- a/blue-green/outputs.tf +++ b/blue-green/outputs.tf @@ -1,9 +1,19 @@ output "blue_asg_id" { description = "Blue autoscaling group id" - value = "${module.blue.asg_id}" + value = "${module.blue.asg_id}" } output "green_asg_id" { description = "Green autoscaling group id" - value = "${module.green.asg_id}" + value = "${module.green.asg_id}" +} + +output "nonbinding_blue_asg_name" { + description = "This is the raw blue autoscaling group name, without creating a dependency with the actual autoscaling group resource" + value = "${module.blue.nonbinding_asg_name}" +} + +output "nonbinding_green_asg_name" { + description = "This is the raw green autoscaling group name, without creating a dependency with the actual autoscaling group resource" + value = "${module.green.nonbinding_asg_name}" } diff --git a/blue-green/variables.tf b/blue-green/variables.tf index fb2a9d1..a0b1c44 100644 --- a/blue-green/variables.tf +++ b/blue-green/variables.tf @@ -1,106 +1,117 @@ -variable "project" { - description = "Project name to use" +variable "name" { + description = "Name of the Auto Scaling Groups" } -variable "name" { - description = "Name of the stack" +variable "blue_max_size" { + description = "The maximum size of the blue autoscaling group" +} + +variable "blue_min_size" { + description = "The minimum size of the blue autoscaling group" } -variable "environment" { - description = "Environment to deploy on" +variable "blue_desired_capacity" { + description = "The number of Amazon EC2 instances that should be running in the blue autoscaling roup" +} + +variable "blue_instance_type" { + description = "The Blue instance type to launch" } variable "blue_ami" { - description = "(Required) The EC2 image ID to launch in the blue autoscaling group" + description = "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 "blue_user_data" { + description = "The user data to provide when launching the Blue instances" + default = "# Hello World" } -variable "instance_type" { - description = "(Required) The size of instance to launch" +variable "blue_disk_volume_size" { + description = "The size of the EBS volume in GB for the Blue instances" + default = "8" } -variable "key_name" { - description = "(Optional) The key name that should be used for the instance" - default = "" +variable "blue_disk_volume_type" { + description = "The EBS volume type for the Blue instances" + default = "gp2" } -variable "loadbalancers" { - type = "list" - description = "(Optional) A list of load balancer names to add to the autoscaling groups" - default = [] +variable "green_max_size" { + description = "The maximum size of the green autoscaling group" } -variable "security_groups" { - type = "list" - description = "(Optional) A list of associated security group IDS" - default = [] +variable "green_min_size" { + description = "The minimum size of the green autoscaling group" } -variable "iam_instance_profile" { - description = "(Optional) The IAM instance profile to associate with launched instances" - default = "" +variable "green_desired_capacity" { + description = "The number of Amazon EC2 instances that should be running in the green autoscaling roup" } -variable "associate_public_ip_address" { - description = "(Optional) Associate a public ip address with an instance in a VPC" - default = false +variable "green_instance_type" { + description = "The Green instance type to launch" } -variable "user_data" { - description = "(Optional) The user data to provide when launching the instance" - default = "# Hello World" +variable "green_ami" { + description = "The EC2 image ID to launch in the Green autoscaling group" } -variable "disk_volume_size" { - description = "(Optional) The size of the volume in gigabytes" - default = "8" +variable "green_user_data" { + description = "The user data to provide when launching the Green instances" + default = "# Hello World" } -variable "disk_volume_type" { - description = "(Optional) The type of the volume. Default is standard" - default = "standard" +variable "green_disk_volume_size" { + description = "The size of the EBS volume in GB for the Green instances" + default = "8" } -variable "subnets" { - description = "(Optional) A list of subnet IDs to launch resources in" - type = "list" - default = [] +variable "green_disk_volume_type" { + description = "The EBS volume type for the Green instances" + default = "gp2" } -variable "blue_max_size" { - description = "(Required) The maximum size of the blue autoscaling group" +variable "key_name" { + description = "The key name that should be used for the instance" + default = "" } -variable "blue_min_size" { - description = "(Required) The minimum size of the blue autoscaling group" +variable "loadbalancers" { + type = "list" + description = "A list of load balancer names to add to the autoscaling groups" + default = [] } -variable "blue_desired_capacity" { - description = "(Required) The number of Amazon EC2 instances that should be running in the blue autoscaling roup" +variable "security_groups" { + type = "list" + description = "A list of associated security group IDS" + default = [] } -variable "green_max_size" { - description = "(Required) The maximum size of the green autoscaling group" +variable "iam_instance_profile" { + description = "The IAM instance profile to associate with launched instances" + default = "" } -variable "green_min_size" { - description = "(Required) The minimum size of the green autoscaling group" +variable "associate_public_ip_address" { + description = "Associate a public ip address with an instance in a VPC" + default = false } -variable "green_desired_capacity" { - description = "(Required) The number of Amazon EC2 instances that should be running in the green autoscaling roup" +variable "subnets" { + description = "A list of subnet IDs to launch resources in" + type = "list" + default = [] } variable "health_check_grace_period" { - description = "(Optional, Default: 300) Time (in seconds) after instance comes into service before checking health" + description = "Time (in seconds) after instance comes into service before checking health" default = "300" } variable "termination_policies" { - description = "(Optional, Default: ['Default']) Order in termination policies to apply when choosing instances to terminate." + description = "Order in termination policies to apply when choosing instances to terminate." type = "list" default = ["Default"] } @@ -117,7 +128,7 @@ variable "health_check_type" { } variable "tags" { - description = "(Optional, Default: []) List of map of additional tags" + description = "List as a map of additional tags" type = "list" default = [] } @@ -128,6 +139,12 @@ variable "spot_price" { } variable "wait_for_capacity_timeout" { - description = " 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." + description = "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." default = "10m" } + +variable "initial_lifecycle_hooks" { + description = "One or more [Lifecycle Hooks](http://docs.aws.amazon.com/autoscaling/latest/userguide/lifecycle-hooks.html) to attach to the autoscaling group before instances are launched. The syntax is exactly the same as the separate [`aws_autoscaling_lifecycle_hook`](https://www.terraform.io/docs/providers/aws/r/autoscaling_lifecycle_hooks.html) resource, without the autoscaling_group_name attribute" + type = "list" + default = [] +} diff --git a/bluegreen.py b/bluegreen.py index 4e9ed17..c696d32 100755 --- a/bluegreen.py +++ b/bluegreen.py @@ -12,7 +12,7 @@ def main(argv): helptext = 'bluegreen.py -f -a -c -t -e -i [-r ]' try: - opts, args = getopt.getopt(argv, "hsf:a:c:t:e:i:r:", ["folder=", "ami=", "command=", "timeout=", "environment=", "inactive-desired=", "role-arn="]) + opts, args = getopt.getopt(argv, "hsf:a:c:t:e:i:r:", ["folder=","ami=", "command=", "timeout=", "environment=", "inactive-desired=", "role-arn="]) except getopt.GetoptError: print helptext sys.exit(2) diff --git a/scaling/main.tf b/scaling/main.tf index 4951d65..d68bcc4 100644 --- a/scaling/main.tf +++ b/scaling/main.tf @@ -1,6 +1,6 @@ resource "aws_cloudwatch_metric_alarm" "alarm_down" { count = "${var.num_asg}" - alarm_name = "${var.environment}-${var.project}-${var.name}-down${count.index}" + alarm_name = "${var.name}-down${count.index}" comparison_operator = "LessThanOrEqualToThreshold" evaluation_periods = "${var.evaluation_periods}" metric_name = "${var.metric_name}" @@ -8,7 +8,7 @@ resource "aws_cloudwatch_metric_alarm" "alarm_down" { period = "${var.period_down}" statistic = "${var.statistic}" threshold = "${var.threshold_down}" - dimensions = "${map(var.dimension_name, var.dimension_value == "false" ? var.autoscaling_group_name[count.index] : var.dimension_value)}" + dimensions = "${map(var.dimension_name, var.dimension_value == "false" ? var.autoscaling_group_names[count.index] : var.dimension_value)}" alarm_description = "This metric monitors CPU utilization down" alarm_actions = ["${aws_autoscaling_policy.down.*.arn}"] @@ -16,7 +16,7 @@ resource "aws_cloudwatch_metric_alarm" "alarm_down" { resource "aws_cloudwatch_metric_alarm" "alarm_up" { count = "${var.num_asg}" - alarm_name = "${var.environment}-${var.project}-${var.name}-up${count.index}" + alarm_name = "${var.name}-up${count.index}" comparison_operator = "GreaterThanOrEqualToThreshold" evaluation_periods = "${var.evaluation_periods}" metric_name = "${var.metric_name}" @@ -24,7 +24,7 @@ resource "aws_cloudwatch_metric_alarm" "alarm_up" { period = "${var.period_up}" statistic = "${var.statistic}" threshold = "${var.threshold_up}" - dimensions = "${map(var.dimension_name, var.dimension_value == "false" ? var.autoscaling_group_name[count.index] : var.dimension_value)}" + dimensions = "${map(var.dimension_name, var.dimension_value == "false" ? var.autoscaling_group_names[count.index] : var.dimension_value)}" alarm_description = "This metric monitors CPU utilization up" alarm_actions = ["${aws_autoscaling_policy.up.*.arn}"] @@ -32,8 +32,8 @@ resource "aws_cloudwatch_metric_alarm" "alarm_up" { resource "aws_autoscaling_policy" "up" { count = "${var.num_asg}" - name = "${var.environment}-${var.project}-${var.name}-up${count.index}" - autoscaling_group_name = "${var.autoscaling_group_name[count.index]}" + name = "${var.name}-up${count.index}" + autoscaling_group_name = "${var.autoscaling_group_names[count.index]}" adjustment_type = "${var.adjustment_type}" policy_type = "${var.policy_type}" cooldown = "${var.cooldown_up}" @@ -42,8 +42,8 @@ resource "aws_autoscaling_policy" "up" { resource "aws_autoscaling_policy" "down" { count = "${var.num_asg}" - name = "${var.environment}-${var.project}-${var.name}-down${count.index}" - autoscaling_group_name = "${var.autoscaling_group_name[count.index]}" + name = "${var.name}-down${count.index}" + autoscaling_group_name = "${var.autoscaling_group_names[count.index]}" adjustment_type = "${var.adjustment_type}" policy_type = "${var.policy_type}" cooldown = "${var.cooldown_down}" diff --git a/scaling/variables.tf b/scaling/variables.tf index 7968c98..5e357a0 100644 --- a/scaling/variables.tf +++ b/scaling/variables.tf @@ -1,21 +1,25 @@ +variable "name" { + description = "Name for the created resources" +} + variable "evaluation_periods" { default = "4" - description = "the number of samples to evaluate" + description = "The number of samples to evaluate" } variable "namespace" { default = "AWS/EC2" - description = "the namespace of the cloudwatch metric" + description = "The namespace of the cloudwatch metric" } variable "period_down" { default = "120" - description = "he period in seconds over which the selected metric statistic is applied." + description = "The period in seconds over which the selected metric statistic is applied." } variable "period_up" { default = "60" - description = "he period in seconds over which the selected metric statistic is applied." + description = "The period in seconds over which the selected metric statistic is applied." } variable "statistic" { @@ -59,24 +63,12 @@ variable "adjustment_down" { } variable "num_asg" { - description = "the number of autoscaling groups passed" + description = "The number of autoscaling groups passed" 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" +variable "autoscaling_group_names" { + description = "The names of the Auto Scaling Groups this config needs to be applied to" type = "list" } diff --git a/single-stack/main.tf b/single-stack/main.tf index 1dafca7..cea757e 100644 --- a/single-stack/main.tf +++ b/single-stack/main.tf @@ -19,8 +19,12 @@ resource "aws_launch_configuration" "bluegreen_launchconfig" { } } +locals { + asg_name = "${var.name}-${var.color}" +} + resource "aws_autoscaling_group" "bluegreen_asg" { - name = "asg-${var.project}-${var.name}-${var.environment}-${var.color}" + name = "${local.asg_name}" launch_configuration = "${aws_launch_configuration.bluegreen_launchconfig.id}" vpc_zone_identifier = ["${var.subnets}"] load_balancers = ["${var.loadbalancers}"] @@ -32,13 +36,12 @@ resource "aws_autoscaling_group" "bluegreen_asg" { termination_policies = ["${var.termination_policies}"] target_group_arns = ["${var.target_group_arns}"] wait_for_capacity_timeout = "${var.wait_for_capacity_timeout}" - + initial_lifecycle_hook = ["${var.initial_lifecycle_hooks}"] + tags = ["${concat( list( - map("key", "Environment", "value", "${var.environment}", "propagate_at_launch", true), - map("key", "Project", "value", "${var.project}", "propagate_at_launch", true), + map("key", "Name", "value", "${local.asg_name}", "propagate_at_launch", true), map("key", "Type", "value", "${var.name}", "propagate_at_launch", true), - map("key", "Name", "value", "${var.project}-${var.name}-${var.environment}-${var.color}", "propagate_at_launch", true), map("key", "Color", "value", "${var.color}", "propagate_at_launch", true) ), var.tags) diff --git a/single-stack/outputs.tf b/single-stack/outputs.tf index 20761d2..c4c5aae 100644 --- a/single-stack/outputs.tf +++ b/single-stack/outputs.tf @@ -1,3 +1,9 @@ output "asg_id" { - value = "${aws_autoscaling_group.bluegreen_asg.id}" + description = "ASG ID" + value = "${aws_autoscaling_group.bluegreen_asg.id}" +} + +output "nonbinding_asg_name" { + description = "This is the raw autoscaling group name, without creating a dependency with the actual autoscaling group resource" + value = "${local.asg_name}" } diff --git a/single-stack/variables.tf b/single-stack/variables.tf index 7867cfc..fd9c489 100644 --- a/single-stack/variables.tf +++ b/single-stack/variables.tf @@ -1,94 +1,86 @@ variable "color" { - description = "" -} - -variable "project" { - description = "Project name to use" + description = "Color of the Auto Scaling Group" } variable "name" { - description = "Name of the stack" -} - -variable "environment" { - description = "Environment to deploy on" + description = "Name of the Auto Scaling Group" } variable "ami" { - description = "(Required) The EC2 image ID to launch" + description = "The EC2 image ID to launch" } variable "instance_type" { - description = "(Required) The size of instance to launch" + description = "The size of instance to launch" } variable "key_name" { - description = "(Optional) The key name that should be used for the instance" + description = "The key name that should be used for the instance" default = "" } variable "loadbalancers" { type = "list" - description = "(Optional) A list of load balancer names to add to the autoscaling group" + description = "A list of load balancer names to add to the autoscaling group" default = [] } variable "security_groups" { type = "list" - description = "(Optional) A list of associated security group IDS" + description = "A list of associated security group IDS" default = [] } variable "iam_instance_profile" { - description = "(Optional) The IAM instance profile to associate with launched instances" + description = "The IAM instance profile to associate with launched instances" default = "" } variable "associate_public_ip_address" { - description = "(Optional) Associate a public ip address with an instance in a VPC" + description = "Associate a public ip address with an instance in a VPC" default = false } variable "user_data" { - description = "(Optional) The user data to provide when launching the instance" + description = "The user data to provide when launching the instance" default = "# Hello World" } variable "disk_volume_size" { - description = "(Optional) The size of the volume in gigabytes" + description = "The size of the volume in gigabytes" default = "8" } variable "disk_volume_type" { - description = "(Optional) The type of the volume. Default is standard" - default = "standard" + description = "The type of the volume" + default = "gp2" } variable "subnets" { - description = "(Optional) A list of subnet IDs to launch resources in" + description = "A list of subnet IDs to launch resources in" type = "list" default = [] } variable "max_size" { - description = "(Required) The maximum size of the auto scale group" + description = "The maximum size of the auto scale group" } variable "min_size" { - description = "(Required) The minimum size of the auto scale group" + description = "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" + description = "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" + description = "Time (in seconds) after instance comes into service before checking health" default = "300" } variable "termination_policies" { - description = "(Optional, Default: ['Default']) Order in termination policies to apply when choosing instances to terminate. Always end with 'Default'." + description = "Order in termination policies to apply when choosing instances to terminate. Always end with 'Default'." type = "list" default = ["Default"] } @@ -105,7 +97,7 @@ variable "health_check_type" { } variable "tags" { - description = "(Optional, Default: []) List of map of additional tags" + description = "List of map of additional tags" type = "list" default = [] } @@ -119,3 +111,9 @@ variable "wait_for_capacity_timeout" { description = "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." default = "10m" } + +variable "initial_lifecycle_hooks" { + description = "One or more [Lifecycle Hooks](http://docs.aws.amazon.com/autoscaling/latest/userguide/lifecycle-hooks.html) to attach to the autoscaling group before instances are launched. The syntax is exactly the same as the separate [`aws_autoscaling_lifecycle_hook`](https://www.terraform.io/docs/providers/aws/r/autoscaling_lifecycle_hooks.html) resource, without the autoscaling_group_name attribute" + type = "list" + default = [] +}