Avoid random suffix for costruct resources

Hey,

CDKTF adds random suffixes to resources inside constructs.
For example, if we have a KubernetesCluster construct, with a backend-cluster id, it will add a suffix to it:

module.kubernetes-cluster_backend-cluster_**CBB7574D**

When we change the name of the construct, or refactor constructs, we would like to move the terraform state to the new state to avoid recreating resources.

For example, rename KubernetesCluster to KubeCluster:

terraform state mv module.kubernetes-cluster module.kube-cluster

However, because the random ids change, the state will break, requiring all resources under the construct to be recreated.

From the docs, constructs are suffixed with a hash. I’m not sure why this is necessary, since constructs require a unique id anyway.


Question

Is there a way to avoid these random ids, or make them deterministic?


Thanks,

Koby

That part of the id isn’t random. It is a hash of the path to the resource within the construct tree.

You can use overrideLogicalId to explicitly set for particular resources. You could potentially override stack.allocateLogicalId to change the id generation strategy, but that is pretty untested.
If you are willing to deal with unique ids yourself, you can also put everything at the root stack scope. There is a special case to remove the hash at that level.

Construct ids only need to be defined unique within their scope. The hash ensures global uniqueness is met.

Resource Refactoring - Moved State · Issue #1292 · hashicorp/terraform-cdk · GitHub is a general issue for refactoring if you’d like to add any more thoughts.

1 Like