'How to run only delta changes in Terraform for new workspace

I'm using terraform in a CI/CD pipeline.

  • When I open a pull request, I create a workspace named after the feature branch.
  • Terraform creates all the resources with the workspace name attached to them so as to be unique.
  • Once this is deployed to prod there is a cleanup step that destroys everything created by that workspace.

This works fine but recreating all the resources for every pull request soon will be unfeasible. Is there a way in Terraform to defer to the prod tfstate file so to plan only for the delta from this and their dependencies?

A simple example:

my prod tfstate has these resources

  • 1 database(dbA)
  • 2 schemas(dbA.schemaA,dbA.schemaB)
  • 1 table (dbA.schemaA.tableA).

If I add one table(dbA.schemaB.tableB) in dbA.schemaB, I'd want terraform to plan for

  • dbA
  • dbA.schemaB
  • dbA.schemaB.tableB

and not for

  • dbA.schemaA
  • dbA.schemaA.tableA


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source