Terraform是一種開源工具,用於安全高效地預覽、配置和管理雲基礎架構和資源。您可以使用Terraform管理Apsara File Storage NAS資源。本文為您示範如何通過Terraform建立一個NAS檔案系統。
本教程所含範例程式碼支援一鍵運行,您可以直接運行代碼。一鍵運行
前提條件
為了降低資訊安全風險,建議使用最小許可權的RAM使用者完成此教程的操作。請參見建立RAM使用者與管理RAM使用者的許可權,完成此教程所需最小許可權的權限原則如下:
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": [ "nas:CreateFileSystem", "nas:DeleteFileSystem", "nas:DescribeFileSystems", "nas:GetRecycleBinAttribute", "nas:ListTagResources", "nas:DescribeNfsAcl", "nas:EnableRecycleBin", "nas:UpdateRecycleBinAttribute", "nas:EnableNfsAcl" ], "Resource": "*" } ] }準備Terraform運行環境,您可以選擇以下任一方式來使用Terraform。
Explorer:阿里雲提供了Terraform的線上運行環境,您無需安裝Terraform,登入後即可線上使用和體驗Terraform。適用於零成本、快速、便捷地體驗和調試Terraform的情境。
使用Terraform快速建立資源:阿里雲Cloud Shell中預裝了Terraform的組件,並已配置好身份憑證,您可直接在Cloud Shell中運行Terraform的命令。適用於低成本、快速、便捷地訪問和使用Terraform的情境。
在本地安裝和配置Terraform:適用於網路連接較差或需要自訂開發環境的情境。
使用的資源
alicloud_nas_file_system:NAS檔案系統。
建立檔案系統
建立一個工作目錄,並在該工作目錄中建立名為main.tf的設定檔,然後將以下代碼複製到main.tf中。
variable "region" { default = "cn-hangzhou" } provider "alicloud" { region = var.region } data "alicloud_nas_zones" "default" { file_system_type = "standard" } resource "random_integer" "default" { min = 10000 max = 99999 } resource "alicloud_nas_file_system" "default" { protocol_type = "NFS" storage_type = "Capacity" description = "nas_system_${random_integer.default.result}" encrypt_type = 1 file_system_type = "standard" recycle_bin { status = "Enable" reserved_days = "10" } nfs_acl { enabled = true } zone_id = data.alicloud_nas_zones.default.zones.0.zone_id }執行如下命令,初始化Terraform運行環境。
terraform init返回資訊如下,則Terraform初始化成功。
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.執行如下命令,開始執行代碼。
terraform apply在執行過程中,根據提示輸入
yes並按下Enter鍵,等待命令執行完成,若出現以下資訊,則表示代碼執行成功。You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure. 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 Apply complete! Resources: 2 added, 0 changed, 0 destroyed.驗證結果。
執行terraform show命令
您可以在工作目錄中,使用以下命令查詢Terraform已建立資源的詳細資料:
terraform show# alicloud_nas_file_system.default: resource "alicloud_nas_file_system" "default" { capacity = 104 create_time = "2025-01-13T15:21:16CST" description = "nas_system_xxx" encrypt_type = 1 file_system_type = "standard" id = "69edxxx" kms_key_id = "9f4e9b5c-7873-420e-9aaf-xxx" protocol_type = "NFS" resource_group_id = "rg-xxx" status = "Running" storage_type = "Capacity" tags = {} vpc_id = null zone_id = "cn-hangzhou-f" nfs_acl { enabled = true } recycle_bin { enable_time = "2025-01-13T07:21:21Z" reserved_days = 10 secondary_size = 0 size = 0 status = "Enable" } } # random_integer.default: resource "random_integer" "default" { id = "xxx" max = 99999 min = 10000 result = xxx }登入控制台查看
登入檔案儲存體控制台,查看已建立的檔案系統。
清理資源
當您不再需要上述通過Terraform建立或管理的資源時,請運行以下命令以釋放資源。關於terraform destroy的更多資訊,請參見常用命令。
terraform destroy在執行過程中,預覽將刪除的資源,確定刪除請根據提示輸入yes並按下Enter鍵,等待命令執行完成,若出現以下資訊,則表示資源刪除成功。
Plan: 0 to add, 0 to change, 2 to destroy.
Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
...
Destroy complete! Resources: 2 destroyed.完整樣本
本教程所含範例程式碼支援一鍵運行,您可以直接運行代碼。一鍵運行
範例程式碼
如果您想體驗更多完整樣本,請前往更多完整樣本。