diff --git a/README.md b/README.md index 7ed2a5c..94aa9ae 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ module "fck-nat" { | Name | Type | |------|------| | [aws_autoscaling_group.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_group) | resource | +| [aws_autoscaling_lifecycle_hook.spot_termination_wait](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_lifecycle_hook) | resource | | [aws_iam_instance_profile.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_instance_profile) | resource | | [aws_iam_role.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource | | [aws_instance.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance) | resource | @@ -76,6 +77,7 @@ module "fck-nat" { | [ebs\_root\_volume\_size](#input\_ebs\_root\_volume\_size) | Size of the EBS root volume in GB | `number` | `8` | no | | [eip\_allocation\_ids](#input\_eip\_allocation\_ids) | EIP allocation IDs to use for the NAT instance. Automatically assign a public IP if none is provided. Note: Currently only supports at most one EIP allocation. | `list(string)` | `[]` | no | | [encryption](#input\_encryption) | Whether or not to encrypt the EBS volume | `bool` | `true` | no | +| [ha\_additional\_instance\_types](#input\_ha\_additional\_instance\_types) | Additional instance types used by autoscaling rebalancing when the primary instance is unavailable | `list(string)` |
[
"t4g.small"
]
| no | | [ha\_mode](#input\_ha\_mode) | Whether or not high-availability mode should be enabled via autoscaling group | `bool` | `true` | no | | [instance\_type](#input\_instance\_type) | Instance type to use for the NAT instance | `string` | `"t4g.micro"` | no | | [kms\_key\_id](#input\_kms\_key\_id) | Will use the provided KMS key ID to encrypt the EBS volume. Uses the default KMS key if none provided | `string` | `null` | no | @@ -116,4 +118,4 @@ module "fck-nat" { | [security\_group\_id](#output\_security\_group\_id) | Deprecated. The ID of the security group used by fck-nat ENIs | | [security\_group\_ids](#output\_security\_group\_ids) | List of security group IDs used by fck-nat ENIs | | [subnet\_id](#output\_subnet\_id) | Subnet ID to which the fck-nat instance is deployed into | -| [vpc\_id](#output\_vpc\_id) | VPC ID to which the fck-nat instance is deployed into | \ No newline at end of file +| [vpc\_id](#output\_vpc\_id) | VPC ID to which the fck-nat instance is deployed into | diff --git a/asg.tf b/asg.tf index 1cb53b9..b085025 100644 --- a/asg.tf +++ b/asg.tf @@ -8,9 +8,31 @@ resource "aws_autoscaling_group" "main" { health_check_type = "EC2" vpc_zone_identifier = [var.subnet_id] - launch_template { - id = aws_launch_template.main.id - version = "$Latest" + capacity_rebalance = var.use_spot_instances + + mixed_instances_policy { + instances_distribution { + on_demand_percentage_above_base_capacity = var.use_spot_instances ? 0 : 100 + spot_allocation_strategy = "price-capacity-optimized" + } + launch_template { + launch_template_specification { + launch_template_id = aws_launch_template.main.id + version = aws_launch_template.main.latest_version + } + + override { + instance_type = var.instance_type + } + + dynamic "override" { + for_each = toset(var.ha_additional_instance_types) + + content { + instance_type = override.value + } + } + } } dynamic "tag" { @@ -60,3 +82,12 @@ resource "aws_autoscaling_group" "main" { delete = "15m" } } + +resource "aws_autoscaling_lifecycle_hook" "spot_termination_wait" { + count = var.ha_mode && var.use_spot_instances ? 1 : 0 + + name = "TerminationWait" + autoscaling_group_name = aws_autoscaling_group.main[0].name + heartbeat_timeout = 300 + lifecycle_transition = "autoscaling:EC2_INSTANCE_TERMINATING" +} diff --git a/ec2.tf b/ec2.tf index 66b42ab..beba8f4 100644 --- a/ec2.tf +++ b/ec2.tf @@ -61,7 +61,7 @@ resource "aws_launch_template" "main" { } dynamic "instance_market_options" { - for_each = var.use_spot_instances ? ["x"] : [] + for_each = var.use_spot_instances && !var.ha_mode ? ["x"] : [] content { market_type = "spot" @@ -100,7 +100,7 @@ resource "aws_instance" "main" { launch_template { id = aws_launch_template.main.id - version = "$Latest" + version = aws_launch_template.main.latest_version } tags = var.tags diff --git a/variables.tf b/variables.tf index ccb61ca..910f915 100644 --- a/variables.tf +++ b/variables.tf @@ -55,6 +55,12 @@ variable "ha_mode" { default = true } +variable "ha_additional_instance_types" { + description = "Additional instance types used by autoscaling rebalancing when the primary instance is unavailable" + type = list(string) + default = ["t4g.small"] +} + variable "instance_type" { description = "Instance type to use for the NAT instance" type = string