Terraform报错:模块内未声明google_service_account_key.mstr_bq_key资源
问题排查与修复方案
核心问题
模块module.mstr_bq_connection的service_account.tf第22行试图引用google_service_account_key.mstr_bq_key,但这个资源既没有在模块内部声明,也没有通过输入变量从根模块传入。而你在根模块main.tf中创建的是google_service_account_key.mstr_bq_sa_key,两者名称不匹配,且模块无法直接访问根模块的资源。
修复方案(二选一)
方案1:将根模块的SA密钥传递给模块
如果SA密钥需要在根模块统一管理,模块仅需使用该密钥,按以下步骤操作:
- 在模块的
variables.tf中添加输入变量:variable "mstr_bq_sa_key" { type = object({ id = string name = string private_key = string # 根据实际需要添加其他属性 }) description = "Service Account Key for BigQuery Connection" } - 修改模块
service_account.tf第22行的引用,将google_service_account_key.mstr_bq_key替换为var.mstr_bq_sa_key - 在根模块调用
module.m_bq_connection时传入该密钥:module "m_bq_connection" { source = "./path/to/module" # 其他变量... mstr_bq_sa_key = google_service_account_key.mstr_bq_sa_key }
方案2:在模块内部创建SA密钥
如果SA密钥属于模块的一部分,应该在模块内声明该资源:
- 将根模块
main.tf中的google_service_account_key.mstr_bq_sa_key代码移动到模块的service_account.tf中,并重命名为mstr_bq_key(或者直接修改模块内的引用为mstr_bq_sa_key) - 示例模块内的资源声明:
resource "google_service_account_key" "mstr_bq_key" { service_account_id = google_service_account.mstr_bq_sa.id # 其他配置... } - 删除根模块中重复的
google_service_account_key.mstr_bq_sa_key资源
验证操作
修改完成后,执行以下命令确认错误修复:
terraform init terraform plan
内容的提问来源于stack exchange,提问作者adithyar24200
相关产品推荐
相关产品推荐

