【最佳實踐】通過Terraform管理OSS資源

figo168hf發表於2018-11-30

1.Terraform簡介

   Terraform 是一個開源的自動化的資源編排工具,支援多家雲服務提供商。阿里雲作為第三大雲服務提供商,terraform-alicloud-provider 已經支援了超過 90 多個 Resource 和 Data Source,覆蓋20多個服務和產品,吸引了越來越多的開發者加入到阿里雲Terraform生態的建設中。

  HashiCorp Terraform 是一個IT基礎架構自動化編排工具,可以用程式碼來管理維護 IT 資源。Terraform的命令列介面 (CLI) 提供一種簡單機制,用於將配置檔案部署到阿里雲或其他任意支援的雲上,並對其進行版本控制。它編寫了描述雲資源拓撲的配置檔案中的基礎結構,例如虛擬機器、儲存帳戶和網路介面。Terraform 的命令列介面(CLI)提供一種簡單機制,用於將配置檔案部署到阿里雲或任何其他支援的雲並對其進行版本控制。

  Terraform是一個高度可擴充套件的工具,通過 Provider 來支援新的基礎架構。您可以使用Terraform來建立、修改、刪除ECS、VPC、RDS、SLB等多種資源。

2.OSS的Terraform Module都能夠提供哪些操作?

  目前阿里雲已經將OSS的module釋出到了GitHub上。目前OSS提供的主要功能有Bucket管理,以及日常的檔案物件管理。例如:

  • Bucket 管理功能:
1.建立Bucket
2.設定Bucket ACL
3.設定Bucket CORS
4.設定Bucket Logging
5.設定Bucket 靜態網站託管
6.設定Bucket Referer
7.設定Bucket  Lifecycle
  • Object管理功能:
1.檔案上傳
2.設定檔案服務端加密方式
3.設定ACL
4.設定物件後設資料資訊

1.OSS Module在GitHub上下載地址:GitHub下載地址
2.OSS Terraform Module介紹:Module介紹

  接下來我們就從簡單的示例 開始瞭解,Terraform是如何管理Bucket和檔案物件。

3.安裝Terraform

  在使用Terraform的簡單模板語言定義、預覽和部署雲基礎結構前,您需要安裝預配置Terraform。

【安裝步驟】如下:

1.前往 Terraform官網 下載適用於您的作業系統的程式包;
2.將程式包解壓到/usr/local/bin。如果將可執行檔案解壓到其他目錄,則需要將路徑加入到全域性變數:
3.執行 terraform 驗證路徑配置。

將顯示可用的Terraform選項的列表,類似如下所示,表示安裝完成。
username:~$ terraform
Usage: terraform [-version] [-help] <command> [args]

4.為提高許可權管理的靈活性和安全性,建議您建立RAM使用者,併為其授權。

**注意**:請不要使用主賬號進行操作!!!

  常見的terraform命令是 terraform init, terraform plan, terraform apply

4.建立配置檔案

  需要為每個terraform專案建立1個獨立的執行目錄。所以,我們建立terraform-test目錄。該目錄下所有*.tf 檔案都會被terraform載入,因此,在初始化配置之前需要有1個.tf檔案。

mkdir terraform-test
cd terraform-test

  Terraform在執行時,會讀取該目錄空間下所有.tf以及.tfvars 檔案。因此,沒有必要將所有配置資訊寫在1個配置檔案中。使用者可以按照實際用途將配置資訊寫入到不同的檔案中。例如:

provider.tf                -- provider 配置
terraform.tfvars           -- 配置 provider 要用到的變數
varable.tf                  -- 通用變數
resource.tf                 -- 資源定義
data.tf                     -- 包檔案定義
output.tf                    -- 輸出

Step1:如下我們將建立provider.tf 檔案存放使用者的身份認證資訊。

provider "alicloud" {
    region           = "cn-hanghzou"
    access_key  = "your-access-key-here"
    secret_key   = "your-secret-key-here"
}
 說明:以下示例中都會用到provider.tf檔案,請不要刪除該檔案。此外,建議將AK資訊單獨存放到provider.tf檔案,不建議將AK資訊與物件操作放在同一個tf檔案中。

Step2:我們以建立1個Bucket為例。如下我們建立1個test.tf檔案,內容如下:

resource "alicloud_oss_bucket" "bucket-acl"{
  bucket = "figo-chen-2020"
  acl = "private"
}

4.1配置檔案介紹

