Terraform之AWS上遇到的坑

1.前情概要
过完年回来,terraform init始终报错,找了一圈发现是网络地址路径多了一个层级,直接导致无法下载provider插件
于是考虑1做本地化插件,2做providerRegistry私库
从效率上来看,方案1更快,从长远角度来看,方案2更有优势
就目前情况而言采用了方案1,以下是具体步骤:
2.创建本地provider仓库及插件缓存目录
C:/Users/xxx/.terraform.d/plugin-cache
C:/Users/xxx/.terraform.d/filesystem-mirror

3.准备CLI配置文件terraform.rc
plugin_cache_dir = "C:/Users/xxx/.terraform.d/plugin-cache"
disable_checkpoint = true
plugin_cache_may_break_dependency_lock_file = true
provider_installation {
filesystem_mirror {
path = "C:/Users/xxx/.terraform.d/filesystem-mirror"
include = ["registry.terraform.io//"]
}
}
注:Windows下所有配置路径使用“/”分隔而非“\”。

TF_CLI_CONFIG_FILE用户环境变量配置

terraform.tfrc存放路径用户可自定义

$env:TF_CLI_CONFIG_FILE="C:/Users/xxx/AppData/Roaming/terraform.rc"

或在高级系统变量中设置:

系统属性->高级->环境变量->用户变量->新建
新建用户变量,变量名为:TF_CLI_CONFIG_FILE
变量值为terraform.rc文件路径

将二进制可执行程序放到配置本地插件库目录
程序放置子目录应遵循以下命名规则:
<user>/.terraform.d/filesystem-mirror/<hostname>/<namespace>/<type>/<version>/<os_arch>/

其中 os_arch: 一般是 linux_arm64, darwin_amd64,windows_amd64等
以本机Windows 10为例:

C:\Users\xxx.terraform.d\filesystem-mirror\registry.terraform.io\hashicorp\aws\5.86.0\windows_amd64

设置亚马逊云provider.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}

provider "aws" {
access_key = "<YOUR AK>"
secret_key = "<YOUR SK>"
region = "us-east-1"
}

4.初始化亚马逊云provider
在工作空间下执行terraform init,初始化成功控制台输出信息如下:

image.png

参考配置文档:
https://developer.hashicorp.com/terraform/cli/config/config-file

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容