Skip to content

Commit bd56015

Browse files
committed
feat: added support for deploying instances into multiple instance types
1 parent ab18a6d commit bd56015

File tree

5 files changed

+34
-21
lines changed

5 files changed

+34
-21
lines changed

asg.tf

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,31 @@ resource "aws_autoscaling_group" "main" {
77
desired_capacity = 1
88
health_check_type = "EC2"
99
vpc_zone_identifier = [var.subnet_id]
10+
capacity_rebalance = var.use_spot_instances && length(var.instance_types) > 1 ? true : false
1011

11-
launch_template {
12-
id = aws_launch_template.main.id
13-
version = "$Latest"
12+
dynamic "mixed_instances_policy" {
13+
for_each = length(var.instance_types) > 0 ? [1] : []
14+
content {
15+
instances_distribution {
16+
on_demand_base_capacity = var.use_spot_instances ? 0 : 1
17+
on_demand_percentage_above_base_capacity = var.use_spot_instances ? 0 : 100
18+
spot_allocation_strategy = "price-capacity-optimized"
19+
}
20+
21+
launch_template {
22+
launch_template_specification {
23+
launch_template_id = aws_launch_template.main.id
24+
version = "$Latest"
25+
}
26+
27+
dynamic "override" {
28+
for_each = var.instance_types
29+
content {
30+
instance_type = override.value
31+
}
32+
}
33+
}
34+
}
1435
}
1536

1637
dynamic "tag" {

ec2.tf

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ resource "aws_launch_template" "main" {
3535
#checkov:skip=CKV_AWS_88:NAT instances must have a public IP.
3636
name = var.name
3737
image_id = local.ami_id
38-
instance_type = var.instance_type
38+
instance_type = length(var.instance_types) > 0 ? var.instance_types[0] : null
3939
key_name = var.ssh_key_name
4040

4141
block_device_mappings {
@@ -60,14 +60,6 @@ resource "aws_launch_template" "main" {
6060
security_groups = local.security_groups
6161
}
6262

63-
dynamic "instance_market_options" {
64-
for_each = var.use_spot_instances ? ["x"] : []
65-
66-
content {
67-
market_type = "spot"
68-
}
69-
}
70-
7163
dynamic "tag_specifications" {
7264
for_each = ["instance", "network-interface", "volume"]
7365

main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
locals {
2-
is_arm = can(regex("[a-zA-Z]+\\d+g[a-z]*\\..+", var.instance_type))
2+
is_arm = can(regex("[a-zA-Z]+\\d+g[a-z]*\\..+", var.instance_types[0]))
33
ami_id = var.ami_id != null ? var.ami_id : data.aws_ami.main[0].id
44
cwagent_param_arn = var.use_cloudwatch_agent ? var.cloudwatch_agent_configuration_param_arn != null ? var.cloudwatch_agent_configuration_param_arn : aws_ssm_parameter.cloudwatch_agent_config[0].arn : null
55
cwagent_param_name = var.use_cloudwatch_agent ? var.cloudwatch_agent_configuration_param_arn != null ? split("/", data.aws_arn.ssm_param[0].resource)[1] : aws_ssm_parameter.cloudwatch_agent_config[0].name : null
@@ -80,4 +80,4 @@ resource "aws_ssm_parameter" "cloudwatch_agent_config" {
8080
METRICS_NAMESPACE = var.cloudwatch_agent_configuration.namespace
8181
METRICS_ENDPOINT_OVERRIDE = var.cloudwatch_agent_configuration.endpoint_override
8282
})
83-
}
83+
}

output.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ output "ha_mode" {
2828
value = var.ha_mode
2929
}
3030

31-
output "instance_type" {
32-
description = "Instance type used for the fck-nat instance"
33-
value = aws_launch_template.main.instance_type
31+
output "instance_types" {
32+
description = "Instance types used for the fck-nat instance (from the ASG mixed instances policy or launch template default)"
33+
value = length(var.instance_types) > 0 ? var.instance_types : [aws_launch_template.main.instance_type]
3434
}
3535

3636
output "ami_id" {

variables.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ variable "ha_mode" {
5555
default = true
5656
}
5757

58-
variable "instance_type" {
59-
description = "Instance type to use for the NAT instance"
60-
type = string
61-
default = "t4g.micro"
58+
variable "instance_types" {
59+
description = "List of instance types to use for the NAT instance (ASG mixed instances policy)"
60+
type = list(string)
61+
default = ["t4g.nano"]
6262
}
6363

6464
variable "ami_id" {

0 commit comments

Comments
 (0)