4.1.1″alicloud_oss_bucket”介紹

  您可以從該連結檢視到bucket所有配置資訊。 如果bucketfigo-chen-2020不存在,則執行 terraform apply 後將自動建立該bucket。若已經存在Bucket,則強制進行重新命名操作。

注意:資源的操作行為與Terraform的狀態有關。若無,則建立該Bucket,若有,則強制進行重新命名操作。重新命名操作會被拆分為“刪除+新建”2個操作步驟。

5.初始化工作目錄

  新建terraform工作目錄,並建立配置檔案後。terraform applyterraform plan 等命令是無法執行的。需要先進行初始化操作。

Terraform init

  執行 terraform init命令後,會在當前目錄建立.terraform目錄。並依據 *.tf檔案中的配置資訊下載對應的外掛。

   示例輸出資訊如下:

root@figo-hangzhou:~/terraform-test# terraform  init

Initializing provider plugins...
- Checking for available provider plugins on https://releases.hashicorp.com...
- Downloading plugin for provider "alicloud" (1.24.0)...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.alicloud: version = "~> 1.24"

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.

6.執行Terraform命令

  1. 新建配置檔案,並執行初始化後,就可以執行相關Terraform命令了。Terraform提供了預覽功能,允許在正式執行之前檢視將要執行那些操作。

terraform plan

  輸出資訊如下:

root@figo-hangzhou:~/terraform-test# terraform  plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


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

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + alicloud_oss_bucket.bucket-acl
      id:                <computed>
      acl:               "private"
      bucket:            "figo-chen-2020"
      creation_date:     <computed>
      extranet_endpoint: <computed>
      intranet_endpoint: <computed>
      location:          <computed>
      logging_isenable:  "true"
      owner:             <computed>
      referer_config.#:  <computed>
      storage_class:     <computed>


Plan: 1 to add, 0 to change, 0 to destroy.

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

Note: You didn`t specify an "-out" parameter to save this plan, so Terraform
can`t guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

  2.若要執行工作目錄中的配置檔案,請執行如下命令:

terraform apply

  輸出資訊如下:

root@figo-hangzhou:~/terraform-test# terraform  apply

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + alicloud_oss_bucket.bucket-acl
      id:                <computed>
      acl:               "private"
      bucket:            "figo-chen-2020"
      creation_date:     <computed>
      extranet_endpoint: <computed>
      intranet_endpoint: <computed>
      location:          <computed>
      logging_isenable:  "true"
      owner:             <computed>
      referer_config.#:  <computed>
      storage_class:     <computed>


Plan: 1 to add, 0 to change, 0 to destroy.

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

alicloud_oss_bucket.bucket-acl: Creating...
  acl:               "" => "private"
  bucket:            "" => "figo-chen-2020"
  creation_date:     "" => "<computed>"
  extranet_endpoint: "" => "<computed>"
  intranet_endpoint: "" => "<computed>"
  location:          "" => "<computed>"
  logging_isenable:  "" => "true"
  owner:             "" => "<computed>"
  referer_config.#:  "" => "<computed>"
  storage_class:     "" => "<computed>"
alicloud_oss_bucket.bucket-acl: Creation complete after 1s (ID: figo-chen-2020)

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

  如上已經成功的建立了 figo-chen-2020 這個bucket,並且ACL是Private。

  3.若要顯示刪除已存在的資源,請執行如下命令:

terraform  destroy

7.Bucket相關配置操作示例

7.1設定Bucket Access-log示例:

resource "alicloud_oss_bucket" "that"{
  bucket = "figo-chen-2019"
  acl = "private"
}

resource "alicloud_oss_bucket" "bucket-logging" {
  bucket = "figo-chen-2018"
  acl = "private"

  logging {
    target_bucket = "${alicloud_oss_bucket.that.bucket}"
    target_prefix = "log/"
  }

  logging_isenable = true
}

說明:若Bucket不是通過terraform建立。則通過 如下命令匯入現有的Bucket。

terraform import alicloud_oss_bucket.bucket bucket-name

1.編輯mail.tf檔案,在此檔案中編輯匯入Bucket的ACL等其他資訊。例如:

resource "alicloud_oss_bucket" "bucket"{
  bucket = "test-hangzhou-2025"
  acl = "private"
}

2.執行terraform import命令,如下是匯入“test-hangzhou-2025”這個bucket的命令:

terraform import alicloud_oss_bucket.bucket test-hangzhou-2025

8.其他Bucket配置操作示例:

9.Object相關配置操作示例


相關文章