自定义Terraform Provider初始化报错:无法解析terraform.local主机
自定义Terraform Provider初始化失败问题
环境与配置详情
- 自行开发的Terraform Provider可执行文件存放路径:
.terraform.d/plugins/terraform.local/local/ecrpushimage/darwin_amd64/1.0.0/ - 上述路径为手动创建,运行环境为Intel芯片的MacOS主机
- 使用的Terraform配置:
terraform { required_providers { ecrpushimage = { source = "terraform.local/local/ecrpushimage" version = ">= 1.0.0" } } }
执行terraform init后的报错信息
╷
│ Error: Failed to query available provider packages
│
│ Could not retrieve the list of available versions for provider terraform.local/local/ecrpushimage:
│ could not connect to terraform.local: failed to request discovery document: Get
│ "https://terraform.local/.well-known/terraform.json": dial tcp: lookup terraform.local: no such host
解决方法
问题根源是Terraform默认会尝试从远程仓库拉取Provider,而你的自定义Provider是本地存储的,需要明确告知Terraform直接使用本地版本,跳过远程查询。
方案1:配置本地镜像规则
在用户主目录下创建或修改.terraformrc文件,添加以下内容:
provider_installation { filesystem_mirror { path = "~/.terraform.d/plugins" include = ["terraform.local/local/*"] } direct { exclude = ["terraform.local/local/*"] } }
该配置会让Terraform对terraform.local/local/前缀的Provider,直接从本地~/.terraform.d/plugins路径读取,不再发起远程请求。
方案2:初始化时指定插件目录
执行init命令时直接指定本地插件所在目录:
terraform init -plugin-dir=~/.terraform.d/plugins
此方式会让Terraform直接从指定目录加载插件,跳过远程版本查询步骤。
额外注意事项
- 确保本地Provider文件名符合Terraform命名规范:
terraform-provider-<名称>_<版本>_<系统>_<架构>,例如你的场景下应为terraform-provider-ecrpushimage_1.0.0_darwin_amd64(不要带.exe这类多余后缀) - Intel芯片MacOS对应的架构标识为
amd64,你当前的路径配置是正确的
内容的提问来源于stack exchange,提问作者paul445
相关产品推荐
相关产品推荐

