How do you create a resource conditionally?
BeginnerConceptLoops, Conditionals, Dynamic Blocks and Functions
Answer
Use count with a boolean expression (or for_each with an empty map).
resource "aws_eip" "nat" {
count = var.enable_nat ? 1 : 0
}
# reference: one(aws_eip.nat[*].id)
More on Loops, Conditionals, Dynamic Blocks and Functions
How does `count` work?BeginnerHow does `for_each` work?BeginnerWhen should you choose `for_each` over `count`?IntermediateHow do you use `for_each` with a list of objects?IntermediateWhat does "The for_each value depends on resource attributes that cannot be determined until apply" mean?AdvancedWhat is the conditional expression syntax?Beginner