Home > OS >  How to move terraform resource in terraform apply
How to move terraform resource in terraform apply

Time:04-07

I have custom resource defined in my terraform module:

resource "aws_alb_target_group" "whatever" 
{
   ....
}

Turns out whatever is not good name, and I need to update it.

Classic way of doing it would be to login onto each environment and execute terraform state mv, however I have lots of environments, and no automation for such action.

How I can change name of resource without manually moving state (only through editing terraform modules and applying plans)?

CodePudding user response:

Based on the explanation in the question, I guess your best bet would be to use the moved block [1]. So for example, in your case that would be:

resource "aws_alb_target_group" "whatever" 
{
   ....
}

moved {
  from = aws_alb_target_group.whatever
  to   = aws_alb_target_group.a_much_better_whatever
}

EDIT: As @Matt Schuchard noted, the moved block is available only for Terraform versions >=1.1.0.


[1] https://www.terraform.io/language/modules/develop/refactoring#moved-block-syntax

  • Related