> For the complete documentation index, see [llms.txt](https://docs.datafy.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.datafy.io/volume-lifecycle/creating-autoscaling-volumes.md).

# Creating Autoscaling Volumes

Create new EBS volumes and PVCs that autoscale from the moment they're created, with no activation and no initial data copy.

{% hint style="success" %}
Creating autoscaling volumes is supported from AutoScaler version 2.1.0. In Kubernetes, it requires Helm chart version 4.1.0.
{% endhint %}

Creating an autoscaling volume gives you a new, empty volume that autoscales from the moment it's created. This is different from [activating autoscaling](https://docs.datafy.io/volume-lifecycle/managing-autoscaling-volumes.md#activating-autoscaling), which applies to an existing volume and starts by copying its data to Datafy Volumes.

Creating the volume as autoscaling, rather than letting an [autoscaling rule](https://docs.datafy.io/volume-lifecycle/autoscaling-rules.md) activate it later, works best for:

* **Short-lived workloads** — a volume that exists for only minutes or hours can spend a large share of its life, or all of it, at full size, waiting for activation to complete.
* **Very large volumes** — the more data there is to copy, the longer activation takes.

There are a few ways to create an autoscaling volume. Choose the one that matches how the volume is used:

1. [PVCs in Kubernetes](#pvcs-in-kubernetes) — annotate a PVC, or set a StorageClass parameter.
2. [Volumes attached to EC2 instances](#volumes-attached-to-ec2-instances) — create and attach the volume with the [Datafy API](https://docs.datafy.io/resources/api.md#volume-lifecycle), or with [Terraform](#using-terraform) and the [`datafyaws` provider](https://docs.datafy.io/set-up-and-installation/iac-reconciliation.md#terraform).

{% hint style="info" %}
Volumes attached directly to Kubernetes nodes, rather than claimed by PVCs, are created the same way as [volumes attached to EC2 instances](#volumes-attached-to-ec2-instances).

The exception is a Bottlerocket local volume (Bottlerocket's data volume), which is created when the node launches. Activate autoscaling on it with an [autoscaling rule](https://docs.datafy.io/volume-lifecycle/autoscaling-rules.md) instead. See [Bottlerocket Local Volumes](https://docs.datafy.io/how-it-works/autoscaler-on-kubernetes.md#bottlerocket-local-volumes).
{% endhint %}

## Volume Creation Parameters

These parameters apply to every volume created as autoscaling, whatever the method:

* **Size** — the size you request is the volume's maximum size, and the size the filesystem sees. It must be at least your account's [Autoscaling Minimum Size](https://docs.datafy.io/set-up-and-installation/autoscaling-configurations.md#autoscaling-activation-configurations). The volume starts at about the Autoscaling Minimum Size (by default, 50 GiB or 10% of the requested size, whichever is larger), and [grows](https://docs.datafy.io/how-it-works/how-autoscaler-works.md#growing) up to the requested size as data is written.
* **Volume type** — gp3 only.
* **IOPS and throughput** — 3,000 IOPS and 125 MiB/s by default. You can set 3,000–16,000 IOPS and 125–1,000 MiB/s, with up to 1 MiB/s of throughput for every 4 IOPS.
* **Encryption** — volumes are encrypted by default, with your AWS account's default EBS encryption key. You can set a custom KMS key instead.
* **Tags** — tags you set when creating the volume are added to its Datafy Volumes in AWS, and appear on the volume in the Datafy app.
* **Volume ID** — Datafy returns an ID that identifies the autoscaling volume in the Datafy app and the Datafy API. In Kubernetes, the PersistentVolume bound to the PVC references this ID. It's a virtual ID that doesn't appear in the AWS console. The [Datafy Volumes](https://docs.datafy.io/how-it-works/how-autoscaler-works.md#datafy-volume-pairs) that hold the data carry the tag `datafy:source-volume:id` with this ID.

{% hint style="info" %}
This is similar to creating an autoscaling volume from a [Datafy snapshot](https://docs.datafy.io/volume-lifecycle/datafy-snapshots.md#restoring-a-volume-from-a-datafy-snapshot), except that the new volume is empty.
{% endhint %}

## PVCs in Kubernetes

{% hint style="warning" %}
AutoScaler must be [installed on the cluster](https://docs.datafy.io/set-up-and-installation/datafy-installation/installation.md#kubernetes-cluster) before you annotate a PVC or set the StorageClass parameter. Without it, an annotated PVC gets a standard EBS volume, and PVCs from a StorageClass with the parameter fail to provision.
{% endhint %}

Create a new autoscaling volume in one of two ways:

* [Annotate the PVC](#annotating-a-pvc), to create a single autoscaling volume.
* [Set a StorageClass parameter](#setting-a-storageclass-parameter), so every PVC that uses the class autoscales.

### Annotating a PVC

Add the `datafy.io/autoscaling-native: "true"` annotation to new PVCs:

```yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: data
  annotations:
    datafy.io/autoscaling-native: "true"
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: gp3
  resources:
    requests:
      storage: 200Gi
```

{% hint style="info" %}
For consistent sizing, set PVC sizes in binary units (`Gi`) rather than decimal units (`G`).
{% endhint %}

The annotation only takes effect before the volume is provisioned:

* The value must be `"true"` or `"false"`. Only `"true"` creates an autoscaling volume.
* You can add, change, or remove the annotation while the PVC is unbound. With a `WaitForFirstConsumer` StorageClass, that's until a pod using the PVC is scheduled.
* Once the PVC is bound to a PersistentVolume, the annotation can't be added. To autoscale a volume that already exists, [activate autoscaling](https://docs.datafy.io/volume-lifecycle/managing-autoscaling-volumes.md#activating-autoscaling) on it instead.
* Datafy removes the annotation from the PVC when the volume is first attached to a node, and it continues as an autoscaling volume.

### Setting a StorageClass Parameter

To make every new PVC of a StorageClass autoscale, add `datafy.io/autoscaling-native: "true"` to the StorageClass `parameters`:

```yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: gp3-autoscaling
provisioner: ebs.csi.aws.com
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true
parameters:
  datafy.io/autoscaling-native: "true"
  csi.storage.k8s.io/fstype: xfs
```

When the StorageClass sets the parameter, it applies to every PVC of that class, and the PVC annotation is ignored. Setting the parameter to `"false"` stops PVCs of that class from autoscaling at creation, even if they're annotated.

{% hint style="warning" %}
Remove the parameter from your StorageClasses before [uninstalling AutoScaler](https://docs.datafy.io/set-up-and-installation/uninstalling-datafy.md). Only the [Datafy proxy](https://docs.datafy.io/how-it-works/autoscaler-on-kubernetes.md#installing-datafy-on-kubernetes) recognizes it, so once AutoScaler is uninstalled, the EBS CSI driver rejects PVCs from a StorageClass that still sets it.
{% endhint %}

### StorageClass Parameters That Apply

Of the [EBS CSI driver's StorageClass parameters](https://github.com/kubernetes-sigs/aws-ebs-csi-driver/blob/master/docs/parameters.md), these apply to autoscaling PVCs:

* `iops` and `throughput` — within the limits in [Volume Creation Parameters](#volume-creation-parameters). `iopsPerGB` isn't applied, so set `iops` explicitly for more than the default 3,000 IOPS.
* `tagSpecification_<n>` — applied as described in [Volume Creation Parameters](#volume-creation-parameters).
* `csi.storage.k8s.io/fstype` — `ext4` or `xfs`. The CSI driver formats the volume on first mount, as for any PVC.

Autoscaling PVCs are always gp3, whatever `type` the StorageClass sets.

If Datafy rejects the request, for example because it's below the minimum size, the PVC stays `Pending` with a `ProvisioningFailed` event.

{% hint style="info" %}
A PVC restored from a [Datafy snapshot](https://docs.datafy.io/volume-lifecycle/datafy-snapshots.md#kubernetes-csi-integration) is always created as an autoscaling volume, with or without the annotation.
{% endhint %}

## Volumes Attached to EC2 Instances

Creating an autoscaling volume for an EC2 instance follows the same flow as creating an EBS volume through AWS. Only the create and attach steps are different: you run them with the Datafy API instead of the EC2 API, or with [Terraform](#using-terraform) using the [`datafyaws` provider](https://docs.datafy.io/set-up-and-installation/iac-reconciliation.md#terraform). Everything after the attach, such as formatting and mounting, is the same as for any new EBS volume.

| Step                     | AWS                                                                                            | Datafy API                                                                                                           | Terraform                                                                                                                                                              |
| ------------------------ | ---------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Create the volume        | [`CreateVolume`](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CreateVolume.html) | [`create-autoscaling-volume`](https://docs.datafy.io/resources/api.md#post-api-v1-volumes-create-autoscaling-volume) | [`aws_ebs_volume`](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ebs_volume) with `provider = datafyaws` and `datafy_autoscaling = true` |
| Attach it to an instance | [`AttachVolume`](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_AttachVolume.html) | [`attach`](https://docs.datafy.io/resources/api.md#post-api-v1-volumes-volumeid-attach)                              | [`aws_volume_attachment`](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/volume_attachment) with `provider = datafyaws`                   |

### Using the Datafy API

1. Create the volume with the [`create-autoscaling-volume`](https://docs.datafy.io/resources/api.md#post-api-v1-volumes-create-autoscaling-volume) endpoint. Pass the instance's Availability Zone and the size in GiB, plus any other [volume creation parameters](#volume-creation-parameters). The response includes the new volume ID.
2. Attach the volume with the [`attach`](https://docs.datafy.io/resources/api.md#post-api-v1-volumes-volumeid-attach) endpoint. Pass the instance ID and a device name that isn't in use on the instance, such as `/dev/sdf`. AutoScaler doesn't need to be installed on the instance yet.
3. Once AutoScaler is [installed](https://docs.datafy.io/set-up-and-installation/datafy-installation/installation.md#ec2-installation) on the instance, [format and mount the volume](https://docs.aws.amazon.com/ebs/latest/userguide/ebs-using-volumes.html) as you would any other EBS volume. Until then, the volume can't be used in any way, including formatting and mounting.

{% hint style="info" %}
On the instance, the volume appears as `/dev/dvol<ID>`, where `<ID>` is the volume ID without the `vol-` prefix. For example, `vol-0a1b2c3d4e5f67890` appears as `/dev/dvol0a1b2c3d4e5f67890`. You can format and mount it using either this path or the device name you passed when attaching it, such as `/dev/sdf`.

The device can take a few minutes to appear after the volume is attached.
{% endhint %}

{% hint style="warning" %}
In `/etc/fstab`, mount the volume by its filesystem UUID, not its device path. See [Autoscaling Volume Lifecycle](https://docs.datafy.io/volume-lifecycle/managing-autoscaling-volumes.md#autoscaling-volume-lifecycle).
{% endhint %}

### Using Terraform

{% hint style="info" %}
Before you start, set up the `datafyaws` provider. See [IaC Reconciliation](https://docs.datafy.io/set-up-and-installation/iac-reconciliation.md#getting-started).
{% endhint %}

Instead of the API calls in steps 1 and 2, set `datafy_autoscaling = true` on an `aws_ebs_volume` resource, and attach it with `aws_volume_attachment`, both using the `datafyaws` provider:

```hcl
resource "aws_ebs_volume" "data" {
  provider = datafyaws

  availability_zone  = aws_instance.app.availability_zone
  size               = 200
  type               = "gp3"
  datafy_autoscaling = true

  tags = {
    Name = "app-data"
  }
}

resource "aws_volume_attachment" "data" {
  provider = datafyaws

  device_name = "/dev/sdf"
  volume_id   = aws_ebs_volume.data.id
  instance_id = aws_instance.app.id
}
```

* Set `type = "gp3"`. The plan fails if `type` is unset or set to any other type.
* `size`, `iops`, `throughput`, `encrypted`, `kms_key_id`, and `tags` apply as described in [Volume Creation Parameters](#volume-creation-parameters). `size` is the volume's maximum size.
* `datafy_autoscaling` can't be added to or changed on an existing volume. To autoscale a volume that already exists, [activate autoscaling](https://docs.datafy.io/volume-lifecycle/managing-autoscaling-volumes.md#activating-autoscaling) on it instead.
* To remove `datafy_autoscaling` from the configuration, [deactivate autoscaling](https://docs.datafy.io/volume-lifecycle/managing-autoscaling-volumes.md#deactivating-autoscaling) on the volume first, then delete the argument. Setting it to `false` isn't allowed.
* To change `size`, `iops`, or `throughput` later, see [Modifying Autoscaling Volumes](https://docs.datafy.io/volume-lifecycle/modify-a-volumes-parameters.md#using-terraform).

After `terraform apply`, format and mount the volume as in [step 3 of Using the Datafy API](#using-the-datafy-api).

### Automating Volume Creation at Launch

You can use the Datafy API calls as part of your automated instance provisioning, for example in the instance's [user data](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html).

<details>

<summary>Example: Creating a Volume From User Data</summary>

This example runs the whole flow from an instance's user data, so the instance boots with a new autoscaling volume. It installs AutoScaler, then creates, attaches, formats, and mounts a 200 GiB `xfs` volume at `/data`. Adapt it to your environment:

* It's written for Amazon Linux 2023.
* Replace `<VERSION>` with the AutoScaler version to install, and `<YOUR_TOKEN>` with your [API token](https://docs.datafy.io/set-up-and-installation/datafy-installation/token-generation.md).

{% code title="user-data.sh" overflow="wrap" %}

```bash
#!/bin/bash
set -euo pipefail

VERSION="<VERSION>"
TOKEN="<YOUR_TOKEN>"
SIZE_GIB=200
MOUNT_POINT=/data
FS_TYPE=xfs
API=https://api.datafy.io/api/v1

# Install Docker (required for AutoScaler) and jq (parses the API responses)
yum update -y
yum install -y docker jq
service docker start

# Install AutoScaler
curl -sSfL https://agent.datafy.io/install?version=$VERSION | AGENT_MODE="autoscaler" TOKEN=$TOKEN sh

# Instance ID and Availability Zone, from the instance metadata service
IMDS_TOKEN=$(curl -sSf -X PUT http://169.254.169.254/latest/api/token \
  -H "X-aws-ec2-metadata-token-ttl-seconds: 300")
INSTANCE_ID=$(curl -sSf -H "X-aws-ec2-metadata-token: $IMDS_TOKEN" \
  http://169.254.169.254/latest/meta-data/instance-id)
AZ=$(curl -sSf -H "X-aws-ec2-metadata-token: $IMDS_TOKEN" \
  http://169.254.169.254/latest/meta-data/placement/availability-zone)

# Create the autoscaling volume and attach it to this instance
VOLUME_ID=$(curl -sS --fail-with-body -X POST "$API/volumes/create-autoscaling-volume" \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d "{\"volumeProperties\": {\"availabilityZone\": \"$AZ\", \"diskSize\": $SIZE_GIB}}" \
  | jq -r .volumeId)

curl -sS --fail-with-body -X POST "$API/volumes/$VOLUME_ID/attach" \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d "{\"instanceId\": \"$INSTANCE_ID\", \"deviceName\": \"/dev/sdf\", \"deleteOnTermination\": true}"

# The device can take a few minutes to appear; formatting before udev settles fails
DEVICE="/dev/dvol${VOLUME_ID#vol-}"
for _ in $(seq 60); do [ -b "$DEVICE" ] && break; sleep 5; done
[ -b "$DEVICE" ] || { echo "$DEVICE did not appear" >&2; exit 1; }
udevadm settle --timeout=180

# Format and mount the volume by UUID
mkfs -t "$FS_TYPE" "$DEVICE"
UUID=$(blkid -s UUID -o value "$DEVICE")
mkdir -p "$MOUNT_POINT"
echo "UUID=$UUID $MOUNT_POINT $FS_TYPE defaults,nofail,x-systemd.device-timeout=5min 0 2" >> /etc/fstab
mount "$MOUNT_POINT"
```

{% endcode %}

{% hint style="info" %}
By default, EC2 runs user data scripts only on an instance's first boot, so the script creates one volume per instance. On later boots, the `/etc/fstab` entry mounts the same volume.
{% endhint %}

{% hint style="warning" %}
Anyone who can read the instance's attributes in AWS can read its user data, including the token. In production, read the token from a secret store, such as [AWS Secrets Manager](https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html), instead of writing it into the script.
{% endhint %}

</details>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.datafy.io/volume-lifecycle/creating-autoscaling-volumes.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
