Skip to content

Commit

Permalink
feat: allow mixture of on-demand and spot instances (#140)
Browse files Browse the repository at this point in the history
# Description

Added the possibility of choosing the preferred balance to mix on-demand
and Spot instances on the `on_spot` aws_autoscaling_group resource. As
spot instances might be stopped without further notice, it might be
useful to deploy at least one on-demand instance.

Use the new variable `instances_distribution` to customize the minimum
number of on_demand instances and the percentage of on_demand instances
(excluding the on_demand base capacity).

```hcl
module "bastion" {
  ...
  instances_distribution = {
    on_demand_base_capacity                  = 1
    on_demand_percentage_above_base_capacity = 20
    spot_allocation_strategy                 = "lowest-price"
  }
}
```

# Verification

- [x] simple example applied, still running
- [x] mixture of 3:1 (spot:on-demand) instances (4 in total). Both
instances are created and I am able to connect.

# Checklist

- [x] My code follows the style guidelines of the project
- [x] I have performed a self-review of my own code
- [x] I have made corresponding changes to the documentation

---------

Co-authored-by: kayma <kayma@hlag.com>
Co-authored-by: Matthias Kay <github-public@matthiaskay.de>
  • Loading branch information
3 people committed Jan 31, 2023
1 parent 6ff775b commit 6df557a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
7 changes: 5 additions & 2 deletions autoscaling.tf
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,13 @@ resource "aws_autoscaling_group" "on_spot" {

termination_policies = ["OldestInstance"]

capacity_rebalance = true # https://docs.aws.amazon.com/autoscaling/ec2/userguide/ec2-auto-scaling-capacity-rebalancing.html

mixed_instances_policy {
instances_distribution {
on_demand_percentage_above_base_capacity = 0
on_demand_base_capacity = 0
on_demand_percentage_above_base_capacity = var.instances_distribution.on_demand_percentage_above_base_capacity
on_demand_base_capacity = var.instances_distribution.on_demand_base_capacity
spot_allocation_strategy = var.instances_distribution.spot_allocation_strategy
}

launch_template {
Expand Down
6 changes: 6 additions & 0 deletions examples/spot/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ module "bastion_host" {
enable_spot = true
}

instances_distribution = {
on_demand_base_capacity = 1
on_demand_percentage_above_base_capacity = 0
spot_allocation_strategy = "lowest-price"
}

vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.private_subnets

Expand Down
18 changes: 17 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ variable "instance" {
}
}

variable "instances_distribution" {
type = object({
on_demand_base_capacity = number # absolute minimum amount of on_demand instances
on_demand_percentage_above_base_capacity = number # percentage split between on-demand and Spot instances
spot_allocation_strategy = string
})

description = "Defines the parameters for mixed instances policy auto scaling"

default = {
on_demand_base_capacity = 0
on_demand_percentage_above_base_capacity = 0
spot_allocation_strategy = "lowest-price"
}
}

variable "tags" {
type = map(string)
description = "A list of tags to add to all resources."
Expand All @@ -100,4 +116,4 @@ variable "tags" {
variable "ami_name_filter" {
description = "The search filter string for the bastion AMI."
default = "amzn2-ami-hvm-*-x86_64-ebs"
}
}

0 comments on commit 6df557a

Please sign in to comment.