Manage CSG with Terraform

Updated at:

Terraform is an open-source tool for securely and efficiently creating and managing cloud resources. This topic explains how to manage Cloud Storage Gateway (CSG) with Terraform.

Prerequisites

  • To reduce security risks, we recommend using a RAM user with the minimum required permissions to complete this tutorial. For more information, see RAM authorization.

  • A Terraform runtime environment is required. You can use Terraform in one of the following ways. The examples in this topic assume that Terraform is installed and configured on a local Linux system.

    • Terraform Explorer: Alibaba Cloud provides an online environment where you can run Terraform without installation. This method is ideal for quickly and conveniently trying out and debugging Terraform at no cost.

    • Create resources with Terraform: Alibaba Cloud Cloud Shell comes with pre-installed Terraform components and pre-configured credentials. You can run Terraform commands directly in Cloud Shell. This method is ideal for low-cost, fast, and convenient access to Terraform.

    • Install and configure Terraform locally: This method is ideal if you have poor network connectivity or need a custom development environment.

Resources used

Create a gateway cluster

  1. Create a working directory and a file named main.tf inside it. Then, copy the following code into main.tf.

    variable "region"{
      # The region where the resource is deployed.
      default = "cn-hangzhou"
    }
    variable "access_key"{
      # Your AccessKey ID.
      default = "yourAccessKeyID"
    }
    variable "secret_key"{
      # Your AccessKey secret.
      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. Run the following command to initialize the Terraform runtime environment.

    terraform init

    The following output indicates that the initialization is successful.

    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 indicates that the gateway cluster is created.

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

    Run the following command to view the details of the gateway cluster:

    terraform show
    root@f8b63f957679:~/tf# terraform show
    # alicloud_cloud_storage_gateway_storage_bundle.default:
    resource "alicloud_cloud_storage_gateway_storage_bundle" "default" {
        description        = "tf"
        id                 = "sb-0006tmifu2fe7"
        storage_bundle_name = "tf_storage_bundle"
    }

Create a gateway

File gateway

  1. Create a working directory and a file named main.tf inside it. Then, copy the following code into main.tf.

    variable "region"{
      # The region where the resource is deployed.
      default = "cn-hangzhou"
    }
    variable "access_key"{
      # Your AccessKey ID.
      default = "yourAccessKeyID"
    }
    variable "secret_key"{
      # Your AccessKey secret.
      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
      # The gateway specification. Modify this value as needed.
      gateway_class            = "Basic"
      # The gateway name. Modify this value as needed.
      gateway_name             = "tf-terraform-file"
      location                 = "Cloud"
      payment_type             = "PayAsYouGo"
      reason_type              = "File"
      # Replace the value with your gateway cluster ID.
      storage_bundle_id        = "sb-00095gtq2442k******"
      type                     = "File"
      # Replace the value with your VSwitch ID.
      vswitch_id               = "vsw-wz9sd6vfwnh*******"
      # The public network bandwidth. Modify this value as needed.
      public_network_bandwidth = 5
    }
  2. Run the following command to initialize the Terraform runtime environment.

    terraform init

    The following output indicates that the initialization is successful.

    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 indicates that the file gateway is created.

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

    Run the following command to view the details of the file gateway:

    terraform show
    root@f8b63f957679:~/tf# terraform show
    # alicloud_cloud_storage_gateway_gateway.default:
    resource "alicloud_cloud_storage_gateway" "default" {
        description              = null
        gateway_class            = "Basic"
        gateway_name             = "tf-terraform-test"
        id                       = "gw-0006tmifu2fe7xxx"
        location                 = "Cloud"
        payment_type             = "PayAsYouGo"
        public_network_bandwidth = 5
        reason_type              = "File"
        release_after_expiration = false
        status                   = "Running"
        storage_bundle_id        = "sb-0006tmifu2fexxx"
        type                     = "File"
        vswitch_id               = "vsw-bp1xw499jo5xxx"
    }

Block gateway

  1. Create a working directory and a file named main.tf inside it. Then, copy the following code into main.tf.

    variable "region"{
      # The region where the resource is deployed.
      default = "cn-hangzhou"
    }
    variable "access_key"{
      # Your AccessKey ID.
      default = "yourAccessKeyID"
    }
    variable "secret_key"{
      # Your AccessKey secret.
      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
      # The gateway specification. Modify this value as needed.
      gateway_class            = "Basic"
      # The gateway name. Modify this value as needed.
      gateway_name             = "tf-terraform-block"
      location                 = "Cloud"
      payment_type             = "PayAsYouGo"
      reason_type              = "Iscsi"
      # Replace the value with your gateway cluster ID.
      storage_bundle_id        = "sb-00095gtq2442k******"
      type                     = "Iscsi"
      # Replace the value with your VSwitch ID.
      vswitch_id               = "vsw-wz9sd6vfwnh*******"
      # The public network bandwidth. Modify this value as needed.
      public_network_bandwidth = 5
    }
  2. Run the following command to initialize the Terraform runtime environment.

    terraform init

    The following output indicates that the initialization is successful.

    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 indicates that the block gateway is created.

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

    Run the following command to view the details of the block gateway:

    terraform show
    [root@iZbp17rbg67nnq3im23tq1Z tf_gw]# terraform show
    # alicloud_cloud_storage_gateway_gateway.default:
    resource "alicloud_cloud_storage_gateway" "default" {
        description              = null
        gateway_class            = "Basic"
        gateway_name             = "tf-terraform-block"
        id                       = "gw-0009t9hkxxx"
        location                 = "Cloud"
        payment_type             = "PayAsYouGo"
        public_network_bandwidth = 5
        reason_type              = "File"
        release_after_expiration = false
        status                   = "Running"
        storage_bundle_id        = "sb-0009t9hkxzjxxx"
        type                     = "Iscsi"
        vswitch_id               = "vsw-bp1xw499jxxx"
    }
    [root@iZbp17rbg67nnq3im23tq1Z tf_gw]#

Create a cache

  1. Create a working directory and a file named main.tf inside it. Then, copy the following code into main.tf.

    variable "region"{
      # The region where the resource is deployed.
      default = "cn-hangzhou"
    }
    variable "access_key"{
      # Your AccessKey ID.
      default = "yourAccessKeyID"
    }
    variable "secret_key"{
      # Your AccessKey secret.
      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" {
      # Replace the value with your gateway ID.
      gateway_id               = "gw-0009t9hkx******"
      # The type of the cache disk. Modify this value as needed.
      cache_disk_category      = "cloud_essd"
      # The capacity of the cache disk. Modify this value as needed.
      cache_disk_size_in_gb    = "100"
      # The performance level. This parameter is required only if the disk type is ESSD.
      performance_level        = "PL1"
    }
  2. Run the following command to initialize the Terraform runtime environment.

    terraform init

    The following output indicates that the initialization is successful.

    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 indicates that the cache disk is created.

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

    Run the following command to view the details of the cache disk:

    terraform show
    [root@iZbp17rbg67nnq3im23tq1Z tf_cache]# terraform show
    # alicloud_cloud_storage_gateway_gateway_cache_disk.default:
    resource "alicloud_cloud_storage_gateway_gateway_cache_disk" "default" {
        cache_disk_category  = "cloud_essd"
        cache_disk_size_in_gb = 100
        cache_id             = "d-bp13nlst99xxx3"
        gateway_id           = "gw-0002xci9bxxxnj"
        id                   = "gw-0002xci9bxxxnj:d-bp13nlsxxx8:/dev/vdb"
        local_file_path      = "/dev/vdb"
        performance_level    = "PL1"
        status               = 0
    }

Create a share or volume

Share

  1. Create a working directory and a file named main.tf inside it. Then, copy the following code into main.tf.

    variable "region"{
      # The region where the resource is deployed.
      default = "cn-hangzhou"
    }
    variable "access_key"{
      # Your AccessKey ID.
      default = "yourAccessKeyID"
    }
    variable "secret_key"{
      # Your AccessKey secret.
      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" {
      # The name of the share. Modify this value as needed.
      gateway_file_share_name = "tf-share-terraform"
      # Replace the value with your gateway ID.
      gateway_id              = "gw-000e9g71y8o******"
      # Replace the value with the path of the created cache disk.
      local_path              = "/dev/vdb"
      # Replace the value with your OSS bucket name.
      oss_bucket_name         = "test_bucket"
      # Replace the value with your OSS endpoint.
      oss_endpoint            = "oss-cn-hangzhou-internal.aliyuncs.com"
      # The protocol of the share. Valid values: NFS and SMB.
      protocol                = "NFS"
    }
    Important

    To create a share with multi-bucket aggregation, use the following resource configuration:

    resource "alicloud_cloud_storage_gateway_gateway_file_share" "default" {
      # The name of the share. Modify this value as needed.
      gateway_file_share_name = "tf-share-terraform"
      # Replace the value with your gateway ID.
      gateway_id              = "gw-000e9g71y8o******"
      # Replace the value with the path of the created cache disk.
      local_path              = "/dev/vdb"
      # Replace the value with your OSS bucket names. Separate multiple bucket names with commas (,).
      oss_bucket_name         = "test_bucket1,test_bucket2"
      # Replace the value with your OSS endpoint.
      oss_endpoint            = "oss-cn-hangzhou-internal.aliyuncs.com"
      # The protocol of the share. Valid values: NFS and SMB.
      protocol                = "NFS"
    }
  2. Run the following command to initialize the Terraform runtime environment.

    terraform init

    The following output indicates that the initialization is successful.

    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 indicates that the share is created.

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

    Run the following command to view the details of the share:

    terraform show
    root@f8b63f957679:~/tf# terraform show
    # alicloud_cloud_storage_gateway_file_share.default:
    resource "alicloud_cloud_storage_gateway_file_share" "default" {
        access_based_enumeration = false
        backend_limit           = 0
        browsable               = false
        bypass_cache_read       = false
        cache_mode              = "Cache"
        direct_io               = false
        download_limit          = 0
        fast_reclaim            = false
        fe_limit                = 0
        gateway_file_share_name = "tf-share-terraform"
        gateway_id              = "gw-0006tmifu2fexxx"
        id                      = "gw-0006tmifu2fexxx:NFStf-share-terraform"
        ignore_delete           = false
        in_place                = false
        index_id                = "NFStf-share-terraform"
        lag_period              = 5
        local_path              = "/dev/vdb"
        nfs_v4_optimization     = false
        oss_bucket_name         = "sgw-0127"
        oss_bucket_ssl          = true
        oss_endpoint            = "oss-cn-hangzhou-internal.aliyuncs.com"
        partial_sync_paths      = null
        path_prefix             = null
        protocol                = "NFS"
        remote_sync             = false
        remote_sync_download    = false
        ro_client_list          = null
        ro_user_list            = null
        rw_client_list          = null
        rw_user_list            = null
        squash                  = "none"
        support_archive         = false
        transfer_acceleration   = false
        windows_acl             = false
    }

Volume

  1. Create a working directory and a file named main.tf inside it. Then, copy the following code into main.tf.

    variable "region"{
      # The region where the resource is deployed.
      default = "cn-hangzhou"
    }
    variable "access_key"{
      # Your AccessKey ID.
      default = "yourAccessKeyID"
    }
    variable "secret_key"{
      # Your AccessKey secret.
      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" {
      # The name of the volume. Modify this value as needed.
      gateway_block_volume_name = "tfvolume1"
      # Replace the value with your gateway ID.
      gateway_id              = "gw-000e9g71y8o******"
      # The working mode of the volume. Valid values: cache mode and write-through mode.
      cache_mode              = "Cache"
      # Replace the value with the path of the created cache disk.
      local_path              = "/dev/vdb"
      # Replace the value with your OSS bucket name.
      oss_bucket_name         = "test_bucket"
      # Replace the value with your OSS endpoint.
      oss_endpoint            = "oss-cn-hangzhou-internal.aliyuncs.com"
      # The protocol for block storage.
      protocol                = "iSCSI"
      # The storage allocation unit in bytes. Valid values: 8192, 16384, 32768, 65536, and 131072.
      chunk_size              = "8192"
      # The capacity of the volume.
      size                    = "100"
    }
  2. Run the following command to initialize the Terraform runtime environment.

    terraform init

    The following output indicates that the initialization is successful.

    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 indicates that the volume is created.

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

    Run the following command to view the details of the volume:

    terraform show
    [root@iZbp17rbg67nnq3im23tq1Z tf_volume]# terraform show
    # alicloud_cloud_storage_gateway_gateway_block_volume.default:
    resource "alicloud_cloud_storage_gateway_gateway_block_volume" "default" {
        cache_mode                = "Cache"
        chap_enabled              = false
        chap_in_user              = null
        chunk_size                = 8192
        gateway_block_volume_name = "tfvolume1"
        gateway_id                = "gw-0009t9hkxxxxxxc"
        id                        = "gw-0009t9hkxxxxxxc:tfvolume1-or20250303165520"
        index_id                  = "tfvolume1-or20250303165520"
        local_path                = "/dev/vdc"
        oss_bucket_name           = "test032202"
        oss_bucket_ssl            = true
        oss_endpoint              = "oss-cn-hangzhou-internal.aliyuncs.com"
        protocol                  = "iSCSI"
        size                      = 100
        status                    = "1"
    }

Clean up resources

When you no longer need the resources created with Terraform, run the following command to destroy them. For more information, see Terraform common commands.

terraform destroy

Related topics