Terraform初探:遷移本地項目到Terraform Cloud執行

上一篇文章我們嘗試了在本地環境使用Terraform來創建和管理AWS Lightsail資源,對於管理一些雲資源,我們需要在本地安裝相應的CLI工具和配置訪問相應雲資源的憑據(例如AWS CLI, AccessKeyID等),Terraform通過調用本地的CLI工具或者雲API來管理雲資源的狀態,其默認使用的是local類型的Backend,資源的狀態文件(.tfstate)也是保存在本地文件目錄中的。
這篇文章我們將嘗試使用remote類型的Backend,將項目遷移到Terraform Cloud去執行,並且由Terraform Cloud來管理資源狀態。

什麼是Terraform Cloud

Terraform Cloud 是一個管理Terraform在一致且可靠的環境中運行的SaaS應用,從而可以替換在本地機器是執行Terraform項目,其存儲共享的狀態和機密數據,並可以連接到版本控制系統(如 Git),使得我們可以在團隊中將基礎設施作爲代碼進行工作。
Terraform是一個商業應用,團隊和商業使用將會收取費用並提供了更多的高級功能。但對於個人用戶,可以免費使用基本的功能。關於費用和功能詳情,可以參考 (https://www.hashicorp.com/products/terraform/pricing)。

首先我們需要註冊一個Terraform Cloud的賬號,訪問 https://app.terraform.io/signup/account ,根據提示註冊要給免費的賬號

sigup-page

註冊完成後第一次登陸Terraform Cloud,其會詢問如何開始一個項目,這裏我們選擇 Start from scratch,也就是將從一個空模板開始

start-from-scratch

接下來我們需要創建一個組織(Organization),例如這裏我創建一個名爲 learn-terraform 的組織,一個組織就類似要給命名空間,可以管理多個工作空間(Workspace),還可以管理其下工作空間共享的變量和環境變量。

接下來我們需要在本地環境登錄Terraform Cloud,並添加相應的配置來重新初始化項目。

重新初始項目

完成了Terraform Cloud的賬號註冊之後,我們需要在本地終端運行 terraform login ,會打開瀏覽器來登錄賬號得到一個Token值,將其複製填入終端完成登錄

> terrafrom login
Terraform must now open a web browser to the tokens page for app.terraform.io.

If a browser does not open this automatically, open the following URL to proceed:
    https://app.terraform.io/app/settings/tokens?source=terraform-login


---------------------------------------------------------------------------------

Generate a token using your browser, and copy-paste it into this prompt.

Terraform will store the token in plain text in the following file
for use by subsequent commands:
    /home/mengz/.terraform.d/credentials.tfrc.json

Token for app.terraform.io:
  Enter a value:

接着我們修改項目的配置文件 main.tf ,加入 backend "remote"

terraform {
  backend "remtoe" {
    organization = "learn-terraform"
    workspaces {
      name = "mylightsail"
    }
  }

  ...
}

執行 terraform init ,Terraform將下載remote的插件,連接至Terraform Cloud 的 learn-terraform/mylightsail 工作空間,並將本地的狀態文件遷移到雲端

$ terraform init

Initializing the backend...Do you want to copy existing state to the new backend?  Pre-existing state was found while migrating the previous "local" backend to the  newly configured "remote" backend. No existing state was found in the newly  configured "remote" backend. Do you want to copy this state to the new "remote"  backend? Enter "yes" to copy and "no" to start with an empty state.
  Enter a value: yes
Releasing state lock. This may take a few moments...
Successfully configured the backend "remote"! Terraform will automaticallyuse this backend unless the backend configuration changes....

瀏覽器訪問Terraform Cloud WebUI,進入相應的工作空間可以查看狀態信息。

完成之後可以將本地的 .terraform/terraform.tfstate 文件刪除。本地項目已將Terraform Cloud作爲遠程後端(remote backend),並且關聯了命令行(CLI)驅動的方式,因此後續可以在本地更新資源配置文件,然後在本地運行 plan & apply 命令,這將會觸發在遠端Cloud上執行具體的狀態維護工作。不過要使用Terraform Cloud來執行狀態維護,我們還需要將AWS的訪問憑據配置到Terraform Cloud上。

配置工作空間的環境變量

使用Terraform Cloud來維護雲資源(例如AWS),我們需要配置相應的訪問憑據。這裏我們需要配置AWS的 AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY 作爲項目空間的環境變量。
在工作空間點擊 Variables 標籤頁,點擊 + Add Varaible 按鈕

ad-varaible

選擇 Environment Variables ,然後添加 AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY 兩個環境變量,並設置相應的值。

完成之後,我們就可以本地控制檯運行 terraform planterraform apply 將操作發送到Terraform Cloud端去運行,當然我們還是可以在本地項目執行 terraform show 來查看當前的狀態,狀態將會管理在雲端

terraform-cloud-state

> terraform plan
Running plan in the remote backend. Output will stream here. Pressing Ctrl-C
will stop streaming the logs, but will not stop the plan running remotely.

Preparing the remote plan...

To view this run in a browser, visit:
https://app.terraform.io/app/mengz-infra/my-lightsail/runs/run-LzwFBbihffEKmucd

Waiting for the plan to start...

Terraform v1.0.11
on linux_amd64
Configuring remote state backend...
Initializing Terraform configuration...
aws_lightsail_static_ip.outline-sig-ip: Refreshing state... [id=Outline-EIP]
aws_lightsail_instance.outline-sig: Refreshing state... [id=Outline-Sig]
aws_lightsail_instance_public_ports.outline-sig-public-ports: Refreshing state... [id=Outline-Sig-987241840]
aws_lightsail_static_ip_attachment.outline-sig-ip-attache: Refreshing state... [id=Outline-EIP]

No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.

可以看到計劃是在 remote backend 運行的。

本版化管理項目

最後,我們可以將項目的配置文件提交到版本控制系統(例如 Gitlab),並配置工作空間的版本控制

terraform-cloud-version-control

在Terraform Cloud工作空間的設置裏,按照提示配置關聯相應的版本管理代碼倉庫。完成之後,我們在本地提交更新的代碼後,會自動觸發Terraform Cloud去執行維護新的狀態。不過這將不會允許在本地終端執行 terraform apply

> terraform apply

│ Error: Apply not allowed for workspaces with a VCS connection
│ A workspace that is connected to a VCS requires the VCS-driven workflow to ensure that the VCS remains the single source of truth.

只能通過更新代碼,然後提交到遠程代碼倉庫的方式來觸發狀態維護。這將更加便於與團隊共享基礎設施代碼,以及共同維護基礎設施狀態,同時也更加趨於GitOps的工作方式。

總結

本文基於上一篇文章 - 嘗試使用Terraform在本地環境管理AWS Lightsail資源,延申了將狀態管理的操作遷移到以Terraform Cloud作爲遠程後端的嘗試,除了Terraform Cloud之後,還有其他類型的Backend,可以參考 (https://www.terraform.io/docs/language/settings/backends/index.html)。
自此,我們初探了使用Terraform作爲IaC工具來管理AWS Lightsail資源,當作Terraform學習的一個入門。Hashicrop官方提供了更多的學習資源和文檔,想深入學習Terrform,並投入到實際工作中,還請參考官方文檔

【同時發佈在 Terraform初探:遷移本地項目到Terraform Cloud執行

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章