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

Cloud Storage Gateway:Terraform を使用した CSG の管理

最終更新日:Oct 23, 2025

Terraform は、クラウドリソースを安全かつ効率的に管理するためのオープンソースツールです。このトピックでは、Terraform を使用して Cloud Storage Gateway (CSG) を管理する方法について説明します。

前提条件

  • このトピックの操作を実行するには、最小限の権限を持つ RAM ユーザー を使用することを推奨します。これにより、Alibaba Cloud アカウントの AccessKey ペアが漏洩するリスクを軽減できます。

  • Terraform のランタイム環境が必要です。Terraform は、次のいずれかの方法で使用できます。このトピックでは、ローカルの Linux システムに Terraform をインストールして構成する方法を中心に説明します。

    • Terraform Explorer: Alibaba Cloud はオンラインの Terraform 環境を提供しており、インストールは不要です。ログインするだけで Terraform を使用およびテストでき、追加費用なしで迅速かつ便利にデバッグできます。

    • Cloud Shell: Alibaba Cloud の Cloud Shell には Terraform がプリインストール済みで、認証情報も事前構成されているため、Terraform コマンドを直接実行できます。この方法により、迅速かつコスト効率よく Terraform にアクセスできます。

    • Terraform をローカルにインストールして構成する: この方法は、ネットワーク接続が悪い場合や、開発環境をカスタマイズする必要がある場合に最適です。

ゲートウェイクラスターの作成

  1. 作業ディレクトリを作成し、そのディレクトリに main.tf という名前の構成ファイルを作成します。次に、以下のコードを main.tf にコピーします。

    variable "region"{
      # リソースをデプロイするリージョン。
      default = "cn-hangzhou"
    }
    
    variable "access_key"{
      # AccessKey ID
      default = "yourAccessKeyID"
    }
    
    variable "secret_key"{
      # AccessKey シークレット
      default = "yourAccessKeySecret"
    }
    
    provider "alicloud" {
      access_key = var.access_key
      secret_key = var.secret_key
      region     = var.region
    }
    
    terraform {
      required_providers {
        alicloud = {
          source = "aliyun/alicloud"
          version = "1.240.0"
        }
      }
    }
    
    resource "alicloud_cloud_storage_gateway_storage_bundle" "default" {
      storage_bundle_name = "tf_storage_bundle"
      description = "tf"
    }
  2. 次のコマンドを実行して、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.
  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: 1 added, 0 changed, 0 destroyed.
  4. 結果を検証します。

    作業ディレクトリで次のコマンドを使用して、ゲートウェイクラスターの詳細をクエリします。

    terraform show

    5c7c05cac02e4af2b9fce64deb68b996

ゲートウェイの作成

ファイルゲートウェイ

  1. 新しい作業ディレクトリを作成し、そのディレクトリに main.tf という名前の構成ファイルを作成します。次に、以下のコードを main.tf にコピーします。

    variable "region"{
      # リソースをデプロイするリージョン。
      default = "cn-hangzhou"
    }
    
    variable "access_key"{
      # AccessKey ID
      default = "yourAccessKeyID"
    }
    
    variable "secret_key"{
      # AccessKey シークレット
      default = "yourAccessKeySecret"
    }
    
    provider "alicloud" {
      access_key = var.access_key
      secret_key = var.secret_key
      region     = var.region
    }
    
    terraform {
      required_providers {
        alicloud = {
          source = "aliyun/alicloud"
          version = "1.240.0"
        }
      }
    }
    
    resource "alicloud_cloud_storage_gateway_gateway" "default" {
      release_after_expiration = false
      # ゲートウェイの仕様。必要に応じて変更してください
      gateway_class            = "Basic"
      # ゲートウェイ名。必要に応じて変更してください
      gateway_name             = "tf-terraform-file"
      location                 = "Cloud"
      payment_type             = "PayAsYouGo"
      reason_type              = "File"
      # ゲートウェイクラスター ID に置き換えてください
      storage_bundle_id        = "sb-00095gtq2442k******"
      type                     = "File"
      # vSwitch ID に置き換えてください
      vswitch_id               = "vsw-wz9sd6vfwnh*******"
      # パブリック帯域幅。必要に応じて変更してください
      public_network_bandwidth = 5
    }
  2. 次のコマンドを実行して、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.
  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: 1 added, 0 changed, 0 destroyed.
  4. 結果を検証します。

    作業ディレクトリで次のコマンドを使用して、ファイルゲートウェイの詳細をクエリします。

    terraform show

    fc3753e58aabac1d361a8eb1d021708f

