Create a file system using Terraform

更新时间:
复制 MD 格式

Terraform is an open-source tool that you can use to safely and efficiently preview, configure, and manage cloud infrastructure and resources. You can use Terraform to manage Apsara File Storage NAS resources. This topic describes how to use Terraform to create a NAS file system.

Note

The example code in this tutorial supports one-click execution. You can run the code directly. Run with one click

Prerequisites

  • For security, we recommend using a RAM user with the minimum required permissions for this tutorial. For more information, see Create a RAM user and Manage RAM user permissions. The following permission policy grants the minimum permissions required for this tutorial:

    {
      "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": "*"
        }
      ]
    }
  • Prepare a Terraform environment by using one of the following methods.

    • Explorer: You can try Terraform online without installing it. This no-cost method is ideal for quick trials and debugging.

    • Quickly create resources by using Terraform: Alibaba Cloud's Cloud Shell is pre-installed with Terraform and pre-configured with authentication credentials. You can run Terraform commands directly in Cloud Shell. This low-cost method provides quick and convenient access to Terraform.

    • Install and configure Terraform locally: This method is ideal if you have a poor network connection or need to customize your development environment.

Resources

Create a file system

  1. In a new working directory, create a configuration file named main.tf. Copy the following code into the main.tf file.

    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
    }
  2. Run the following command to initialize the Terraform environment.

    terraform init

    The following output indicates a successful initialization.

    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.
  3. Run the following command to apply the configuration:

    terraform apply

    When prompted, enter yes and press Enter. Wait for the command to complete. The following output confirms that the configuration is applied:

    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.
  4. Verify the result.

    Run terraform show

    In your working directory, run the following command to view the details of the resources created by 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
    }

    View in the console

    Log on to the Apsara File Storage NAS console to view the created file system.

Clean up resources

When you no longer need the resources created or managed with Terraform, run the following command to release them. For more information about terraform destroy, see Common commands.

terraform destroy

During execution, Terraform shows a preview of the resources to be destroyed. To confirm, enter yes and press Enter. Wait for the command to complete. The following output indicates that the resources are destroyed:

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.

Complete example

Note

The sample code in this tutorial supports one-click execution. You can run the code directly. One-click Run

Sample code

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
}

For more complete examples, see More complete examples.

References