Skip to content

Commit 553efa4

Browse files
Add post: "What is Terraform"
This reverts commit f1196ae67c95f38aa0b30c685d6d275a5e6017a4.
1 parent be1addd commit 553efa4

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
title: 'Getting Started with Terraform: Core Concepts and Fundamentals'
3+
date: 2025-05-01T14:53:19+08:00
4+
draft: false
5+
description:
6+
isStarred: false
7+
---
8+
9+
# What is Terraform?
10+
11+
[Terraform](https://github.com/hashicorp/terraform) is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp. It supports the provisioning of both cloud and on-premises resources.
12+
13+
Infrastructure is defined through configuration files, offering several key features:
14+
15+
- State Management: Terraform uses a state file to track resource changes and updates.
16+
- Modules: Reusable components that encapsulate repeated configurations, improving maintainability.
17+
- Remote Backend: Supports backends like S3 and Terraform Cloud, allowing teams to share state files and enabling state locking to prevent conflicts during concurrent updates.
18+
19+
# Infrastructure as Code (IaC)
20+
21+
The core concept of Infrastructure as Code (IaC) is that you can deploy, update, or destroy infrastructure using code, rather than performing manual operations.
22+
This approach offers several benefits:
23+
24+
- Version Control: Since infrastructure is defined in code, it can be managed using version control tools like Git. This allows you to track changes, revert to previous versions, and maintain a clear history.
25+
- Consistency: Using the same code ensures the same infrastructure setup every time, reducing human error and improving reliability.
26+
- Reusability: IaC encourages reusability, allowing you to define infrastructure once and use it across multiple environments.
27+
28+
IaC tools can be categorized into five groups:
29+
30+
1. Shell Scripting Tools – e.g., Bash, Python
31+
1. Configuration Management Tools – e.g., Ansible, Puppet, Chef
32+
1. Server Template Tools – e.g., Docker, Packer
33+
1. Orchestration Tools – e.g., Kubernetes, Docker Swarm
34+
1. Provisioning Tools – e.g., Terraform, Pulumi, CloudFormation
35+
36+
# Terraform Language
37+
38+
Terraform uses a domain-specific language (DSL) to manage infrastructure, known as [HCL](https://github.com/hashicorp/hcl/blob/main/hclsyntax/spec.md) — the HashiCorp Configuration Language.
39+
40+
HCL is a declarative language, meaning developers only need to describe the desired end state of the infrastructure. Terraform will automatically calculate the necessary steps to reach that state and execute the required changes.
41+
42+
Below is an example of how to create a simple VPC using the [Terraform Module](https://github.com/terraform-aws-modules/terraform-aws-vpc).
43+
44+
```hcl
45+
data "aws_availability_zones" "available" {}
46+
47+
locals {
48+
vpc_cidr = "10.0.0.0/16"
49+
azs = slice(data.aws_availability_zones.available.names, 0, 3)
50+
}
51+
52+
module "vpc" {
53+
source = "terraform-aws-modules/vpc/aws"
54+
version = "~> 5.19.0"
55+
56+
name = "demo-vpc"
57+
cidr = local.vpc_cidr
58+
59+
azs = local.azs
60+
private_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 4, k)]
61+
}
62+
```
63+
64+
# How Terraform Works
65+
66+
Terraform provisions infrastructure by interacting with provider APIs through Remote Procedure Calls (RPC).
67+
68+
A Terraform Provider is a type of plugin that allows Terraform to communicate with specific platforms. Common providers include aws, azurerm, google, and digitalocean, which correspond to AWS, Azure, Google Cloud, and DigitalOcean respectively.
69+
70+
In addition to public cloud platforms, Terraform can also provision resources in private clouds and virtualized environments. This means Terraform can manage infrastructure across a wide variety of platforms—both cloud-based and on-premises—through its extensible provider system.
71+
72+
# Terraform Workflow
73+
74+
The Terraform workflow consists of three main steps:
75+
76+
1. Write – Define infrastructure using configuration files (.tf files) written in HCL (HashiCorp Configuration Language).
77+
1. Plan – Preview the changes Terraform will make without applying them. This helps validate and review the planned modifications.
78+
1. Apply – Execute the proposed changes to create, update, or destroy infrastructure resources as defined in the configuration.
79+
80+
## Terraform Main Commands
81+
82+
This section introduces only the main Terraform commands. For a complete list, you can run `terraform -help`.
83+
84+
| Command | Purpose | How It Works |
85+
|-|-|-|
86+
| terraform init | Initialize the project | Downloads the necessary providers, initializes the backend, and verifies the Terraform version. |
87+
| terraform validate | Validate configuration files | Checks for syntax errors, missing variables, duplicate providers, and other issues in the configuration files. |
88+
| terraform plan | Preview changes | Loads the Terraform state, parses configuration and variables, and calculates the resource changes needed. |
89+
| terraform apply | Apply changes | Applies the planned changes by syncing with the backend, executing updates, and saving the new Terraform state. |
90+
| terraform destroy | Destroy managed resources | Removes all infrastructure resources managed by Terraform. |
91+
92+
To learn more about any specific command, use `terraform <command> -help`.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
title: Terraform 入門與核心概念
3+
date: 2025-05-01T14:53:19+08:00
4+
draft: false
5+
description:
6+
isStarred: false
7+
---
8+
9+
# Terraform 是甚麼?
10+
11+
[Terraform](https://github.com/hashicorp/terraform) 是由 HashiCorp 開發的一個開源 Infrastructure as Code (IaC) 工具,可以用來建置和管理雲端和地端 (On-Premises) 的資源。
12+
13+
它是透過組態檔 (Configuration Files) 來定義基礎架構,有以下這些功能:
14+
15+
- 狀態管理:Terraform State 會追蹤資源的變動
16+
- 模組化 (Modules):可以將重複的配置封裝成模組
17+
- Remote Backend:支援 S3 和 Terraform Cloud 等,可以讓團隊共享狀態檔和上鎖,避免多人作業時發生衝突
18+
19+
# Infrastructure as Code (IaC)
20+
21+
基礎架構即程式碼的核心概念是:不管今天需要定義、部署或刪除基礎架構,都是透過程式碼來完成,而不是手動操作,這樣有幾個好處,像是可以透過版本控制管理檔案,也就代表可以回滾、查詢變動等,再來是一致性,確保每次部署都使用相同的設定,可以避免人為失誤,除此之外,也可以重複使用程式碼。
22+
23+
IaC 工具可以大致分為以下五類:
24+
25+
1. 腳本工具:如 Bash 和 Python
26+
2. 組態管理工具:如 Ansible、Puppet 和 Chef
27+
3. 伺服器模板 (Server Template) 工具:如 Docker 和 Packer
28+
4. 調度 (Orchestration) 工具:如 Kubernetes 和 Docker Swarm
29+
5. 配置 (Provisioning) 工具:如 Terraform、Pulumi 和 CloudFormation
30+
31+
# Terraform 組態語言
32+
33+
Terraform 採用特定領域專用語言 (domain-specific language, DSL) 來管理基礎架構,使用的語法為 [HCL](https://github.com/hashicorp/hcl/blob/main/hclsyntax/spec.md) (HashiCorp Configuration Language)。
34+
35+
HCL 是一種宣告式語言,開發者只需要描述最終想要的狀態,Terraform 就會自行計算和執行這些變動。
36+
37+
以下是使用 [Terraform Module](https://github.com/terraform-aws-modules/terraform-aws-vpc) 創建一個基礎的 VPC 範例:
38+
39+
```hcl
40+
data "aws_availability_zones" "available" {}
41+
42+
locals {
43+
vpc_cidr = "10.0.0.0/16"
44+
azs = slice(data.aws_availability_zones.available.names, 0, 3)
45+
}
46+
47+
module "vpc" {
48+
source = "terraform-aws-modules/vpc/aws"
49+
version = "~> 5.19.0"
50+
51+
name = "demo-vpc"
52+
cidr = local.vpc_cidr
53+
54+
azs = local.azs
55+
private_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 4, k)]
56+
}
57+
```
58+
59+
# Terraform 如何運作
60+
61+
Terraform 透過遠端程序呼叫 (Remote Procedure Call, RPC) 與各種 Provider 進行 API 互動,以建置基礎架構資源。
62+
63+
Terraform Provider 是 Terraform Plugin 的一種,像 aws、azurerm、google 和 digitalocean 等 Provider,會對應 AWS、Azure、Google Cloud 和 DigitalOcean。除此之外,也可以對私有雲和虛擬化平台進行配置,也就是說可以在多種平台 (Provider) 上建置基礎架構。
64+
65+
# Terraform Workflow
66+
67+
Terraform 的運作流程可以分成三個步驟:
68+
69+
1. Write:使用 HCL 定義基礎架構組態(.tf 檔案)
70+
2. Plan:模擬 Terraform 變更
71+
3. Apply:執行變更,建立或修改或刪除基礎架構資源
72+
73+
## Terraform 主要指令解析
74+
75+
先介紹主要的指令,對其餘指令有興趣的話,可以透過 `terraform -help` 查詢。
76+
77+
| 指令 | 用途 | 內部運作 |
78+
|-|-|-|
79+
| terraform init | 初始化專案 | 下載 Provider、初始化 backend 和 驗證 Terraform 版本等 |
80+
| terraform validate | 檢驗組態檔是否正確 | 檢查 Terraform 檔案,像是符號正不正確,或是少了變數,或有重複的 provider 等 |
81+
| terraform plan | 預覽變更,顯示 Terraform 即將執行的修改 | 載入 Terraform State、解析組態與變數,以及計算需要變更的資源等 |
82+
| terraform apply | 套用變更,建立或更新資源 | 載入 plan 檔案、與 Backend 同步狀態和執行變更並更新 Terraform State 等 |
83+
| terraform destroy | 刪除資源 | 刪除由 Terraform 管理的資源 |
84+
85+
如果想要看更多關於該指令的說明,可以使用 `terraform <指令> -help` 查詢。

0 commit comments

Comments
 (0)