Issue
I want to create spot instances on a condition from the .tfvars file. Otherwise I want a regular ec2 instance. I know there's a ternary condition, but unsure how to properly use it.
spot = var.spot_instance ? instance_market_options {
market_type = "spot"
} :
resource "aws_launch_template" "foo" {
name = "foo"
iam_instance_profile {
name = "test"
}
image_id = "ami-test"
instance_market_options {
market_type = "spot"
}
instance_type = "t2.micro"
}
Or could it be this?
instance_market_options = var.spot_instance ? {
market_type = "spot"
} :
Solution
Since the code snippet does not seem like it's whole, this should look something like the following:
resource "aws_launch_template" "foo" {
name = "foo"
iam_instance_profile {
name = "test"
}
image_id = "ami-test"
dynamic "instance_market_options" {
for_each = var.spot_instance ? [1] : []
content {
market_type = "spot"
}
}
instance_type = "t2.micro"
}
This is using a combination of for_each
meta-argument and dynamic
block. Additionally, you cannot use any conditions in the *.tfvars
files.
Answered By - Marko E Answer Checked By - Marilyn (WPSolving Volunteer)