解决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。

目录
相关文章
|
4月前
|
数据中心
Terraform如何配置
Terraform如何配置
48 0
|
数据中心 iOS开发 MacOS
玩转阿里云 Terraform(三):Terraform 的安装和加速
本文以Mac OS为例,详细介绍如何在本地安装Terraform,并在文章最后介绍一种可以加速Terraform安装的方法。
14966 0
|
24天前
|
消息中间件 Kubernetes Kafka
Terraform阿里云创建资源1分钟创建集群一键发布应用Terraform 创建 Kubernetes 集群
Terraform阿里云创建资源1分钟创建集群一键发布应用Terraform 创建 Kubernetes 集群
17 0
|
9月前
|
Kubernetes NoSQL 持续交付
使用Terraform/Ansible/Kubernetes在阿里云上自动部署MongoDB
Terraform, Ansible, Kubernetes, MongoDB, AliCloud
292 1
EMQ
|
存储 JSON 安全
使用 Terraform 在阿里云上快速部署 MQTT 集群
本文将以分布式物联网 MQTT 消息服务器 EMQX 为例,采用阿里云作为公有云平台,介绍如何使用 Terraform 快速部署一个高可用的 MQTT 集群。
EMQ
234 0
使用 Terraform 在阿里云上快速部署 MQTT 集群
|
弹性计算 运维 自然语言处理
Terraform 支持自动化开通阿里云产品
## 什么是 Terraform 和云产品开通 **Terraform** 一款开源的运行在客户端的自动化资源编排工具,以 IaC 的理念实现云资源的自动化运维。更多细节可以参考 [Terraform 是什么](https://developer.aliyun.com/article/713099)。 **云产品开通**,顾名思义,就是阿里云客户在使用某些云产品前,需要登录云产品官方控
708 0
Terraform 支持自动化开通阿里云产品
|
存储 运维 自然语言处理
“阿里云开放平台俱乐部”首站启航,助力企业基于Terraform完成上云进阶
简介:阿里云开放平台致力于帮助开发者快速上云并完成云上管理,未来将举办一系列开发者沙龙,帮助开发者解决如何“降本、提效”的管理云资源等问题
“阿里云开放平台俱乐部”首站启航,助力企业基于Terraform完成上云进阶
|
架构师 Devops 数据中心
阿里云开发者DevUP沙龙·北京·基于Terraform玩转阿里云 圆满结束
本次阿里云开发者DevUP沙龙·北京·基于Terraform玩转阿里云线下技术沙龙已完美落幕,本文带你回顾基本次活动的全部干货亮点!
1533 0
阿里云开发者DevUP沙龙·北京·基于Terraform玩转阿里云 圆满结束
|
运维 架构师 数据中心
阿里云开发者 DevUP 沙龙 -北京站 -基于Terraform玩转阿里云邀你参加啦
8月22日,阿里云Terraform技术专家何贵民,携手Terraform官方资深架构师刘宇雷,为大家分享云计算行业自动化编排最前沿技术与应用,期待与您共同成长。
14603 0
阿里云开发者 DevUP 沙龙 -北京站 -基于Terraform玩转阿里云邀你参加啦
|
数据中心
玩转阿里云 Terraform(四):Terraform 常用命令详解
通过前几篇文章的介绍,相信大家对Terraform已经有了大致的熟悉和了解,本文将从实践开始,向大家介绍Terraform的几个常见命令。 Terraform是一个面向客户端的工具,所以对所有资源的管理都是通过Terraform命令来实现的。
11719 0

推荐镜像

更多