Terraform Data block dependency

Hello All,

I am facing an issue while working with terraform 12. Issue is like

i am having a data block that is invocating a lambda function that run some queries on the RDS Instance and output of that lambda is being used to create a CW log subscription filter. In the lambda function , we are running some quires that will make sure that the log stream and log groups gets created for the RDS.

Issue is every time i run a terraform plan it says that the lambda invocation data block will be read due to which its destroying the subscription filter and creating a new one (even when no changes are there) with the same name and config.

Need to know if there is a way i can skip that

Hi @rishabhzs1510,

What you’re describing here sounds like the behavior of depends_on for a data resource in Terraform v0.12: it always forces the data resource to be read only during the apply step, because that’s the only way Terraform v0.12 can guarantee that the read happens after all other changes.

If you need to stay on Terraform v0.12 then I would suggest looking for a different solution that avoids using depends_on as part of a data block. For example, you might be able to use implicit dependencies instead of explicit dependencies to get the same ordering, but implicit dependencies work better because Terraform can understand that the dependency is on a specific attribute of another resource rather than on the resource as a whole.

If you’re instead using Terraform v0.12 only temporarily as part of upgrading to newer releases, you could potentially ignore the problem and keep upgrading until you reach Terraform v1.0. Newer versions of Terraform have a different model for refreshing managed resources and reading data sources which allows reading as part of creating a plan, rather than as a separate step before creating a plan.

That means that depends_on for a data resource works differently in modern Terraform: it’ll defer reading the data resource until the apply phase only if it depends on some other resource that has proposed changes in the plan.