From adb3db7945c5542090d8f483330d97ad47d5445b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix-Antoine=20Fortin?= Date: Fri, 15 Nov 2024 11:40:05 -0500 Subject: [PATCH] Replace legacy spot api by instance_market_options block --- aws/infrastructure.tf | 52 +++++++++---------------------------------- 1 file changed, 11 insertions(+), 41 deletions(-) diff --git a/aws/infrastructure.tf b/aws/infrastructure.tf index ab1201dc..3249077b 100644 --- a/aws/infrastructure.tf +++ b/aws/infrastructure.tf @@ -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]) @@ -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" {