All Products
Search
Document Center

File Storage NAS:Create a file system with Terraform

Last Updated:Jun 23, 2026

You can use Terraform to manage Apsara File Storage NAS resources. This topic walks you through creating a NAS file system with Terraform.

Note

Click Run to execute the code.

Prerequisites

  • To mitigate security risks, use a RAM user with the minimum permissions required 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 runtime environment. You can use Terraform in any of the following ways:

Resources used

Create a file system

  1. Create a working directory and a configuration file named main.tf within it. Then, 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 runtime environment.

    terraform init

    If the following message appears, Terraform is successfully initialized.

    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

    During execution, enter yes when prompted and press the Enter key. If the following message appears, the configuration is applied successfully.

    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 results.

    Terraform show

    In your working directory, run the following command to view details about the created resources:

    terraform show

    image

    Console

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

    image

Clean up resources

When you no longer need the resources, run the following command to destroy them. For more information about the terraform destroy command, see Common commands.

terraform destroy

During execution, a preview of the resources to be deleted is displayed. Enter yes at the prompt and press the Enter key. If the following message appears, the resources are deleted.

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

Click Run to execute the code.

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
}

To explore more complete examples, go to More Examples.