Skip to content

Terraform

一句话解释

Terraform 是 Infrastructure as Code (IaC) tool 那个 lets 你定义 and provision cloud infrastructure using declarative 配置 files.

用大白话说

Terraform is a tool by HashiCorp that lets you define your infrastructure — servers, databases, networks, load balancers — as code. Instead of clicking through a cloud console to create resources, you write configuration files and Terraform creates everything for you.

Terraform uses a declarative approach: you describe what you want (e.g., "I 想要 3 服务器 and 加载 balancer"), and Terraform figures out how to create it. It tracks the state of your infrastructure in a state file, so when you change your configuration, Terraform knows what already exists and only creates/updates/deletes what changed.

Terraform works with all major cloud providers (AWS, Azure, GCP) and many other services through providers. This means you can use the same tool and language to manage infrastructure across different clouds.

示例

hcl
resource "aws_instance" "web" {
  ami           = "ami-12345678"
  instance_type = "t2.micro"
  count         = 3

  tags = {
    Name = "WebServer"
  }
}

这个 Terraform code 创建 3 AWS EC2 实例 using specified AMI and 实例类型.

工作原理

  1. You write infrastructure definitions in HCL (HashiCorp Configuration Language).
  2. terraform 计划 shows what will be created, changed, or destroyed.
  3. terraform apply executes the changes against your cloud provider.
  4. Terraform stores the state in a state file so it can manage updates.

常见混淆

  • 容易混淆的对象: Ansible

    关键区别: Terraform provisions infrastructure (创建 resources); Ansible 配置 existing 服务器 (安装 software, manages files).

相关术语

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表