File tree Expand file tree Collapse file tree 5 files changed +34
-21
lines changed Expand file tree Collapse file tree 5 files changed +34
-21
lines changed Original file line number Diff line number Diff line change @@ -7,10 +7,31 @@ resource "aws_autoscaling_group" "main" {
7
7
desired_capacity = 1
8
8
health_check_type = " EC2"
9
9
vpc_zone_identifier = [var . subnet_id ]
10
+ capacity_rebalance = var. use_spot_instances && length (var. instance_types ) > 1 ? true : false
10
11
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
+ }
14
35
}
15
36
16
37
dynamic "tag" {
Original file line number Diff line number Diff line change @@ -35,7 +35,7 @@ resource "aws_launch_template" "main" {
35
35
# checkov:skip=CKV_AWS_88:NAT instances must have a public IP.
36
36
name = var. name
37
37
image_id = local. ami_id
38
- instance_type = var. instance_type
38
+ instance_type = length ( var. instance_types ) > 0 ? var . instance_types [ 0 ] : null
39
39
key_name = var. ssh_key_name
40
40
41
41
block_device_mappings {
@@ -60,14 +60,6 @@ resource "aws_launch_template" "main" {
60
60
security_groups = local. security_groups
61
61
}
62
62
63
- dynamic "instance_market_options" {
64
- for_each = var. use_spot_instances ? [" x" ] : []
65
-
66
- content {
67
- market_type = " spot"
68
- }
69
- }
70
-
71
63
dynamic "tag_specifications" {
72
64
for_each = [" instance" , " network-interface" , " volume" ]
73
65
Original file line number Diff line number Diff line change 1
1
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 ] ))
3
3
ami_id = var. ami_id != null ? var. ami_id : data. aws_ami . main [0 ]. id
4
4
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
5
5
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" {
80
80
METRICS_NAMESPACE = var.cloudwatch_agent_configuration.namespace
81
81
METRICS_ENDPOINT_OVERRIDE = var.cloudwatch_agent_configuration.endpoint_override
82
82
})
83
- }
83
+ }
Original file line number Diff line number Diff line change @@ -28,9 +28,9 @@ output "ha_mode" {
28
28
value = var. ha_mode
29
29
}
30
30
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 ]
34
34
}
35
35
36
36
output "ami_id" {
Original file line number Diff line number Diff line change @@ -55,10 +55,10 @@ variable "ha_mode" {
55
55
default = true
56
56
}
57
57
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 " ]
62
62
}
63
63
64
64
variable "ami_id" {
You can’t perform that action at this time.
0 commit comments