Terraform está muy bien pero hay cosas que me cuestan siempre de recordar. Y esta es una muy simple seguramente pero no hay manera de que recuerde bien como funciona en general.
Queremos crear un recurso siempre y cuando, le pasemos un parámetro.
In Terraform, a boolean true is converted to a 1 and a boolean false is converted to a 0.
Supongamos este ejemplo. Queremos crear una Elastic IP solo cuando un valor esté a TRUE (1).
variable "create_eip" {
description = "If set to true, create an EIP for the microservice"
}
resource "aws_eip" "example" {
count = "${var.create_eip ? 1 : 0}"
instance = "${aws_instance.example.id}"
}
el recurso será creado solo si, usando el HCL sintaxis ternaria condicional, var.create_eip sea “true” o 1. Si por contra var.create_eip es “false” o 0, el recurso on se creará.
CONDITION ? TRUEVAL : FALSEVAL
“The condition can be any valid interpolation syntax.” En nuestro caso, la variable var.create_eip
“The true and false value can also be any valid interpolation syntax. ”
“The returned types by the true and false side must be the same.”
Links