Terraform 入門教學:5 步搞掂 Infrastructure as Code

Terraform 係乜東東? 如果你做 IT infrastructure,你一定聽過 Infrastructure as Code(IaC)呢個 term。簡單嚟講就係用 code 去定義同管理你嘅伺服器、網絡、防火牆,唔使次次用手 click GUI。而喺 IaC 嘅世界入面,Terraform 絕對係最主流嘅工具之一。 Terraform 係 HashiCorp 開發嘅 open-sour...

Terraform 係乜東東?

如果你做 IT infrastructure,你一定聽過 Infrastructure as Code(IaC)呢個 term。簡單嚟講就係用 code 去定義同管理你嘅伺服器、網絡、防火牆,唔使次次用手 click GUI。而喺 IaC 嘅世界入面,Terraform 絕對係最主流嘅工具之一。

👉 延伸閱讀:Linux Server 安全加固 6 步搞掂 — Defense in Depth 實戰指南

Terraform 係 HashiCorp 開發嘅 open-source 工具,用 HCL(HashiCorp Configuration Language)去描述 infrastructure。佢最大嘅賣點係 multi-cloud —— 同一個 workflow 可以管理 AWS、Azure、GCP、甚至 on-premise VMware,唔使學幾套唔同嘅工具。

Terraform 核心概念:3 分鐘搞明

學 Terraform 之前,你要理解三個最核心嘅概念:

1. Provider — 即係你要管理邊個平台。例如想管理 AWS 就用 hashicorp/aws provider,想管理 Azure 就用 hashicorp/azurerm

2. Resource — 即係你要 create 嘅嘢。例如一部 EC2 instance、一個 S3 bucket、一條 firewall rule,全部都係 resource。

3. State — Terraform 會 keep 一個 state file(terraform.tfstate),記錄曬所有已經 deploy 咗嘅 resource。每次 run terraform plan 嘅時候,佢會對比 state file 同你嘅 config,計出有咩要改。

Terraform 實戰:5 步 deploy 第一部 EC2

以下會教你喺 5 步之內用 Terraform deploy 一部 AWS EC2 instance。假設你已經裝好 AWS CLI 同設定好 credentials。

Step 1: 安裝 Terraform

macOS 用家可以直接用 Homebrew:

brew tap hashicorp/tap
brew install hashicorp/tap/terraform

# 檢查安裝
terraform version

Linux 用家可以參考官方 download page。

Step 2: 建立 Terraform config 檔案

開一個新 folder,建立 main.tf

# main.tf — Terraform 基本設定
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = "ap-east-1"  # 香港 region
}

Step 3: 定義 EC2 resource

喺同一個 main.tf 入面加以下內容:

resource "aws_instance" "web_server" {
  ami           = "ami-0c1c30571d2dae5c9"  # Ubuntu 24.04 LTS (HK)
  instance_type = "t3.micro"

  tags = {
    Name        = "Terraform-Demo"
    Environment = "Dev"
  }
}

呢段 code 定義咗一部 t3.micro EC2,行 Ubuntu 24.04,放喺香港 region。

Step 4: Initialize & Plan

# Download provider plugins
terraform init

# Preview 將會做嘅改動(唔會實際 deploy)
terraform plan

terraform plan 會顯示一個 diff,話俾你知會 create 咩 resource、改咩 setting。呢個係 Terraform 最重要嘅 safety net —— 喺真正 apply 之前 preview 一次。

Step 5: Apply 部署

terraform apply

# 佢會再 show 一次 plan,然後叫你 confirm
# 打 'yes' 就可以 deploy

幾分鐘後,你部 EC2 instance 就會喺 AWS console 出現!

Terraform 進階技巧:Variables & Outputs

寫死 AMI ID 同 instance type 唔係好 practice。Terraform 支援 variables 令 config 更 flexible:

# variables.tf
variable "instance_type" {
  description = "EC2 instance type"
  type        = string
  default     = "t3.micro"
}

variable "environment" {
  description = "Deployment environment"
  type        = string
  default     = "dev"
}

# main.tf 改用 variable
resource "aws_instance" "web_server" {
  ami           = "ami-0c1c30571d2dae5c9"
  instance_type = var.instance_type

  tags = {
    Name        = "Terraform-${var.environment}"
    Environment = var.environment
  }
}

咁你就可以用 terraform apply -var="environment=prod" 去 override default values。

Terraform 常見中伏位

1. State file 唔好放 local — 如果你同 team members 一齊用 Terraform,state file 一定要放 remote backend(例如 S3 + DynamoDB lock)。唔係就會出現兩個人同時 apply 嘅災難。

# backend.tf
terraform {
  backend "s3" {
    bucket         = "my-terraform-state"
    key            = "prod/terraform.tfstate"
    region         = "ap-east-1"
    dynamodb_table = "terraform-lock"
  }
}

2. 唔好手動改 AWS console 嘅 resource — Terraform 唔知道你手動改咗嘢,下次 apply 可能會 overwrite 返你嘅改動。所有嘢經 Terraform 做。

3. 用 terraform destroy 之前 double check — 呢個 command 會 delete 曬所有 Terraform manage 嘅 resource。生產環境用之前一定要三思!

總結:點解 IT 人要學 Terraform?

Terraform 已經成為 DevOps 同 Cloud Engineer 嘅必備技能。佢唔單止慳時間(唔使手動 click console),仲確保 infrastructure 係 reproducible、version-controlled、同 auditable。無論你係 manage 緊 3 部機定 3000 部機,Terraform 都可以幫你將 infrastructure 管理提升到另一個層次。

🔗 參考資料:NVD NIST 漏洞資料庫

今日學咗基本功,聽日就可以開始實戰。快啲開個 AWS free tier account 試下啦!

#Deployment #firewall #Terraform #部署 #IT教學