iSCSI ゲートウェイ

  1. 新しい作業ディレクトリを作成し、そのディレクトリに main.tf という名前の構成ファイルを作成します。次に、以下のコードを main.tf にコピーします。

    variable "region"{
      # リソースをデプロイするリージョン。
      default = "cn-hangzhou"
    }
    
    variable "access_key"{
      # AccessKey ID
      default = "yourAccessKeyID"
    }
    
    variable "secret_key"{
      # AccessKey シークレット
      default = "yourAccessKeySecret"
    }
    
    provider "alicloud" {
      access_key = var.access_key
      secret_key = var.secret_key
      region     = var.region
    }
    
    terraform {
      required_providers {
        alicloud = {
          source = "aliyun/alicloud"
          version = "1.240.0"
        }
      }
    }
    
    resource "alicloud_cloud_storage_gateway_gateway" "default" {
      release_after_expiration = false
      # ゲートウェイの仕様。必要に応じて変更してください
      gateway_class            = "Basic"
      # ゲートウェイ名。必要に応じて変更してください
      gateway_name             = "tf-terraform-block"
      location                 = "Cloud"
      payment_type             = "PayAsYouGo"
      reason_type              = "File"
      # ゲートウェイクラスター ID に置き換えてください
      storage_bundle_id        = "sb-00095gtq2442k******"
      type                     = "Iscsi"
      # vSwitch ID に置き換えてください
      vswitch_id               = "vsw-wz9sd6vfwnh*******"
      # パブリック帯域幅。必要に応じて変更してください
      public_network_bandwidth = 5
    }
  2. 次のコマンドを実行して、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.
  3. 次のコマンドを実行します。

    terraform apply

    実行中にプロンプトが表示されたら、yes と入力して [Enter] を押します。コマンドが完了するまで待ちます。次のメッセージが表示された場合、iSCSI ゲートウェイが作成されています。

    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: 1 added, 0 changed, 0 destroyed.
  4. 結果を検証します。

    作業ディレクトリで次のコマンドを使用して、iSCSI ゲートウェイの詳細をクエリします。

    terraform show

    image

キャッシュの作成

  1. 新しい作業ディレクトリを作成し、そのディレクトリに main.tf という名前の構成ファイルを作成します。次に、以下のコードを main.tf にコピーします。

    variable "region"{
      # リソースをデプロイするリージョン。
      default = "cn-hangzhou"
    }
    
    variable "access_key"{
      # AccessKey ID
      default = "yourAccessKeyID"
    }
    
    variable "secret_key"{
      # AccessKey シークレット
      default = "yourAccessKeySecret"
    }
    
    provider "alicloud" {
      access_key = var.access_key
      secret_key = var.secret_key
      region     = var.region
    }
    
    terraform {
      required_providers {
        alicloud = {
          source = "aliyun/alicloud"
          version = "1.240.0"
        }
      }
    }
    
    resource "alicloud_cloud_storage_gateway_gateway_cache_disk" "default" {
      # ゲートウェイ ID に置き換えてください
      gateway_id               = "gw-0009t9hkx******"
      # キャッシュディスクのタイプ。必要に応じて変更してください
      cache_disk_category      = "cloud_essd"
      # キャッシュディスクの容量。必要に応じて変更してください
      cache_disk_size_in_gb    = "100"
      # パフォーマンスレベル。ディスクタイプが ESSD の場合にのみ必要です
      performance_level        = "PL1"
    }
  2. 次のコマンドを実行して、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.
  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: 1 added, 0 changed, 0 destroyed.
  4. 結果を検証します。

    作業ディレクトリで次のコマンドを使用して、キャッシュディスクの詳細をクエリします。

    terraform show

    image

