---
title: "Create several file storages via Terraform"
sidebar_label: "Create several file storages"
description: "How to create several file storages"
sidebar_position: 2
---

import Formbricks from '@theme/MDXComponents/Formbricks'
import ConfigureProviders from '@site/i18n/en/docusaurus-plugin-content-docs-terraform/current/_partials/common/configure-providers.mdx'
import ConfigureProvidersConfig from '@site/i18n/en/docusaurus-plugin-content-docs-terraform/current/_partials/common/configure-providers-config.mdx'
import CreateNetworks from '@site/i18n/en/docusaurus-plugin-content-docs-terraform/current/_partials/cloud-networks/create-networks.mdx'
import CreateResourcesAlert from '@site/i18n/en/docusaurus-plugin-content-docs-terraform/current/_partials/common/create-resources-alert.mdx'

# Create several file storages via Terraform

<CreateResourcesAlert />

<br />

1. Optional: [configure providers](#configure-providers).
2. [Create a private network and subnet](#create-networks).
3. [Set variables for file storages](#add-storage-variables).
4. [Configure storage networks](#configure-storage-networks).
5. [Create file storages](#create-storages).
6. [Configure access rules for file storages](#configure-access).

## Configuration files \{#config}

<details>
  <summary>Example configuration file for providers</summary>

  <ConfigureProvidersConfig />
</details>

<details>
  <summary>Example configuration file for creating three file storages</summary>

  ```hcl
  resource "openstack_networking_network_v2" "network_1" {
    name           = "private-network"
    admin_state_up = "true"
  }

  resource "openstack_networking_subnet_v2" "subnet_1" {
    name       = "private-subnet"
    network_id = openstack_networking_network_v2.network_1.id
    cidr       = "192.168.199.0/24"
  }

  variable "storages" {
    default = [
      { name = "nfs-storage-1", size = 50, type="basic.ru-3a", proto="NFS", ip = "192.168.199.10" },
      { name = "nfs-storage-2", size = 100, type="fast.ru-3a", proto="NFS", ip = "192.168.199.11" },
      { name = "nfs-storage-3", size = 50, type="universal.ru-3a", proto="CIFS", ip = "192.168.199.12" },
    ]
  }

  resource "openstack_sharedfilesystem_sharenetwork_v2" "share_network" {
    count             = length(var.storages)
    name              = "share-network-${count.index}"
    neutron_net_id    = openstack_networking_network_v2.network_1.id
    neutron_subnet_id = openstack_networking_subnet_v2.subnet_1.id
  }

  resource "openstack_sharedfilesystem_share_v2" "share" {
    count            = length(var.storages)
    name             = var.storages[count.index].name
    share_type       = var.storages[count.index].type
    share_proto      = var.storages[count.index].proto
    size             = var.storages[count.index].size
    share_network_id = openstack_sharedfilesystem_sharenetwork_v2.sharenetwork.id
    metadata         = { ip = var.storages[count.index].ip }
  }

  resource "openstack_sharedfilesystem_share_access_v2" "share_access" {
    count        = length(var.storages)
    access_level = "rw"
    access_to    = "0.0.0.0/0"
    access_type  = "ip"
    share_id     = openstack_sharedfilesystem_share_v2.share[count.index].id
  }
  ```
</details>

## 1. Optional: configure providers \{#configure-providers}

If you have [configured the Selectel and OpenStack providers](/terraform/configure-terraform-environment/#configure-providers), skip this step.

<ConfigureProviders />

## 2. Create a private network and subnet \{#create-networks}

<CreateNetworks />

## 3. Set variables for file storages \{#add-storage-variables}

Define a variable with parameters for each file storage you need to create.

```hcl
variable "storages" {
  default = [
    { name = "nfs-storage-1", size = 50, type="basic.ru-3a", proto="NFS", ip = "192.168.199.10" },
    { name = "nfs-storage-2", size = 100, type="fast.ru-3a", proto="NFS", ip = "192.168.199.11" },
    { name = "nfs-storage-3", size = 50, type="universal.ru-3a", proto="CIFS", ip = "192.168.199.12" },
  ]
}
```

Where:

* `name` — file storage name;
* `size` — file storage size in GB, e.g., `50`. Limitations: from 50 GB to 50 TB;
* `type` — [file storage type](/file-storage/about/about-file-storage/#file-storage-types) and [pool segment](/infrastructure/locations/#pool) in the format `<type>.<pool_segment>`, e.g., `basic.ru-9a`:
  * `<type> `
    * `basic` — HDD Basic type;
    * `universal` — SSD Universal type;
    * `fast` — SSD Fast type;
  * `<pool_segment>` — pool segment where the file storage will be created, for example `ru-9a`. The list of available pool segments can be found in the instructions [Product availability by location](/infrastructure/product-availability-by-location/);
* `proto` — file storage protocol:
  * `NFS` — for the NFSv4 protocol;
  * `CIFS` — for the CIFS SMBv3 protocol;
* optional: `ip` — private IP address of the file storage, e.g., `192.168.199.10`. You cannot change the IP address after the storage is created.

## 4. Configure storage networks \{#configure-storage-networks}

A separate [openstack\_sharedfilesystem\_sharenetwork\_v2](/terraform/openstack-provider-reference/shared-filesystem-manila/resources/openstack_sharedfilesystem_sharenetwork_v2/) resource will be automatically created for each file storage, even if they use the same private network and subnet.

```hcl
resource "openstack_sharedfilesystem_sharenetwork_v2" "share_network" {
  count             = length(var.storages)
  name              = "share-network-${count.index}"
  neutron_net_id    = openstack_networking_network_v2.network_1.id
  neutron_subnet_id = openstack_networking_subnet_v2.subnet_1.id
}
```

## 5. Create file storages \{#create-storages}

```hcl
resource "openstack_sharedfilesystem_share_v2" "share" {
  count            = length(var.storages)
  name             = var.storages[count.index].name
  share_type       = var.storages[count.index].type
  share_proto      = var.storages[count.index].proto
  size             = var.storages[count.index].size
  share_network_id = openstack_sharedfilesystem_sharenetwork_v2.share_network[count.index].id
  metadata         = { ip = var.storages[count.index].ip }
}
```

Where:

* `count` — number of file storages to create. The value will be based on the number of file storages you [specified in the variable earlier](#add-storage-variables);
* `share_type` — [file storage type](/file-storage/about/about-file-storage/#file-storage-types) and [pool segment](/infrastructure/locations/#pool) in the format `<type>.<pool_segment>`, e.g., `basic.ru-9a`. The value for each file storage is taken from the variable you [specified earlier](#add-storage-variables);
* `share_proto` — file storage protocol. The value for each file storage is taken from the variable you [defined earlier](#add-storage-variables):
  * `NFS` — for the NFSv4 protocol;
  * `CIFS` — for the CIFS SMBv3 protocol;
* `size` — file storage size in GB, for example, `50`. Limitations: from 50 GB to 50 TB. The value for each file storage is taken from the variable you [defined earlier](#add-storage-variables);
* optional: `metadata = {"ip": "<ip_address>"}` — private IP address of the file storage, e.g., `192.168.199.10`. You cannot change the IP address after the storage is created. The value for each file storage is taken from the variable you [specified earlier](#add-storage-variables).

See the detailed resource description [openstack\_sharedfilesystem\_share\_v2](/terraform/openstack-provider-reference/shared-filesystem-manila/resources/openstack_sharedfilesystem_share_v2/).

## 6. Configure access rules for file storages \{#configure-access}

```hcl
resource "openstack_sharedfilesystem_share_access_v2" "share_access" {
  count        = length(var.storages)
  access_level = "rw"
  access_to    = "0.0.0.0/0"
  access_type  = "ip"
  share_id     = openstack_sharedfilesystem_share_v2.share[count.index].id
}
```

Where:

* `count` — number of file storages. The value will be based on the number of file storages you [defined in the variable earlier](#add-storage-variables);
* `access_level` — [access level](/file-storage/manage/configure-access-by-ips/#access-levels) to the storage, which depends on the protocol:
  * for CIFS SMBv3 — `rw` (read and write);
  * for NFSv4 — `ro` (read-only) or `rw` (read and write);
* `access_to` — IP address or CIDR of the private subnet for which access will be granted. For example:
  * `192.168.199.10` — access for an IP address;
  * `192.168.199.0/24` — access for the private subnet range;
  * `0.0.0.0/0` — access for all addresses of the private subnet where the file storages are located. To configure access to file storages for any network client, you can add the [openstack\_sharedfilesystem\_share\_v2](#create-storages) resource line `metadata = {"access_list_allow_all": "true"}`.

See the detailed resource description [openstack\_sharedfilesystem\_share\_access\_v2](/terraform/openstack-provider-reference/shared-filesystem-manila/resources/openstack_sharedfilesystem_share_access_v2/).

<Formbricks />
