# ec2-autoscaling

Creates an EC2 Auto Scaling Group with a versioned launch template, target-tracking scaling policies, lifecycle hooks, and an optional warm pool.

Provisions a launch template (IMDSv2, encrypted root volume, detailed monitoring), an ASG with configurable min/max/desired, target-tracking CPU scaling, ASG lifecycle hooks for draining and warming, ALB/NLB target group registration, and optional warm pool for reduced scale-out latency.

## Usage

```hcl
module "asg" {
  source  = "hcassc.jfrog.io/iac-terraform-modules-virtual/compute/ec2-autoscaling/aws"
  version = "0.1.0"

  name                     = "prod-app-asg"
  ami_id                   = data.aws_ami.app.id
  instance_type            = "m5.large"
  security_group_ids       = [module.security_groups.ids["app"]]
  vpc_zone_identifier      = module.vpc.private_subnet_id_list
  iam_instance_profile_arn = module.iam_role.instance_profile_arn
  target_group_arns        = [module.load_balancer.target_group_arns["app"]]
  max_size                 = 10
}
```

<!-- BEGIN_TF_DOCS — DO NOT EDIT BELOW THIS LINE -->
## Requirements

| Name | Version |
| ---- | ------- |
| <a name="requirement_terraform"></a> [terraform](#requirement_terraform) | ~> 1.5 |
| <a name="requirement_aws"></a> [aws](#requirement_aws) | ~> 6.50 |

## Providers

| Name | Version |
| ---- | ------- |
| <a name="provider_aws"></a> [aws](#provider_aws) | 6.50.0 |

## Resources

| Name | Type |
| ---- | ---- |
| [aws_autoscaling_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_group) | resource |
| [aws_autoscaling_lifecycle_hook.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_lifecycle_hook) | resource |
| [aws_autoscaling_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_policy) | resource |
| [aws_launch_template.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_template) | resource |

## Inputs

| Name | Description | Type | Default | Required |
| ---- | ----------- | ---- | ------- | :------: |
| <a name="input_ami_id"></a> [ami_id](#input_ami_id) | AMI ID for the launch template | `string` | n/a | yes |
| <a name="input_instance_type"></a> [instance_type](#input_instance_type) | EC2 instance type | `string` | n/a | yes |
| <a name="input_name"></a> [name](#input_name) | Name prefix for the ASG and launch template | `string` | n/a | yes |
| <a name="input_security_group_ids"></a> [security_group_ids](#input_security_group_ids) | Security group IDs for launch template | `list(string)` | n/a | yes |
| <a name="input_vpc_zone_identifier"></a> [vpc_zone_identifier](#input_vpc_zone_identifier) | List of subnet IDs for the ASG | `list(string)` | n/a | yes |
| <a name="input_desired_capacity"></a> [desired_capacity](#input_desired_capacity) | Desired number of instances | `number` | `1` | no |
| <a name="input_health_check_type"></a> [health_check_type](#input_health_check_type) | EC2 or ELB health check type | `string` | `"EC2"` | no |
| <a name="input_iam_instance_profile_arn"></a> [iam_instance_profile_arn](#input_iam_instance_profile_arn) | ARN of the IAM instance profile | `string` | `null` | no |
| <a name="input_key_name"></a> [key_name](#input_key_name) | EC2 key pair name | `string` | `null` | no |
| <a name="input_lifecycle_hooks"></a> [lifecycle_hooks](#input_lifecycle_hooks) | Map of lifecycle hooks | <pre>map(object({<br/>    lifecycle_transition = string<br/>    heartbeat_timeout    = optional(number, 60)<br/>    default_result       = optional(string, "CONTINUE")<br/>    notification_arn     = optional(string, null)<br/>  }))</pre> | `{}` | no |
| <a name="input_max_size"></a> [max_size](#input_max_size) | Maximum number of instances | `number` | `3` | no |
| <a name="input_metadata_http_tokens"></a> [metadata_http_tokens](#input_metadata_http_tokens) | IMDSv2 setting — required or optional | `string` | `"required"` | no |
| <a name="input_min_size"></a> [min_size](#input_min_size) | Minimum number of instances | `number` | `1` | no |
| <a name="input_root_volume_kms_key_id"></a> [root_volume_kms_key_id](#input_root_volume_kms_key_id) | KMS key ID for root volume encryption | `string` | `null` | no |
| <a name="input_root_volume_size"></a> [root_volume_size](#input_root_volume_size) | Root EBS volume size in GiB | `number` | `20` | no |
| <a name="input_scaling_policies"></a> [scaling_policies](#input_scaling_policies) | Map of target-tracking scaling policies | <pre>map(object({<br/>    predefined_metric_type = optional(string, "ASGAverageCPUUtilization")<br/>    target_value           = optional(number, 60)<br/>    cooldown               = optional(number, 300)<br/>  }))</pre> | `{}` | no |
| <a name="input_tags"></a> [tags](#input_tags) | Resource tags to apply to all resources | `map(string)` | `{}` | no |
| <a name="input_target_group_arns"></a> [target_group_arns](#input_target_group_arns) | ALB/NLB target group ARNs to associate | `list(string)` | `[]` | no |
| <a name="input_user_data"></a> [user_data](#input_user_data) | Base64-encoded user data | `string` | `null` | no |
| <a name="input_warm_pool_enabled"></a> [warm_pool_enabled](#input_warm_pool_enabled) | Enable warm pool for faster scale-out | `bool` | `false` | no |
| <a name="input_warm_pool_min_size"></a> [warm_pool_min_size](#input_warm_pool_min_size) | Minimum number of instances in the warm pool | `number` | `1` | no |

## Outputs

| Name | Description |
| ---- | ----------- |
| <a name="output_asg_arn"></a> [asg_arn](#output_asg_arn) | Auto Scaling Group ARN |
| <a name="output_asg_id"></a> [asg_id](#output_asg_id) | Auto Scaling Group name (ID) |
| <a name="output_launch_template_arn"></a> [launch_template_arn](#output_launch_template_arn) | Launch template ARN |
| <a name="output_launch_template_id"></a> [launch_template_id](#output_launch_template_id) | Launch template ID |
| <a name="output_launch_template_latest_version"></a> [launch_template_latest_version](#output_launch_template_latest_version) | Latest launch template version number |
<!-- END_TF_DOCS — DO NOT EDIT ABOVE THIS LINE -->
