37 · Introducing Network Programmability網路可程式化簡介

TerraformTerraform

HashiCorp Terraform is a declarative Infrastructure-as-Code (IaC) tool that allows you to define public cloud and on-premises resources using human-readable configuration files. These files can be written in HashiCorps proprietary language called Hashicorp Configuration Language (HCL) or JSON. Terraform employs a state-driven resource model to define the desired state of infrastructure components and services. It is used to automate the provisioning and management of a company's cloud and on-premises resources in a declarative and consistent manner, all in an agentless architecture. A notable difference between Terraform and Ansible is that Terraform is written in GO. Terraform was first released in 2014 as an open-source tool but later moved away from its longstanding open-source licensing and is now under the Business Source License (BSL) 1.1 which is more restrictive in terms of commercial use.

HashiCorp Terraform 是一套宣告式的基礎架構即程式碼(Infrastructure-as-Code,IaC)工具,可讓你使用人類可讀的設定檔來定義公有雲與地端資源。這些檔案可以用 HashiCorp 專有的 HashiCorp Configuration Language(HCL)或 JSON 撰寫。Terraform 採用狀態驅動的資源模型,來定義基礎架構元件與服務的期望狀態。它以宣告式且一致的方式,用來自動化公司雲端與地端資源的佈建與管理,且整體架構為無代理程式(agentless)。Terraform 與 Ansible 之間一個顯著差異是,Terraform 以 GO 語言撰寫。Terraform 於 2014 年首次以開放原始碼工具發行,但後來脫離其長期採用的開放原始碼授權,目前採用 Business Source License(BSL)1.1,在商業使用方面限制較多。

Terraform's agentless architecture simplifies the adoption of infrastructure automation. Since Terraform is agentless, integrations can use REST APIs, NETCONF, SSH, or even SNMP, if desired. This allows for seamless integration with a wide range of cloud services, on-premises solutions, and SaaS platforms, enabling consistent and scalable infrastructure management.

Terraform 無代理程式的架構簡化了基礎架構自動化的導入。由於 Terraform 是無代理程式的,整合可使用 REST API、NETCONF、SSH,甚至 SNMP,視需求而定。這使其能與各種雲端服務、地端解決方案及 SaaS 平台無縫整合,實現一致且可擴充的基礎架構管理。

The key Terraform components are:

Terraform 的關鍵元件包括:

  • Configurationfiles: Used to define the desired state (what should be configured in the real-world at the end of the process) of the infrastructure using so-called resource blocks. Resources can be network devices or their components, virtual machines, or even higher-level objects like DNS entries.Configurationfiles(設定檔):使用所謂的資源區塊(resource block)來定義基礎架構的期望狀態(流程結束時實際世界中應設定的內容)。資源可以是網路裝置或其元件、虛擬機器,甚至是更高層級的物件,例如 DNS 項目。
  • State file:Used to keep track of the real-world resources that are currently deployed. By comparing the desired state (defined in your configuration files) to the current state (stored in the state file), Terraform can determine the necessary actions to update the real-world infrastructure to match the desired state.State file(狀態檔):用來追蹤目前已部署的實際世界資源。透過比較期望狀態(定義在設定檔中)與目前狀態(儲存在狀態檔中),Terraform 可以判斷更新實際基礎架構以符合期望狀態所需採取的動作。
  • Providers: Providers are the components that do all the work in Terraform. Every supported service or infrastructure platform has a provider that defines which resources are available and performs API calls to manage those resources.Providers(提供者):提供者是 Terraform 中執行所有實際工作的元件。每個受支援的服務或基礎架構平台都有一個提供者,用來定義有哪些資源可用,並執行 API 呼叫以管理這些資源。

How Terraform Works

Terraform 的運作方式

How Terraform works:

Terraform 的運作方式:

  1. Plan: Terraform identifies the differences between the current state (in the state file) and the desired state (in the configuration files) and determines the necessary actions to align them.Plan(規劃):Terraform 找出目前狀態(在狀態檔中)與期望狀態(在設定檔中)之間的差異,並決定使其一致所需的動作。
  2. Apply: Terraform executes the planned actions.Apply(套用):Terraform 執行規劃好的動作。
  3. Consolidate: State file is updated to reflect the current state of the infrastructure.Consolidate(整合):更新狀態檔以反映基礎架構的目前狀態。

A DevOps or network engineer—Terraform administrator—typically defines the configuration files on a Terraform controller, a machine where Terraform is installed. A Terraform workflow consists of plan and apply phases, both triggered by an administrator manually or by an automated pipeline on the Terraform controller. During the planning phase, Terraform calculates the differences between the current state (defined in the state file) and the desired state (defined in the configuration files) and determines the necessary actions to achieve the desired state. The apply phase is triggered once the intended changes are verified. During this phase, Terraform executes the required actions to align the infrastructure with the desired state specified in the configuration files. Once the changes are applied, the state and configuration files are consolidated (state file is updated to reflect the current state of the infrastructure).

DevOps 或網路工程師——即 Terraform 管理員——通常會在 Terraform 控制器(安裝 Terraform 的機器)上定義設定檔。Terraform 的工作流程包含 plan(規劃)與 apply(套用)兩個階段,兩者都由管理員在 Terraform 控制器上手動觸發,或由自動化管線觸發。在規劃階段,Terraform 會計算目前狀態(定義於狀態檔)與期望狀態(定義於設定檔)之間的差異,並決定達成期望狀態所需的動作。一旦驗證了預期變更,就會觸發套用階段。在此階段,Terraform 會執行必要的動作,使基礎架構與設定檔中指定的期望狀態一致。變更套用後,狀態檔與設定檔會被整合(更新狀態檔以反映基礎架構的目前狀態)。

Key Properties of Terraform

Terraform 的關鍵特性

Remember these key properties of Terraform:

請記住以下 Terraform 的關鍵特性:

  • Terraform is a declarative tool initially designed to automate the provisioning of cloud resources across multiple public cloud providers, and was extended to network applications.Terraform 是一種宣告式工具,最初設計用於自動化跨多個公有雲提供者的雲端資源佈建,後來擴展到網路應用程式。
  • Terraform excels at managing resources in an immutable fashion.Terraform 擅長以不可變(immutable)方式管理資源。
  • It uses an agentless push model (easy to adopt).它使用無代理程式的推送模型(易於採用)。
  • It uses a configuration language known as HashiCorp Configuration Language (HCL), or JSON.它使用稱為 HashiCorp Configuration Language(HCL)或 JSON 的設定語言。
    • An example of a Terraform configuration for provisioning interface configurations can be seen in the figure.圖中可看到一個用於佈建介面設定的 Terraform 設定範例。
What is the primary configuration language used by Terraform to define infrastructure as code?Terraform 用來定義基礎架構即程式碼的主要設定語言是什麼?
Which Terraform component is responsible for interfacing with various services and infrastructure platforms to manage resources?哪個 Terraform 元件負責與各種服務及基礎架構平台介接以管理資源?