Merging two states involves moving resources from one to the other using using
terraform state mv [options] SOURCE DESTINATION https://www.terraform.io/docs/commands/state/mv.html.
terraform state pull > source.tfstateand
terraform state pull > destination.tfstateif the configuration has an appropriate backend configuration. If not, the state files can be downloaded directly from their backends (i.e. S3 bucket or Terraform Enterprise workspace) https://www.terraform.io/docs/commands/state/pull.html.
destination.tfstateas the final state. I will move resources from
terraform state list -state=destination.tfstate > destination-resources.txt. Use
terraform state list -state=source.tfstate > source-resources.txtto view the existing resource in the source state. Note that if you wish for all resources in both states to be present in the merged/end state, all of the resource addresses must be unique https://www.terraform.io/docs/commands/state/list.html.
aws_instance.foo) and top level module (e.g.
module.bar) listed for
source.tfstate, perform a
terraform state mvto move the resource from the source to the destination.
module.baz.module.qux) should not need to be moved, as they will be moved along with the top level modules.
Example: Using local backend, add options
terraform state mv -state=destination.tfstate -state-out=source.tfstate aws_instance.foo aws_instance.foo
terraform state mv -state=destination.tfstate -state-out=source.tfstate module.bar module.bar
terraform state list -state=source.tfstate. All resources should now exist in the destination state, which can be checked similarly with
terraform state list -state=destination.tfstate.
destination.tfstatein a text editor and increment the "serial" value by
terraform state push, or (3) use the Terraform Cloud / Enterprise API to create a new state version.
# In the directory where the combined configuration has been initialized using `terraform init`
terraform state push path/to/destination.tfstate
terraform state listto view the state list information from the state in the backend, which was just pushed. Run
terraform planto see the results and ensure that they are as expected. If the configuration was combined and the states were combined, this should likely show no changes.