Preconditions and Postconditions
Input validation stops resource creation in the case of invalid inputs if you run the configuration script, which means the validation takes place before the terraform
apply
operation.
But if you want a similar condition(s) to be applied to resources, data sources, or outputs and the evaluation has to be done before and after, based on the scenario, Terraform has precondition
and postcondition
to explore.
For resources and data sources, the precondition
and postcondition
checks need to be added under the lifecycle {}
block. The outputs can contain a precondition
check only without the lifecycle {}
block. The precondition
and postcondition
check will contain the condition and the error message parameters, such as the one you saw with the input validation.
Look at the following code snippet:
data "aws_ecr_repository" "repo_data" {
name = aws_ecr_repository.app_repo.name
lifecycle {
...