共有またはボリュームの作成

共有の作成

  1. 新しい作業ディレクトリを作成し、そのディレクトリに main.tf という名前の構成ファイルを作成します。次に、以下のコードを main.tf にコピーします。

    variable "region"{
      # リソースをデプロイするリージョン。
      default = "cn-hangzhou"
    }
    
    variable "access_key"{
      # AccessKey ID
      default = "yourAccessKeyID"
    }
    
    variable "secret_key"{
      # AccessKey シークレット
      default = "yourAccessKeySecret"
    }
    
    provider "alicloud" {
      access_key = var.access_key
      secret_key = var.secret_key
      region     = var.region
    }
    
    terraform {
      required_providers {
        alicloud = {
          source = "aliyun/alicloud"
          version = "1.240.0"
        }
      }
    }
    
    resource "alicloud_cloud_storage_gateway_gateway_file_share" "default" {
      # 共有名。必要に応じて変更してください
      gateway_file_share_name = "tf-share-terraform"
      # ゲートウェイ ID に置き換えてください
      gateway_id              = "gw-000e9g71y8o******"
      # 作成したキャッシュディスク ID に置き換えてください
      local_path              = "/dev/vdb"
      # OSS バケットに置き換えてください
      oss_bucket_name         = "test_bucket"
      # OSS エンドポイントに置き換えてください
      oss_endpoint            = "oss-cn-hangzhou-internal.aliyuncs.com"
      # 共有プロトコル、NFS または SMB
      protocol                = "NFS"
    }
  2. 次のコマンドを実行して、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.
  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: 1 added, 0 changed, 0 destroyed.
  4. 結果を検証します。

    作業ディレクトリで次のコマンドを使用して、共有の詳細をクエリします。

    terraform show

    a30a559c5a21c604367a771dbecabd92

ボリュームの作成

  1. 新しい作業ディレクトリを作成し、そのディレクトリに main.tf という名前の構成ファイルを作成します。次に、以下のコードを main.tf にコピーします。

    variable "region"{
      # リソースをデプロイするリージョン。
      default = "cn-hangzhou"
    }
    
    variable "access_key"{
      # AccessKey ID
      default = "yourAccessKeyID"
    }
    
    variable "secret_key"{
      # AccessKey シークレット
      default = "yourAccessKeySecret"
    }
    
    provider "alicloud" {
      access_key = var.access_key
      secret_key = var.secret_key
      region     = var.region
    }
    
    terraform {
      required_providers {
        alicloud = {
          source = "aliyun/alicloud"
          version = "1.240.0"
        }
      }
    }
    
    resource "alicloud_cloud_storage_gateway_gateway_block_volume" "default" {
      # ボリューム名。必要に応じて変更してください
      gateway_block_volume_name = "tfvolume1"
      # ゲートウェイ ID に置き換えてください
      gateway_id              = "gw-000e9g71y8o******"
      # ボリュームの動作モード、キャッシュモードまたは WriteThrough モード
      cache_mode              = "Cache"
      # 作成したキャッシュディスク ID に置き換えてください
      local_path              = "/dev/vdb"
      # OSS バケットに置き換えてください
      oss_bucket_name         = "test_bucket"
      # OSS エンドポイントに置き換えてください
      oss_endpoint            = "oss-cn-hangzhou-internal.aliyuncs.com"
      # ブロックストレージプロトコル
      protocol                = "iSCSI"
      # ストレージ割り当て単位、8K/16K/32K/64K/128K
      chunk_size              = "8192"
      # 容量
      size                    = "100"
    }
  2. 次のコマンドを実行して、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.
  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: 1 added, 0 changed, 0 destroyed.
  4. 結果を検証します。

    作業ディレクトリで次のコマンドを使用して、ボリュームの詳細をクエリします。

    terraform show

    image

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

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

terraform destroy

参考資料