DEV Community

Sree Lasya Vallabhaneni
Sree Lasya Vallabhaneni

Posted on

2

How to move items in terraform state without force replacement

I have started working on terraform very recently, One of the biggest annoying factors was when you have to change the terraform state without a forced replacement of the resource.

Terraform remembers a state which is nothing but how the infrastructure looks in the remote real world and in the local configuration.
So when you change a few attributes in the resource like name of the resource, instances, modules which are already created, in the plan it specifies that as not an in-place change

Thankfully there is a command that my colleague has mentioned is "mv"

This command is nothing but like a Linux mv command.

You need to specify source and destination:

terraform state mv aws_subnet.main aws_subnet-us-west-2c

This will move the existing terraform state to an updated terraform state and when you run a terraform plan, you should not see any infrastructure changes if done correctly.

For more information, you can refer: https://www.terraform.io/docs/commands/state/mv.html

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE