天天看點

【最佳實踐】通過Terraform 管理OSS資源1.Terraform簡介2.OSS的Terraform Module都能夠提供哪些操作?3.安裝Terraform4.建立配置檔案5.初始化工作目錄6.執行Terraform指令7.Bucket相關配置操作示例8.其他Bucket配置操作示例:9.Object相關配置操作示例

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 apply

terraform 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相關配置操作示例

繼續閱讀