Volume Attachment¶
HtzVolumeAttachment attaches a Hetzner Cloud Volume to a Server. It models the attachment relationship as a first-class resource with its own lifecycle — create attaches, update re-attaches to a different server, and destroy detaches.
Type: Hetzner::Storage::VolumeAttachment
Import: @cdk-x/hetzner
Props¶
| Prop | Type | Required | Description |
|---|---|---|---|
volumeId |
number \| IResolvable |
✅ | ID of the volume to attach. Typically supplied via attrVolumeId from an HtzVolume. |
serverId |
number \| IResolvable |
✅ | ID of the server to attach the volume to. Typically supplied via attrServerId from an HtzServer. |
automount |
boolean |
— | If true, Hetzner automatically mounts the volume inside the server after attaching. Default: false. |
No attribute getters
HtzVolumeAttachment has no attr* getters — its primary identifier is the volumeId, which you already hold from HtzVolume.attrVolumeId.
Create example¶
attrVolumeIdproduces a{ ref, attr }token — the engine resolves it afterHtzVolumeis created and injects the numeric ID. The attachment automatically depends on the volume.- Same for
attrServerId— the attachment waits for the server to be ready before attaching.
Dependency graph¶
The engine infers dependencies from the { ref, attr } tokens — no manual addDependency() call is needed:
Update behavior¶
Changing serverId triggers a detach-then-reattach sequence:
- Engine calls
POST /volumes/{id}/actions/detach— waits for the action to complete. - Engine calls
POST /volumes/{id}/actions/attachwith the newserverId— waits for the action to complete.
Changing automount alone also triggers this re-attach cycle.
Downtime during re-attach
The volume is unmounted from the old server before it is mounted on the new one. Any running process that depends on the volume will encounter I/O errors during this window.
Destroy behavior¶
The engine calls POST /volumes/{id}/actions/detach and waits for the Hetzner action to reach success before marking the resource as destroyed. The underlying volume is not deleted — only the attachment is removed.
Destroy order
Declare HtzVolumeAttachment before destroying HtzVolume or HtzServer. The engine's dependency graph handles this automatically when you use attrVolumeId and attrServerId tokens.
Location constraint¶
Hetzner requires the volume and the server to be in the same location. Attempting to attach across locations results in a 422 invalid_input error from the API.
// Both must share the same location
const volume = new HtzVolume(stack, 'Vol', {
name: 'data',
size: 20,
location: 'fsn1', // (1)!
});
const server = new HtzServer(stack, 'Srv', {
name: 'worker',
serverType: ServerType.CX22,
image: 'ubuntu-24.04',
location: Location.FSN1, // (2)!
});
- Volume is in
fsn1. - Server must also be in
fsn1.
See also
- Volume — the block storage resource being attached
- Server — the compute resource receiving the volume
- Tokens & Cross-resource References — how
attrVolumeIdandattrServerIdresolve at deploy time