Skip to content

Commit

Permalink
feat(modules/asg): enable launch template versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
saumitra-dev committed Jul 25, 2024
1 parent 63817ed commit 5a49c6a
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ No resources.
| <a name="input_asg_instances_tags"></a> [asg\_instances\_tags](#input\_asg\_instances\_tags) | Resources Tags to propagate to the Instances | `map(string)` | `{}` | no |
| <a name="input_asg_launch_template"></a> [asg\_launch\_template](#input\_asg\_launch\_template) | Launch Template to use with the Autoscaling group | <pre>object({<br> name = optional(string, null)<br> block_device_mappings = optional(list(object({<br> device_name = string<br> ebs = optional(object({<br> delete_on_termination = optional(bool, null)<br> snapshot_id = optional(string, null)<br> volume_size = optional(number, null)<br> volume_type = optional(string, null)<br> }))<br> no_device = optional(any, null)<br> virtual_name = optional(string, null)<br> })), [])<br> image_id = optional(string, null)<br> instance_type = optional(string, null)<br> vpc_security_group_ids = optional(list(string), [])<br> key_name = optional(string, null)<br> user_data = optional(string, null)<br> tags = optional(map(string), {})<br> })</pre> | `{}` | no |
| <a name="input_asg_launch_template_id"></a> [asg\_launch\_template\_id](#input\_asg\_launch\_template\_id) | Identifier of the Launch Template | `string` | `null` | no |
| <a name="input_asg_launch_template_version"></a> [asg\_launch\_template\_version](#input\_asg\_launch\_template\_version) | (Optional) Template version. | `string` | `null` | no |
| <a name="input_asg_max_size"></a> [asg\_max\_size](#input\_asg\_max\_size) | Maximum size of the Auto Scaling Group | `number` | n/a | yes |
| <a name="input_asg_min_size"></a> [asg\_min\_size](#input\_asg\_min\_size) | Minimum size of the Auto Scaling Group | `number` | n/a | yes |
| <a name="input_asg_name"></a> [asg\_name](#input\_asg\_name) | (Optional) Name of the Auto Scaling Group. | `string` | n/a | yes |
Expand Down
1 change: 1 addition & 0 deletions examples/complete/locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ locals {
key_name = "test-atlantis"
security_group_ids = []
}
asg_launch_template_version = "$Latest"

# IAM Role Variables
asg_iam_role_name = "my-asg-role"
Expand Down
5 changes: 3 additions & 2 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ module "ecs" {
asg_tags = local.asg_tags

# Launch Template
asg_create_launch_template = local.asg_create_launch_template
asg_launch_template = local.asg_launch_template
asg_create_launch_template = local.asg_create_launch_template
asg_launch_template = local.asg_launch_template
asg_launch_template_version = local.asg_launch_template_version

# IAM Role
asg_iam_role_name = local.asg_iam_role_name
Expand Down
7 changes: 4 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ module "asg" {
tags = var.asg_tags

# Launch Template
create_launch_template = var.asg_create_launch_template
launch_template_id = var.asg_launch_template_id
launch_template = var.asg_launch_template
create_launch_template = var.asg_create_launch_template
launch_template_id = var.asg_launch_template_id
launch_template = var.asg_launch_template
launch_template_version = var.asg_launch_template_version

# IAM Role
iam_role_name = var.asg_iam_role_name
Expand Down
3 changes: 2 additions & 1 deletion modules/asg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ No modules.
| <a name="input_iam_role_tags"></a> [iam\_role\_tags](#input\_iam\_role\_tags) | Key-value mapping of tags for the IAM role. | `map(string)` | `{}` | no |
| <a name="input_instances_tags"></a> [instances\_tags](#input\_instances\_tags) | Resources Tags to propagate to the Instances | `map(string)` | `{}` | no |
| <a name="input_launch_template"></a> [launch\_template](#input\_launch\_template) | Launch Template to use with the Autoscaling group | <pre>object({<br> name = optional(string, null)<br> block_device_mappings = optional(list(object({<br> device_name = string<br> ebs = optional(object({<br> delete_on_termination = optional(bool, null)<br> snapshot_id = optional(string, null)<br> volume_size = optional(number, null)<br> volume_type = optional(string, null)<br> }))<br> no_device = optional(any, null)<br> virtual_name = optional(string, null)<br> })), [])<br> image_id = optional(string, null)<br> instance_type = optional(string, null)<br> vpc_security_group_ids = optional(list(string), [])<br> key_name = optional(string, null)<br> user_data = optional(string, null)<br> tags = optional(map(string), {})<br> })</pre> | `{}` | no |
| <a name="input_launch_template_id"></a> [launch\_template\_id](#input\_launch\_template\_id) | Identifier of the Launch Template | `string` | `null` | no |
| <a name="input_launch_template_id"></a> [launch\_template\_id](#input\_launch\_template\_id) | (Optional) ID of the launch template. | `string` | `null` | no |
| <a name="input_launch_template_version"></a> [launch\_template\_version](#input\_launch\_template\_version) | (Optional) Template version. | `string` | `"$Latest"` | no |
| <a name="input_max_size"></a> [max\_size](#input\_max\_size) | Maximum size of the Auto Scaling Group | `number` | n/a | yes |
| <a name="input_min_size"></a> [min\_size](#input\_min\_size) | Minimum size of the Auto Scaling Group | `number` | n/a | yes |
| <a name="input_name"></a> [name](#input\_name) | (Optional) Name of the Auto Scaling Group. | `string` | `null` | no |
Expand Down
3 changes: 2 additions & 1 deletion modules/asg/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ resource "aws_autoscaling_group" "this" {
health_check_type = var.health_check_type

launch_template {
id = var.create_launch_template ? aws_launch_template.this[0].id : var.launch_template_id
id = var.create_launch_template ? aws_launch_template.this[0].id : var.launch_template_id
version = var.launch_template_version != null ? var.launch_template_version : null
}

dynamic "tag" {
Expand Down
8 changes: 7 additions & 1 deletion modules/asg/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,17 @@ variable "create_launch_template" {
}

variable "launch_template_id" {
description = "Identifier of the Launch Template"
description = "(Optional) ID of the launch template."
type = string
default = null
}

variable "launch_template_version" {
description = "(Optional) Template version."
type = string
default = "$Latest"
}

################################################################################
# IAM Role
################################################################################
Expand Down
10 changes: 8 additions & 2 deletions tests/asg_unit_tests.tftest.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ run "asg_attributes_match" {
max_size = 10
health_check_type = "ELB"

create_launch_template = false
launch_template_id = "lt-068f72b729example"
create_launch_template = false
launch_template_id = "lt-068f72b729example"
launch_template_version = "$Default"

iam_role_policy_attachments = [
"arn:aws:iam::aws:policy/abcd/efgh"
Expand Down Expand Up @@ -78,6 +79,11 @@ run "asg_attributes_match" {
error_message = "Launch template id mismatch"
}

assert {
condition = aws_autoscaling_group.this.launch_template[0].version == var.launch_template_version
error_message = "Launch template version mismatch"
}

assert {
condition = length(aws_launch_template.this) == 0
error_message = "Launch template was created"
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ variable "asg_launch_template_id" {
}
}

variable "asg_launch_template_version" {
description = "(Optional) Template version."
type = string
default = null
}

################################################################################
### IAM Role
################################################################################
Expand Down

0 comments on commit 5a49c6a

Please sign in to comment.