Category: setup
Terraform CLI installation and initializationObjective
- Detect whether
terraformis already installed locally; if not installed, install automatically - Ensure
terraform versionoutputs normally - Guide through completing
terraform initworkspace initialization - Handle basic environment issues such as Provider downloads and mirror configuration
About Provider credential configuration: Credential configuration for various cloud providers (AWS/Azure/GCP/Alibaba Cloud, etc.) is outside the scope of this skill. Provider credentials should be configured as needed when actually using Terraform; please refer to terraform-skill for guidance on Provider credential configuration.
When to trigger this skill
- The user is using Terraform-related features for the first time
- Error when executing
terraformcommandcommand not found terraform initreports Provider download failure (network/mirror issue)- The user proactively requests to install or reinstall Terraform
- Prerequisite dependency check for other Terraform skills (terraform-skill)
Complete workflow
Step 1: Detect installation status
which terraform 2>/dev/null || echo "NOT_INSTALLED"
If not installed, proceed to the installation workflow. If already installed, skip to Step 3.
Step 2: Install Terraform
macOS (Homebrew — recommended)
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
macOS (manual download)
ARCH=$(uname -m)
case "$ARCH" in
x86_64|amd64) ARCH="amd64" ;;
arm64|aarch64) ARCH="arm64" ;;
esac
curl -fsSL "https://releases.hashicorp.com/terraform/1.14.6/terraform_1.14.6_darwin_${ARCH}.zip" -o /tmp/terraform.zip
mkdir -p ~/.local/bin
unzip -o /tmp/terraform.zip -d ~/.local/bin/
chmod +x ~/.local/bin/terraform
export PATH="$HOME/.local/bin:$PATH"
Linux (manual download)
ARCH=$(uname -m)
case "$ARCH" in
x86_64|amd64) ARCH="amd64" ;;
arm64|aarch64) ARCH="arm64" ;;
esac
curl -fsSL "https://releases.hashicorp.com/terraform/1.14.6/terraform_1.14.6_linux_${ARCH}.zip" -o /tmp/terraform.zip
mkdir -p ~/.local/bin
unzip -o /tmp/terraform.zip -d ~/.local/bin/
chmod +x ~/.local/bin/terraform
export PATH="$HOME/.local/bin:$PATH"
Linux (APT — Debian/Ubuntu)
sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
wget -O- https://apt.releases.hashicorp.com/gpg | \
gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg > /dev/null
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt-get update && sudo apt-get install terraform
Linux (YUM — RHEL/CentOS/Fedora)
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum -y install terraform
Verify after installation:
terraform version
Step 3: Initialize the Terraform workspace
In a directory that has a .tf file, execute:
terraform init
terraform init will perform:
- Initialize Backend — configure state storage (local by default)
- Install Provider plugins — download the Providers declared in
.tfProvider declared in - Download modules — fetch referenced remote modules
- Create lock file — generate
.terraform.lock.hclto lock versions
Common init options:
# 升级 Provider 和模块到约束范围内的最新版本
terraform init -upgrade
# 重新配置 Backend
terraform init -reconfigure
# 迁移 State 到新 Backend
terraform init -migrate-state
Step 4: Verify installation and initialization
# 检查版本
terraform version
# 检查配置文件语法(需要先 init)
terraform validate
# 格式化代码
terraform fmt -check
Verification result judgment:
| Output | Meaning | Handling method |
|---|---|---|
Terraform vX.Y.Z |
Version is normal | Installation successful |
Success! The configuration is valid. |
validate passes | Initialization complete |
Error: Failed to query available provider packages |
Provider download failed | Check network or configure mirror |
Error: Inconsistent dependency lock file |
Lock file inconsistent | Execute terraform init -upgrade |
Error: Module not installed |
Module not downloaded | Execute terraform init |
Error: No valid credential sources found |
Provider credentials missing | Refer to terraform-skill configure credentials |
Automation script
This skill provides scripts/setup_terraform.py, used for one-click detection + installation (execute from this skill's directory):
python scripts/setup_terraform.py
This script will:
- Detect whether
terraformis already installed - If not installed, automatically download and install based on OS/Arch (prefer Homebrew on macOS)
- Output version information to confirm successful installation
Agent decision guide
When you (AI Agent) execute this skill, operate according to the following decision tree:
开始
│
├── which terraform → 未找到?
│ └── 根据操作系统自动选择最佳安装方式(无需询问用户)
│ ├── macOS → 优先 brew install,brew 不可用则手动下载
│ └── Linux → 优先手动下载 zip,检测到 apt/yum 则用包管理器
│
├── terraform version → 正常输出?
│ └── 是 → ✅ Terraform 已就绪
│
├── 当前目录有 .tf 文件?
│ ├── 是 → terraform init
│ │ ├── 成功 → terraform validate
│ │ │ ├── 成功 → ✅ 初始化完成
│ │ │ └── 失败 → 根据错误信息自动判断修复方案并执行
│ │ └── Provider 下载失败 → 自动配置阿里云镜像源并重试
│ └── 否 → ✅ 安装完成,等用户创建 .tf 文件后再 init
│
└── 遇到 Provider 凭证错误?
└── 引导用户参考 terraform-skill 进行凭证配置
Automatic Decision-Making Principles
Important: When executing this skill, the Agent shouldautomatically determine and select the most suitable solution and configuration, without asking the user, and execute directly.
Automatic Installation Method Selection Logic
| Operating System | Condition | Automatic Selection |
|---|---|---|
| macOS | which brew exists |
brew install hashicorp/tap/terraform |
| macOS | brew unavailable | Manually download the zip for the corresponding architecture and install it to ~/.local/bin |
| Linux (Debian/Ubuntu) | which apt-get exists |
APT installation |
| Linux (RHEL/CentOS) | which yum exists |
YUM installation |
| Linux (other) | No package manager | Manually download and install the zip |
Automatic Handling of Provider Download Failure
When terraform init fails due to Provider download failure,automatically configure the Alibaba Cloud mirrorand retry:
cat > ~/.terraformrc << 'EOF'
provider_installation {
network_mirror {
url = "https://mirrors.aliyun.com/terraform/"
}
}
EOF
terraform init
Automatic Fix for init Errors
| Error Type | Automatic Fix Operation |
|---|---|
Inconsistent dependency lock file |
Automatically execute terraform init -upgrade |
Module not installed |
Automatically execute terraform init |
Plugin reinitialization required |
Automatically execute terraform init -upgrade |
Backend initialization required |
Automatically execute terraform init |
| Provider credential error | Guide the user to refer to terraform-skill to configure credentials |
Execution Principles
- All scenarios that can be automatically determinedare directly handled by the Agent, which automatically selects the optimal solution based on environment detection results and executes it without asking the user
- The installation method is automatically determined based on the operating system and available toolchain
- Error fixes automatically match the best repair strategy based on the error type
- Mirror configuration defaults to the Alibaba Cloud mirror
- Only in extreme cases where the user must provide sensitive information (such as credentials) or automatic decision-making is impossible should the Agent explain to the user in plain text
Terraform China Mirror Configuration
Downloading Providers in domestic network environments may be slow; a mirror can be configured.
Create or edit ~/.terraformrc:
# 阿里云镜像
provider_installation {
network_mirror {
url = "https://mirrors.aliyun.com/terraform/"
}
}
or the Tencent Cloud mirror:
provider_installation {
network_mirror {
url = "https://mirrors.tencent.com/terraform/"
}
}
Quick Fixes for Common Issues
| Issue | Quick Fix |
|---|---|
command not found: terraform |
brew install hashicorp/tap/terraform or refer to the installation process |
Error: Failed to query available provider packages |
Check the network connection, or configure the Terraform Registry mirror |
Error: Backend initialization required |
Execute terraform init |
Error: Plugin reinitialization required |
Execute terraform init -upgrade |
Error: Inconsistent dependency lock file |
Execute terraform init -upgrade update the lock file |
Error: state snapshot was created by Terraform vX.Y.Z |
Upgrade the Terraform version |
Error: Module not installed |
Execute terraform init or terraform get |
| Provider download timeout | Configure the Terraform Registry mirror (see above) |
Error: No valid credential sources found |
This is a Provider credential issue; refer to terraform-skill |
Configuration File Locations
| File | Location | Description |
|---|---|---|
| CLI configuration | ~/.terraformrc(macOS/Linux) |
Global CLI configuration (mirrors, etc.) |
| Provider cache | ~/.terraform.d/plugins/ |
Cache of downloaded Provider plugins |
| Workspace Provider | .terraform/providers/ |
Provider plugins for the current workspace |
| Lock file | .terraform.lock.hcl |
Provider version locking (should be committed to Git) |
| State file | terraform.tfstate |
Local state file (should not be committed to Git) |
Relationship with other skills
| Skill | Responsibilities |
|---|---|
| terraform-cli-setup(this skill) | Basic environment for Terraform CLI installation, init, mirror configuration, etc. |
| terraform-skill | Actual use of Terraform: writing configurations, Provider credential configuration, module development, testing, CI/CD |
About Alibaba Cloud ROS: Alibaba Cloud Resource Orchestration Service (ROS) has no standalone CLI tool; it is used directly through
aliyun ros <API>using aliyun-cli. If you need to use ROS, please ensure aliyun-cli is installed (refer to the alicloud_cli skill).
Interaction Tips
Important Notes: When using this skill, please follow these rules:
- Automatic decision-making takes priority: For scenarios such as installation methods, mirror selection, and error handling, the Agent should automatically select the optimal solution based on environment detection results and execute it directly, without asking the user questions
- Briefly explain to the user what is being done and why during execution, maintaining information transparency
- Only ask the user when sensitive information that cannot be obtained automatically (such as credentials) is needed
- When encountering an error, first automatically attempt to fix it; if the fix fails, then explain the situation to the user and provide suggestions
References
- Terraform official documentation:https://developer.hashicorp.com/terraform
- Terraform CLI tutorial:https://developer.hashicorp.com/terraform/tutorials/cli
- Terraform installation guide:https://developer.hashicorp.com/terraform/install
- Provider Registry:https://registry.terraform.io/
- GitHub repository:https://github.com/hashicorp/terraform