解决Terraform初始化慢~配置本地离线源

简介: 直奔主题,配置一个离线的源。

image.png

这里不再介绍Terraform是啥了,可以参考最近上线的课程。直奔主题,配置一个离线的源。

需要手动或者terraform init一次下载, 然后缓存。后续直接使用缓存。

本次实践使用的是Linux/Mac 系统,如果是windows系统有两点不同的配置。

  • CLI配置文件的名称为terraform.rc
  • plugin_cache_dir: D:/xxx/xxx

1. 创建配置文件

.terraformrc是Terraform CLI的配置文件

plugin_cache_dir  = "$HOME/.terraform.d/terraform-plugin-cache" 
disable_checkpoint = true
  • plugin_cache_dir 是插件的缓存目录(此目录需要提前创建不然init报错)
  • disable_checkpoint 禁用 需要连接HashiCorp 提供的网络服务的升级和安全公告检查
mkdir -p $HOME/.terraform.d/terraform-plugin-cache

文件创建好了之后, 要通过配置TF_CLI_CONFIG_FILE变量,让TerraformCLI可以加载到配置文件。这个变量的值没有固定配置,而是取决于.terraformrc文件路径。

export TF_CLI_CONFIG_FILE=$HOME/Desktop/terraform/terraform-module-example/.terrafo
rmrc


2. 进行初始化

插件下载方式有两种:

  1. 通过 terraform init 自动下载provider 插件;
  2. 登入registry.terraform.io手动到GitHub下载,并按照目录结构存放到plugin_cache_dir;

本次演示先使用terraform init进行操作, 如果手动到registry下载,需要按照目录结构存放;

terraform init
Initializing modules...
- mydns in ../../modules/dns
- myecs in ../../modules/ecs
- myssecgroup in ../../modules/secgroup
- myvpc in ../../modules/vpc
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/alicloud versions matching "1.164.0"...
- Installing hashicorp/alicloud v1.164.0...
- Installed hashicorp/alicloud v1.164.0 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

初始化之后, 查看plugin_cache_dir中的内容:

$HOME/.terraform.d/terraform-plugin-cache/registry.terraform.io/hashicorp/alicloud/1.164.0/darwin_arm64

➜  .terraform.d pwd
/Users/lizeyang/.terraform.d
➜  .terraform.d tree
.
|____checkpoint_cache
|____checkpoint_signature
|____terraform-plugin-cache
| |____registry.terraform.io
| | |____hashicorp
| | | |____alicloud
| | | | |____1.164.0
| | | | | |____darwin_arm64
| | | | | | |____terraform-provider-alicloud_v1.164.0


3. 模拟断网,离线初始化

方法1:初始化时指定plugin-dir

terraform init --plugin-dir $HOME/.terraform.d/terraform-plugin-cache/

terraform init  --plugin-dir $HOME/.terraform.d/terraform-plugin-cache/
Initializing modules...
- mydns in ../../modules/dns
- myecs in ../../modules/ecs
- myssecgroup in ../../modules/secgroup
- myvpc in ../../modules/vpc
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/alicloud versions matching "1.164.0"...
- Using hashicorp/alicloud v1.164.0 from the shared cache directory
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!

方法2:定义Terraform插件使用本地mirror

provider_installation {
  filesystem_mirror {
    path    = "/Users/lizeyang/.terraform.d/terraform-plugin-cache"
    include = ["registry.terraform.io/*/*"]
  }
}
➜  dev terraform init
Initializing modules...
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/alicloud versions matching "1.164.0"...
- Using hashicorp/alicloud v1.164.0 from the shared cache directory
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!

到此就完成了terraform离线本地源的配置了, 除了这种方式外其实也可以基于terraform开放的HTTP API协议,使用Python Flask写一个registry server。

