From 04d8b6da424a8d5c2b8c45334867c7fa80884cd8 Mon Sep 17 00:00:00 2001 From: saumitra-dev Date: Thu, 25 Jul 2024 16:15:19 +0530 Subject: [PATCH] feat(modules/asg): `protect_from_scale_in` variable Add `health_check_type` variable in root module for asg. --- README.md | 2 ++ main.tf | 16 +++++++++------- modules/asg/README.md | 1 + modules/asg/main.tf | 2 +- modules/asg/variables.tf | 6 ++++++ tests/asg_unit_tests.tftest.hcl | 15 ++++++++------- variables.tf | 12 ++++++++++++ 7 files changed, 39 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index c7010c7..5403874 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ No resources. |------|-------------|------|---------|:--------:| | [asg\_create\_launch\_template](#input\_asg\_create\_launch\_template) | Either to create a Launch Template to associate with the Autoscaling group | `bool` | `true` | no | | [asg\_desired\_capacity](#input\_asg\_desired\_capacity) | The number of Amazon EC2 instances that should be running in the group. | `number` | n/a | yes | +| [asg\_health\_check\_type](#input\_asg\_health\_check\_type) | (Optional) "EC2" or "ELB". Controls how health checking is done. | `string` | `null` | no | | [asg\_iam\_instance\_profile\_name](#input\_asg\_iam\_instance\_profile\_name) | (Optional, Forces new resource) Name of the instance profile. | `string` | `null` | no | | [asg\_iam\_instance\_profile\_tags](#input\_asg\_iam\_instance\_profile\_tags) | (Optional) Map of resource tags for the IAM Instance Profile. | `map(string)` | `{}` | no | | [asg\_iam\_role\_name](#input\_asg\_iam\_role\_name) | (Optional, Forces new resource) Friendly name of the role. | `string` | `null` | no | @@ -47,6 +48,7 @@ No resources. | [asg\_max\_size](#input\_asg\_max\_size) | Maximum size of the Auto Scaling Group | `number` | n/a | yes | | [asg\_min\_size](#input\_asg\_min\_size) | Minimum size of the Auto Scaling Group | `number` | n/a | yes | | [asg\_name](#input\_asg\_name) | (Optional) Name of the Auto Scaling Group. | `string` | n/a | yes | +| [asg\_protect\_from\_scale\_in](#input\_asg\_protect\_from\_scale\_in) | (Optional) Whether newly launched instances are automatically protected from termination by Amazon EC2 Auto Scaling when scaling in. | `bool` | `null` | no | | [asg\_tags](#input\_asg\_tags) | Resources Tags for Autoscaling group | `map(string)` | `{}` | no | | [asg\_vpc\_zone\_identifier](#input\_asg\_vpc\_zone\_identifier) | (Optional) List of subnet IDs to launch resources in. | `list(string)` | n/a | yes | | [cluster\_name](#input\_cluster\_name) | (Required) Name of the cluster | `string` | n/a | yes | diff --git a/main.tf b/main.tf index 2cd9432..9b1e650 100644 --- a/main.tf +++ b/main.tf @@ -20,13 +20,15 @@ module "asg" { cluster_name = module.cluster.name - name = var.asg_name - vpc_zone_identifier = var.asg_vpc_zone_identifier - desired_capacity = var.asg_desired_capacity - min_size = var.asg_min_size - max_size = var.asg_max_size - instances_tags = var.asg_instances_tags - tags = var.asg_tags + name = var.asg_name + vpc_zone_identifier = var.asg_vpc_zone_identifier + desired_capacity = var.asg_desired_capacity + min_size = var.asg_min_size + max_size = var.asg_max_size + protect_from_scale_in = var.asg_protect_from_scale_in + health_check_type = var.asg_health_check_type + instances_tags = var.asg_instances_tags + tags = var.asg_tags # Launch Template create_launch_template = var.asg_create_launch_template diff --git a/modules/asg/README.md b/modules/asg/README.md index da8a532..cbf6d20 100644 --- a/modules/asg/README.md +++ b/modules/asg/README.md @@ -49,6 +49,7 @@ No modules. | [max\_size](#input\_max\_size) | Maximum size of the Auto Scaling Group | `number` | n/a | yes | | [min\_size](#input\_min\_size) | Minimum size of the Auto Scaling Group | `number` | n/a | yes | | [name](#input\_name) | (Optional) Name of the Auto Scaling Group. | `string` | `null` | no | +| [protect\_from\_scale\_in](#input\_protect\_from\_scale\_in) | (Optional) Whether newly launched instances are automatically protected from termination by Amazon EC2 Auto Scaling when scaling in. | `bool` | `false` | no | | [tags](#input\_tags) | Resources Tags for Autoscaling group | `map(string)` | `{}` | no | | [vpc\_zone\_identifier](#input\_vpc\_zone\_identifier) | (Optional) List of subnet IDs to launch resources in. | `list(string)` | `[]` | no | diff --git a/modules/asg/main.tf b/modules/asg/main.tf index c9e4bc0..3beeb2d 100644 --- a/modules/asg/main.tf +++ b/modules/asg/main.tf @@ -6,7 +6,7 @@ resource "aws_autoscaling_group" "this" { min_size = var.min_size max_size = var.max_size - protect_from_scale_in = false + protect_from_scale_in = var.protect_from_scale_in health_check_type = var.health_check_type launch_template { diff --git a/modules/asg/variables.tf b/modules/asg/variables.tf index 095f68d..8cad2d8 100644 --- a/modules/asg/variables.tf +++ b/modules/asg/variables.tf @@ -30,6 +30,12 @@ variable "max_size" { type = number } +variable "protect_from_scale_in" { + description = " (Optional) Whether newly launched instances are automatically protected from termination by Amazon EC2 Auto Scaling when scaling in." + type = bool + default = false +} + variable "health_check_type" { description = "(Optional) \"EC2\" or \"ELB\". Controls how health checking is done." type = string diff --git a/tests/asg_unit_tests.tftest.hcl b/tests/asg_unit_tests.tftest.hcl index 7c9d00f..ae92fdc 100644 --- a/tests/asg_unit_tests.tftest.hcl +++ b/tests/asg_unit_tests.tftest.hcl @@ -16,12 +16,13 @@ run "asg_attributes_match" { variables { cluster_name = "example-cluster-name" - name = "example-asg-name" - vpc_zone_identifier = ["subnet-12345678901234", "subnet-12345678901235"] - desired_capacity = 7 - min_size = 1 - max_size = 10 - health_check_type = "ELB" + name = "example-asg-name" + vpc_zone_identifier = ["subnet-12345678901234", "subnet-12345678901235"] + desired_capacity = 7 + min_size = 1 + max_size = 10 + protect_from_scale_in = true + health_check_type = "ELB" create_launch_template = false launch_template_id = "lt-068f72b729example" @@ -65,7 +66,7 @@ run "asg_attributes_match" { } assert { - condition = aws_autoscaling_group.this.protect_from_scale_in == false + condition = aws_autoscaling_group.this.protect_from_scale_in == var.protect_from_scale_in error_message = "Protect from scale in mismatch" } diff --git a/variables.tf b/variables.tf index d53f2f2..0a6779f 100644 --- a/variables.tf +++ b/variables.tf @@ -73,6 +73,18 @@ variable "asg_max_size" { } } +variable "asg_protect_from_scale_in" { + description = " (Optional) Whether newly launched instances are automatically protected from termination by Amazon EC2 Auto Scaling when scaling in." + type = bool + default = null +} + +variable "asg_health_check_type" { + description = "(Optional) \"EC2\" or \"ELB\". Controls how health checking is done." + type = string + default = null +} + variable "asg_instances_tags" { description = "Resources Tags to propagate to the Instances" type = map(string)