Skip to content

Commit

Permalink
Replace legacy spot api by instance_market_options block
Browse files Browse the repository at this point in the history
  • Loading branch information
cmd-ntrf committed Nov 15, 2024
1 parent c4813e9 commit adb3db7
Showing 1 changed file with 11 additions and 41 deletions.
52 changes: 11 additions & 41 deletions aws/infrastructure.tf
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ data "aws_ec2_instance_type" "instance_type" {
}

resource "aws_instance" "instances" {
for_each = {for key, values in module.design.instances_to_build: key => values if !contains(values["tags"], "spot")}
for_each = module.design.instances_to_build
instance_type = each.value.type
ami = lookup(each.value, "image", var.image)
user_data = base64gzip(module.configuration.user_data[each.key])
Expand Down Expand Up @@ -113,49 +113,19 @@ resource "aws_instance" "instances" {
]
}

depends_on = [aws_internet_gateway.gw]
}

resource "aws_spot_instance_request" "spot_instances" {
for_each = {for key, values in module.design.instances_to_build: key => values if contains(values["tags"], "spot")}
instance_type = each.value.type
ami = lookup(each.value, "image", var.image)
user_data = base64gzip(module.configuration.user_data[each.key])
availability_zone = local.availability_zone
placement_group = contains(each.value.tags, "efa") ? aws_placement_group.efa_group.id : null

key_name = aws_key_pair.key.key_name

network_interface {
network_interface_id = aws_network_interface.nic[each.key].id
device_index = 0
}

ebs_optimized = true
root_block_device {
volume_type = lookup(each.value, "disk_type", "gp2")
volume_size = lookup(each.value, "disk_size", 20)
}

tags = {
Name = format("%s-%s", var.cluster_name, each.key)
}

lifecycle {
ignore_changes = [
ami
]
dynamic "instance_market_options" {
for_each = contains(values.tags, "spot") ? [each] : []
iterator = spot
content {
spot_options {
spot_instance_type = lookup(spot.value, "spot_instance_type", "persistent")
instance_interruption_behavior = lookup(spot.value, "instance_interruption_behavior", "stop")
max_price = lookup(spot.value, "max_price", null)
}
}
}

depends_on = [aws_internet_gateway.gw]

# spot specific variables
wait_for_fulfillment = lookup(each.value, "wait_for_fulfillment", true)
spot_type = lookup(each.value, "spot_type", "persistent")
instance_interruption_behavior = lookup(each.value, "instance_interruption_behavior", "stop")
spot_price = lookup(each.value, "spot_price", null)
block_duration_minutes = lookup(each.value, "block_duration_minutes", null)

}

resource "aws_ebs_volume" "volumes" {
Expand Down

0 comments on commit adb3db7

Please sign in to comment.