There is one resource which don't have pre-defined resource. So as as workaround, we preferred to used "null-resource".
Let's discuss with a example (Please don't stick with this example. Try to get the nature of requirement. Played a lot with conditional expression) -
Based on the terraform ACTION value (apply, destroy etc), do the below activity. Enlighten here if more inbuilt Terraform environment variables are available that can provide more flexibility in automation.
locals {
action = "${TF_CLI_ARGS}" == "apply" ? "install" : ("${TF_CLI_ARGS}" == "destroy" ? "remove" : "update")
package = "httpd"
version = "2.4.6"
}
resource "null_resource" "test" {
triggers = {
action = "${local.action}"
}
triggers = {
action = "${local.version}"
}
provisioner "local-exec" {
command = "yum -y ${local.action} ${package}-${version}"
}
}
Case 1: Install
Install the given package if apply
is in command line.
Case 2: Delete
Uninstall the given package if destroy
is in command line.
Case 3: Update
Update the given package based on delta in version number.