Trigger a null_resource the first time then when file no longer exist or variable changed

Hello,

I have a special situation that I worked around by using a null_ressource to run a local script.
This script retrieves files and I would like to avoid retrieving those when they haven’t changed. As I know they only change when the version number changes, I have used the following trigger:

triggers = {
    version = var.app_version
}

Upon the first apply, the version trigger changes from null to the variable value so it triggers the files creation, and it won’t be triggered again until app_version changes which is exactly expected.

However, it may happen that the files are removed before a subsequent apply and this makes my aws_lambda_function resources fail because they refer to those files.

I thus modified my trigger to now become this:

triggers = {
    version = fileexists("${local.lambda_source_file_name}") ? var.app_version : "${timestamp()}"
}

My intent was that if the file is missing, I use a timestamp because it is sure to change. However, this means a rerun of the null_resource on the second apply because its trigger goes into the following states:

  1. null → timestamp
  2. timestamp → var.app_version

After this, it stays stable, until either the file goes missing, or var.app_version changes.

I’m thus trying to express this condition in my trigger:

first apply ever
OR
file is missing
OR
var.app_version has changed

But I can’t seem to figure out how to express the “first apply ever” condition.
I mean, I can create the following triggers for my null_resource:

triggers = {
    version = var.app_version
    fileexists = fileexists("${local.lambda_source_file_name}")
}

But this does not change the situation because fileexists will go from false to true upon the second apply, which is what I’d like to avoid.
I looked around and stumbled across the time_static resource from the time package, but this does not help me with my boolean function going from false to true on the second apply, it’s just pushing the issue in another resource.

I did not find a is_first_apply function in the documentation, do you think there is way for me to express it somehow?