DEV Community

Discussion on: šŸ‘· How to make conditionnal resources in terraform?

Collapse
 
tomharrisonjr profile image
Tom Harrison

One simple solution is:

  1. create a variable, e.g.
variable "make_lambda" {
  type = bool
  default = true
  description = "Make the Lambda resource unless false"
}
Enter fullscreen mode Exit fullscreen mode

Set the variable in a suitable context, then, use count in the resource name with ternary operator:

resource "aws_lambda_function" "my_lambda" {
  count = var.make_lambda ? 1 : 0
  filename = "lambda.jar"
  ... etc
}
Enter fullscreen mode Exit fullscreen mode

The value of count determines how many instances of a resource will be created. While its intent is for things like clusters of machines, in this usage we'll either create 1 or 0 of the resource depending on the value of the boolean.