Update value if variable is not null

We’re using a CI/CD pipeline (when necessary) to restore a DB from a snapshot, this sets an environment variable with a snapshot ID, and then recreates the resource to be based off of that Snapshot.

However if we run the pipeline to deploy/update the rest of the infrastructure, this variable will not be set and will be null, thus recreating the DB form scratch loosing all the data.

I’ve tried using the lifecycle meta-argument with the ignore_changes argument, but this ignores all changes, including a future attempt to restore from snapshot.

I’ve considered storing the Snapshot ID in an SSM Parameter, fetching it from there, and update it using AWS CLI (from the pipeline or from the local-exec, but this creates difficult circular dependencies.

Is there any way I can ignore_changes if a variable is null or empty but don’t ignore it if it has any value?

Hey @ovidiu.bogdan , I’m from the Terraform product team, and would love to chat more around this use case.

We are exploring a couple of new initiatives to try to address a similar set of challenges.

Would you be open to chatting? Feel free to email me at oismail@hashicorp.com or schedule a time on my calendar Calendly - Omar Ismail

Is there any blocker from having this conversation publicly?

There is no blocker from having it publicly. The live conversation would’ve been easier to dig into the workflow. Will follow up here with more questions

To be clear, Terraform doesn’t really have any capability to handle these sort of edge-triggered event right now, so this would be a discussion about what details might be needed for your particular workflow.

Changes in Terraform require comparison to a stored previous state, but only providing a value at the time you want replacement doesn’t give you anything with which to compare. Currently the logic would need to be implemented externally, either by using the -replace flag, or storing that value in a way Terraform can read it consistently.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.