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


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
file is missing
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?

1 Like

@obones have you found a way to address this?

No, I tried various things but after a while I abandoned the idea.
As I find it more important that a second apply does not show any changes, I am back to square one where I must make sure the downloaded files have not been deleted behind my back.

I hear you but what about a scenario where you sharing a remote state and running the apply or plan on a CI/CD pipeline node?

I totally understand and apart from using a timestamp that forces the null_resource to run each and every time, I cannot see any other way to do this.
Unless the is_first_apply function gets implemented one day.

Or maybe via a variable that never sees its value changed. It would be interesting to see what is done when a variable with default value is first used. Maybe there is a null → value change the first time then no changes ever.