身為DevOps工程師,會Terraform也是很正常的(一) — 初出茅廬篇

身為DevOps工程師
10 min readMar 5, 2022

--

Photo by Brett Jordan on Unsplash

在現在的大雲端時代,人人都可以是雲端王?
對於Infrastructure建置的需求與機會越來越多的情況下。只要公司有AWS(Amazon Web Services), GCP(Google Cloud Platform)這種公有雲帳號,任何人都可以簡單的從網頁上,點個滑鼠就建置出一套叢集出來。在普通的情況下,或許這樣沒錯,但這樣子建出來的叢集中,都是用預設值來對叢集做設定的。舉例來說,假如我今天要在GCP上面建立一套GKE(Google Kubernetes Engine)起來,甚至連名字我都可以不用改,直接按下建立,我就可以有一套GKE可以用囉~

前面的情況聽起來一切都很美好,世上最美好的事莫過於我什麼都不用做就可以吃到甜美的果實。但事實上,有時候總是會有需要客製化的時候,當需要修改某些設定才能夠正確的建立時,建立這些資源的過程就會變得更複雜且瑣碎。如果一直都是同一個人來處理或許不太會做錯,但如果這件事情都要綁定在同一個人身上的話也不是一件好事,小朋友都知道不要把雞蛋放在同一個籃子上。

對於大家來說,事情一定是越簡單越好,那是不是有一種方法,是讓我可以直接把我要在網頁上點滑鼠還有要調整的設定,存成一個檔案的方式,在我需要的時候,我可以直接用檔案來部署出我要的Infrastructure呢?我相信大家都知道,我一定會說有的,沒錯,這也是我今天要跟大家介紹的主題,Terraform能夠帶來的好處之一。

讓我們重新整理一下,我可以將我想要部署的環境所需要的設定,藉由檔案的方式來重複利用,其實那些檔案就是程式碼。這個觀念就是目前很常聽到的IaC(Infrastructure as Code)。而Terraform就這提供這樣功能的一個產品,也是時候該上主菜了。

Terraform安裝

我的環境是用MAC來開發與測試,如果你也是的話,恭喜你。
直接簡單的用homebrew就可以安裝好了。

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

不是MAC的朋友也不用擔心,三種最常見的作業系統Terraform都有支援的。
Linux或Windows的朋友請參考以下網站

https://learn.hashicorp.com/tutorials/terraform/install-cli

安裝完成後,可以試試看輸入。

$ terraform -h

如果你看到一堆command的說明,恭喜你得到它了。

Terraform基礎概念

如前面所提到的,Terraform讓我們藉由編寫檔案的方式,這些檔案是藉由HCL(HashiCorp Configuration Language)的語法來做撰寫。替我們部署出想要的資源出來。在支援度來說,目前你所想到的公有雲平台,Terraform應該都有支援了。大家或許會覺得納悶,我們編寫的檔案都是HCL語法,為什麼可以對應到各個不同的雲平台,難道每個平台都特地開專門的API給Terraform嗎?

其實不是的,在Terraform的架構中,每一個不同的平台都有對應的Provider來做處理,像是有aws的provider也有google的provider。藉由provider成為了中間人的方式,替我們將HCL格式的程式碼轉換成request,發送到aws或google平台對應的API。

使用Terraform的Workflow主要都圍繞著以下三步:
1. coding
根據我們自己的需求去撰寫.tf檔

2. terraform plan
Terraform提供的Command,能讓我們知道目前的.tf檔會造成哪些改變,是很實用的指令。

3. terraform apply
Terraform提供的Command,也會提供目前的.tf檔會造成哪些改變的資訊,在最後會發出一個如果確定要,請yes的確認,問你是不是真的要部署下去。

牛刀小試一下

Terraform config支援以下的格式建立resource,主要分成
- resource
- variable
- output

variable “<variable_name>” {
type = <variable_type>
}
resource “<provider>_<resource_type>” “<resource_name>” {
<Argument_key> = <Argument_value>
}
output “variable_name” {
value = <value>
}

建一個檔案先

廢話不多說,來人!上code
這是一段簡單的HCL格式的程式碼,在這段程式碼中,會在/tmp 目錄下建出一個名為test的檔案。而檔案的內容會是 “abc”。

variable "content" {
type = string
default = "abc"
}
resource "local_file" "myfile" {
filename = "/tmp/test"
content = var.content
}
output "file-content" {
value = local_file.myfile.content
}

編寫完成後,讓我們來執行看看吧!
由於我們是第一次執行,總共會執行三行指令。
- terraform init
- terraform plan
- terraform apply

  1. 在存放code的目錄下,執行init來安裝對應的providers。
$ terraform init
Initializing the backend…
Initializing provider plugins…
- Finding latest version of hashicorp/local…
- Installing hashicorp/local v2.1.0…
- Installed hashicorp/local v2.1.0 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run “terraform init” in the future.
Terraform has been successfully initialized!You may now begin working with Terraform. Try running “terraform plan” to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

2. 執行plan來確認目前這樣的設定下,會發生什麼樣的變動。

$ terraform plan
Terraform used the selected providers to generate the following execution plan. Resource
actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:# local_file.myfile will be created
+ resource “local_file” “myfile” {
+ content = “abc”
+ directory_permission = “0777”
+ file_permission = “0777”
+ filename = “/tmp/test”
+ id = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.Changes to Outputs:
+ file-content = “abc”
────────────────────────────────────────────────────────────────────────────────────────Note: You didn’t use the -out option to save this plan, so Terraform can’t guarantee to
take exactly these actions if you run “terraform apply” now.

3. 執行apply的過程中,仍然會出現這次會發生什麼樣的變動,在確認沒問題後,按下yes。如果有發現不對勁在yes之前都還來得及。

$ terraform apply
Terraform used the selected providers to generate the following execution plan. Resource
actions are indicated with the following symbols:
+ create
Terraform will perform the following actions: # local_file.myfile will be created
+ resource “local_file” “myfile” {
+ content = “abc”
+ directory_permission = “0777”
+ file_permission = “0777”
+ filename = “/tmp/test”
+ id = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.Changes to Outputs:
+ file-content = “abc”
Do you want to perform these actions?
Terraform will perform the actions described above.
Only ‘yes’ will be accepted to approve.
Enter a value: yes

按下yes後,就會開始執行啦!
這時可以看到terraform正在建立什麼東西,在建立完成後,就會顯示多少資源被建立, 修改, 刪除了。

local_file.myfile: Creating…
local_file.myfile: Creation complete after 0s [id=fe05bcdcdc4928012781a5f1a2a77cbb5398e106]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:file-content = “abc”

由於我們有設定output是檔案的內容,這邊顯示是 abc。讓我們來確認看看有沒有被忽悠吧!

$ cat /tmp/test
abc

很好,看起來是真的。
以上大家對於terraform應該有基礎的認識了!
在後續的章節,我們會帶大家慢慢的去認識terraform的眉眉角角。
如果教學中有哪邊不清楚地都歡迎留言討論。
最後很感謝大家的收看。

下台一鞠躬

--

--

身為DevOps工程師

目前在蓋亞資訊擔任DevOps Consultant。最近才從後端的世界轉成投向DevOps的懷抱,目前專注在Kubernetes, GitLab, DevSecOps的學習中。