すべてのプロダクト
Search
ドキュメントセンター

File Storage NAS:Terraform を使用したファイルシステムの作成

最終更新日:Jun 21, 2026

Terraform は、クラウドインフラストラクチャとリソースを安全かつ効率的にプレビュー、設定、管理するために使用できるオープンソースツールです。Terraform を使用して Apsara File Storage NAS (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 環境を準備します。

    • Explorer:Terraform をインストールせずにオンラインで試すことができます。この無料の方法は、簡単な試用やデバッグに最適です。

    • Terraform を使用したリソースの迅速な作成:Alibaba Cloud の Cloud Shell には Terraform がプリインストールされており、認証情報も事前設定されています。Cloud Shell で Terraform コマンドを直接実行できます。この低コストの方法により、Terraform に迅速かつ便利にアクセスできます。

    • Terraform のローカルへのインストールと設定:この方法は、ネットワーク接続が不安定な場合や、開発環境をカスタマイズする必要がある場合に最適です。

リソース

ファイルシステムの作成

  1. 新しい作業ディレクトリに、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
    }
  2. 次のコマンドを実行して、Terraform 環境を初期化します。

    terraform init

    次の出力は、初期化が成功したことを示します。

    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. 次のコマンドを実行して、設定を適用します。

    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.
  4. 結果を確認します。

    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
    }

    コンソールでの表示

    Apsara File Storage NAS コンソールにログインして、作成されたファイルシステムを表示します。

リソースのクリーンアップ

Terraform で作成または管理されているリソースが不要になった場合は、次のコマンドを実行して解放します。terraform destroy の詳細については、「一般的なコマンド」をご参照ください。

terraform destroy

実行中、Terraform は破棄されるリソースのプレビューを表示します。確認するには、「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.

完全な例

説明

このチュートリアルのサンプルコードは、ワンクリック実行に対応しています。コードを直接実行できます。ワンクリックで実行

サンプルコード

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
}

その他の完全な例については、「その他の完全な例」をご参照ください。