terraformgitlabpipelinecd

Terraform cannot re-create/delete resources in a module, because they're used by dependent modules through terraform_remote_state?


I have two Terraform modules - X and Y - where Y depends on X through terraform_remote_state data source.

X is deployed before Y in the pipeline, which means that the pipeline fails whenever X has changes that cause resources to be re-created and/or deleted. In this case, the resource in X cannot be deleted as it's used by some resource in Y.

How is this problem normally solved? Is it an anti-pattern to use terraform_remote_state in the first place?


Solution

  • When splitting the declaration of infrastructure over different configurations that are independently applyable, it's important to choose the division points carefully so that the different configurations really are independently applyable. Terraform only works with one configuration at a time, so it cannot automatically resolve changes that would require simultaneous changes in two different configurations.

    This is not inherently a problem with terraform_remote_state -- the same problem could arise with any use of data blocks to depend on objects managed elsewhere. The root problem here seems to be that Y is declared to manage something that should perhaps actually be managed by X, because it needs to immediately change in response to other changes in X.

    Exactly how to resolve that will depend on exactly which infrastructure objects you're using in practice. Reorganizing which objects are managed by which configuration might solve the problem, but it also might introduce another similar problem. Sometimes successfully splitting infrastructure into two independent units requires a different design approach where e.g. there's some third unit that both X and Y depend on, which provides some way for X and Y to successfully collaborate.

    If you ask a new question where you name exactly the resource types you are using and share your configuration then you may get a more actionable answer.