You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Terraform公共仓库中引用子模块?以RDS备份复制模块为例

解决terraform-aws-modules/rds/aws子模块引用问题

正确的子模块引用方式

由于terraform-aws-rds的子模块未发布到Terraform Registry,必须通过Git仓库路径指定版本和子模块目录,以下是有效配置:

module "db_automated_backups_replication" {
  source  = "git::https://github.com/terraform-aws-modules/terraform-aws-rds.git//modules/db_instance_automated_backups_replication?ref=v5.1.0"
  # 替换v5.1.0为与主模块匹配的5.x版本号,比如v5.0.0或最新的5.x稳定版

  source_db_instance_arn = module.db.db_instance_arn

  providers = {
    aws = aws.replica
  }
}

之前配置无效的原因

  • 相对路径../../modules/...:你的配置目录并非terraform-aws-rds仓库的一部分,相对路径无法定位到子模块文件。
  • Registry路径terraform-aws-modules/rds/aws/modules/...:Terraform Registry仅发布主模块内容,子模块未单独上架,该路径不被识别。
  • SSH Git路径git@github.com:...:若环境未配置GitHub SSH密钥会拉取失败,且未指定版本tag可能导致拉取的子模块与主模块版本不兼容。

额外注意事项

  • 子模块版本必须与主模块~>5.0的版本范围匹配,避免因版本差异引发的兼容性问题。
  • 若使用SSH路径,确保本地或CI环境已配置GitHub SSH访问权限;优先推荐HTTPS路径,适配更多环境。
  • 确认aws.replica provider已正确配置为备份复制的目标区域(RDS自动备份复制需跨区域操作)。

内容的提问来源于stack exchange,提问作者Rad4

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 08:07:31