Terraform Custom Condition(precondition, postcondition)

235 단어·1 분·원문(.md)

Terraform provides custom conditions to ensure that Terraform behaves as intended.

For Terraform to operate as intended by the code author, it proceeds if the condition is met and raises an error if it is not.

This shows that Terraform's custom conditions differ from ternary operators (regarding error generation upon unmet conditions)

precondition, postcondition #

Both are among Terraform's custom conditions.

Precondition/postcondition inspect block fields during the block lifecycle stage.

The condition determines the conditional statement, and if an error occurs, the specified error message is displayed.

The difference between pre and post conditions is that they execute before/after Terraform code execution.

If neither condition is met, Terraform raises an error.

  1. If there are loops like forEach or count, precondition checks each custom condition individually.
  2. postcondition can access the self object because it performs checks during code execution. (precondition cannot).

I consider precondition and postcondition to be about expectation and assurance.

This is because postcondition prevents other parts from referencing a resource if it is incorrectly created.

Can postcondition be checked via terraform apply? The answer is yes.

We set a condition to check if base64sha256 is 1111, as shown below.

If we run apply, an error occurs as expected (because it's not 1111).

Although the custom condition failed, the txt file was created. The significance lies in preventing external references to that resource.

[[Terraform 작동원리]]

DevOps/hcl/condition.md