From 773a05a10886964bb0a9f36933d84d94dc3ff7e7 Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Date: Tue, 17 Dec 2024 20:03:47 -0300 Subject: [PATCH] Uses Spot Only When Cost Is Configured --- config.tf | 2 +- instances/config.tf | 2 +- instances/instance.tf | 17 ++++++++++------- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/config.tf b/config.tf index fcd990d..f0e746a 100644 --- a/config.tf +++ b/config.tf @@ -51,7 +51,7 @@ variable "aws_ami" { variable "aws_spot_price" { type = number - default = 0.0028 + default = 0 } variable "cloudflare_api_token" { diff --git a/instances/config.tf b/instances/config.tf index efe2375..6e66d8f 100644 --- a/instances/config.tf +++ b/instances/config.tf @@ -35,7 +35,7 @@ variable "aws_ami" { variable "aws_spot_price" { type = number - default = 0.0028 + default = 0 } variable "ssh_public_key_main" { diff --git a/instances/instance.tf b/instances/instance.tf index ab52fbd..5c8fac8 100644 --- a/instances/instance.tf +++ b/instances/instance.tf @@ -115,13 +115,16 @@ resource "aws_instance" "worker" { instance_type = var.aws_instance_type vpc_security_group_ids = [aws_security_group.project_pool.id] count = var.number_of_workers - - instance_market_options { - market_type = "spot" - spot_options { - max_price = var.aws_spot_price - instance_interruption_behavior = "stop" - spot_instance_type = "persistent" + + dynamic "instance_market_options" { + for_each = var.aws_spot_price != 0 ? [var.aws_spot_price] : [] + content { + market_type = "spot" + spot_options { + max_price = instance_market_options.value + instance_interruption_behavior = "stop" + spot_instance_type = "persistent" + } } }