Moving & Renaming Resources Within a Stack
You may want to rename a deployed resource in a stack, like theS3Bucket in the following example.
cdktn plan, the CLI may display the following output:
moveTo, moveToId, moveFromId) available on the resource.
Performing Resource Moves
ThemoveTo function is available on all resources and is used for relocating a resource to the location specified by the string target moveTarget. To set a resource’s moveTarget, use the addMoveTarget function that is present on the resource to move to. You can specify an arbitrary string for the moveTarget, but it must be unique within your stack.
Move Targets
AmoveTarget is accessible anywhere within the context of the stack where it is created, including the root of the stack and within a nested construct. This workflow does not support moving resources to a different stack.
Enabling foreach on a Resource
To incorporate a deployed resource into a foreach composition without destroying the resource, specify an index as a second argument in the moveTo function. The index should correspond to the key in the TerraformIterator named iterator.
foreach composition without destroying the existing deployed resource.
Move By Resource Address
In instances where the move target workflow does not easily fit your use case, resource moves can be performed by directly specifying the full resource address to be moved to/from.Move To
Move From
Nested Constructs
Resource addresses in the context of nested constructs will not simply be the specified id given to the resource. When dealing with moving resources by their address to/from nested constructs, runcdktn synth and refer to cdktf.out/stacks/'your stack name' to find the exact address to move to/from.
Moving or renaming modules
To change the id of a module without losing its state use theterraform state mv command. This command needs to be run in the output directory of the stack that contains the module. Commonly this is cdktf.out/stacks/<stack-name>. The command takes two arguments, the first is the current id of the module, the second is the new id of the module.
terraform state mv command refer to the Terraform documentation.
Moving Resources From One Stack To Another
You may want to move a resource from one stack to another, like the following example awsS3Bucket resource.
cdktn deploy '*', the CLI displays the following output.
terraform import and terraform state rm to move the resource’s state. First we need to find the Terraform name of the Resource in the old stack.
terraform state show command to find it. The id is depenendent on the resource being imported, see the import docs for more details. We need to run these commands in the directory of the stack we are importing the resource from.
"my-bucket20221208141249058600000001") we can import it. This command needs to be run in the directory of the stack we are importing the resource to.
cdktn deploy '*' again. We should now see no changes.
Why Do I Need To Find The Terraform Name Of The Resource?
If you use Constructs to organize your code, you might have a level between the generated provider constructs and theTerraformStack construct to organize your code.
This extra level adds a prefix to the name of the resource to ensure uniqueness. Refer to Constructs for more details.