I am new to cdktf and I am using it as a tool to loop through large amounts of resource IDs to generate import blocks in HCL.
I am coming across this error below when using cdktf synth --hcl
RuntimeError: Error: There is already a Construct with name '<NAME>' in TerraformStack [cdktf]
I think I understand that this error is related to when I use the method .generate_config_for_import(self, import_to_id=<NAME>, import_from_id=<ID>). This creates multiple different TF resources using the same TF resource name in the generated import blocks.
I think this is related to the link below, but for import blocks.
import {
to = azurerm_mssql_virtual_machine.<NAME1>
}
import {
to = azurerm_virtual_machine.<NAME1>
}
I have attempted to look at the solution for this which was to use the method .override_logical_id() but that did not seem to work.
Here is some example code of where I am seeing this issue
virtualMachines = {
"<RESOURCE_ID>": "<NAME1>"
}
SqlVirtualMachines = {
"<RESOURCE_ID>": "<NAME1>"
}
class MyStack(TerraformStack):
def __init__(self, scope: Construct, id: str):
super().__init__(scope, id)
AzurermProvider(
self,
id="az",
features={
"resource_group": {"prevent_deletion_if_contains_resources": True}
},
skip_provider_registration=True,
)
for k,v in virtualMachines.items():
VirtualMachine.generate_config_for_import(
self, import_to_id=v, import_from_id=k
)
for k,v in SqlVirtualMachines.items():
MssqlVirtualMachine.generate_config_for_import(
self, import_to_id=v, import_from_id=k
)
If I use the below code this still errors out. The only way to get this to work is to comment out one of the for loops.
for k,v in SqlVirtualMachines.items():
MssqlVirtualMachine.generate_config_for_import(
self, import_to_id=v, import_from_id=k
).override_logical_id("abc")