目录
相关文章
|
9月前
|
数据中心
Terraform如何配置
Terraform如何配置
91 0
|
9天前
|
弹性计算 持续交付 API
基于 ROS 的Terraform托管服务轻松部署ChatGLM-6B
文章介绍了如何利用ROS和Terraform模板轻松自动化部署基于GLM架构、优化中文对话的ChatGLM-6B模型至阿里云,提高了部署效率与便捷性,适用于多种应用场景,且模型部署过程详细,彰显了基础设施即代码(IaC)的优势。
25 3
基于 ROS 的Terraform托管服务轻松部署ChatGLM-6B
|
9天前
|
弹性计算 人工智能 持续交付
基于 ROS 的Terraform托管服务轻松部署Qwen-7B-Chat
文章介绍了如何利用ROS和Terraform模板轻松自动化部署阿里云的Qwen-7B-Chat大语言模型服务,提高了部署效率与便捷性,是实现云资源和服务快速上线的最佳实践。
28 2
基于 ROS 的Terraform托管服务轻松部署Qwen-7B-Chat
|
3月前
|
弹性计算 API 持续交付
基于 ROS 的 Terraform 托管服务轻松部署文本转语音系统 ChatTTS
基于 IaC 的理念,通过定义一个模板,使用 ROS 提供的 Terraform 托管服务进行自动化部署,可以非常高效快捷地部署任意云资源和应用(比如 ChatTTS 服务)。相比于手动部署或者通过 API、SDK 的部署方式,有着高效、稳定等诸多优势,也是服务上云的最佳实践。
50 1
基于 ROS 的 Terraform 托管服务轻松部署文本转语音系统 ChatTTS
|
4月前
|
弹性计算 持续交付 数据中心
一键云部署:ROS的Terraform托管服务助你轻松上线2048经典游戏
阿里云的资源编排服务ROS提供了Terraform托管能力,用户可以直接在ROS控制台上部署Terraform脚本,本文将详细介绍如何使用ROS的Terraform托管服务一键部署经典的2048小游戏到云端,让全世界的玩家都能在线体验。
225 1
EMQ
|
JSON 负载均衡 物联网
使用 Terraform 在 GCP 上一键部署 EMQX MQTT Broker
本文将指导您如何设置 GCP 项目、创建服务账户、编写 Terraform 配置文件,实现在 GCP 上轻松部署 EMQX MQTT Broker。
EMQ
152 0
使用 Terraform 在 GCP 上一键部署 EMQX MQTT Broker
|
存储 运维 安全
App Deploy as Code! SAE & Terraform 实现 IaC 式部署应用
SAE 和 Terraform 的结合,能够帮助企业像处理代码一样管理自己的应用,对资源的操作都变得可审计,可追溯,可回滚,同时也降低人为操作带来的风险。
App Deploy as Code! SAE & Terraform 实现 IaC 式部署应用
|
存储 运维 安全
App Deploy as Code! SAE & Terraform 实现 IaC 式部署应用
本文将简单的介绍 IaC 和 SAE 给企业带来的巨大便利,并通过一个使用 Terraform 创建 SAE 应用的例子感受 SAE & Terraform 给传统企业 IT 设施管理带来的降维打击
App Deploy as Code! SAE & Terraform 实现 IaC 式部署应用
EMQ
|
存储 JSON 安全
使用 Terraform 在 AWS 上快速部署 MQTT 集群
本文将以 MQTT 消息服务器 EMQX 为例,采用 AWS 作为公有云平台,介绍如何使用 Terraform 快速部署一个高可用的 MQTT 集群。
EMQ
230 0
使用 Terraform 在 AWS 上快速部署 MQTT 集群
EMQ
|
存储 JSON 安全
使用 Terraform 在阿里云上快速部署 MQTT 集群
本文将以分布式物联网 MQTT 消息服务器 EMQX 为例,采用阿里云作为公有云平台,介绍如何使用 Terraform 快速部署一个高可用的 MQTT 集群。
EMQ
260 0
使用 Terraform 在阿里云上快速部署 MQTT 集群

推荐镜像

更多