使用cdktf-Java构建Azure HdInsight Kafka集群遇栈识别错误求助
问题原因及修正方案
错误的核心原因是你创建HdinsightKafkaCluster时,把资源挂载到了外部的Construct scope,而非当前的TerraformStack实例,导致CDKtf无法识别该资源所属的栈。
具体修正点:
- 修改
create方法的调用参数,将外部scope替换为当前的KafkaAnnonymous实例(this) - 在构建
HdinsightKafkaCluster时,使用当前栈实例作为父节点
修正后的代码片段:
public class KafkaAnnonymous extends TerraformStack { public KafkaAnnonymous(Construct scope, String id, final Environment environment, ResourceGroup rg, String storageAccount, Network network, KeyVault kv) { super(scope, id); AzurermProvider.Builder.create(this, "azureProvider") .features(AzurermProviderFeatures.builder() .build()) .subscriptionId("<SUBSCRIPTION_ID>") .build(); AzurermBackend.Builder.create(this) .resourceGroupName("g_rg") .storageAccountName("g_storage_account_name") .containerName("terraform") .key(String.format("%s.terraform.tfstate", id)) .build(); // 传入当前栈实例this代替外部scope create(this, id, environment, rg, storageAccount, network, kv); } private final void create(Construct stackScope, String id, final Environment environment, ResourceGroup rg, String storageAccount, Network network, KeyVault kv) { HdinsightKafkaCluster.Builder // 使用当前栈实例作为父构造体 .create(stackScope, id) .clusterVersion("2.4.1") .resourceGroupName(rg.getResourceGroup().getName()) .name(id) .location("westeurope") .tier("Standard") .componentVersion(componentVersion()) .gateway(gateway(password)) .storageAccount(storage(storageAccount)) .roles(roles(network, password)) .build(); } ... } }
补充说明:
CDKtf要求所有Terraform资源必须作为TerraformStack的子节点存在,只有这样资源才能被正确关联到对应的栈中,生成有效的基础设施代码。如果将资源挂载到外部的Construct上,就会触发栈识别失败的错误。
内容的提问来源于stack exchange,提问作者SnakeEye
相关产品推荐
相关产品推荐

