Question

How to use the API to create a droplet with attached block storage?

I’m trying to use the API to create a droplet that has attached block storage.

In the web interface you can create a volume directly in the “new droplet” interface. However this doesn’t appear to be the case with the API. That’s fine. I first create the block volume with the Block Storage API. Then I create a droplet.

When I create the droplet however, the VM is automatically powered on. This means that I have to poll the action to see when the VM has been created, then force it to power down, attach the block storage, then power it back up. This seems like a kind of ugly way to get the storage attached. Plus, I’m using cloud-init to automate running a task in the VM, and if the VM is already powered on and I power it off, it could mess up the cloud-init flow (e.g. on the second boot a script may not run).

The API docs do not indicate any parameters that can be used to include a block volume in the create endpoint, nor do I see any parameters to create the droplet without powering it on.

What’s the best practice for doing this via the API?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Jeff Braunstein
DigitalOcean Employee
DigitalOcean Employee badge
November 16, 2022

You can specify Volume ID(s) when you create a Droplet. I am sorry, I have discovered that the documentation is missing that field, I will get that fixed. Also, the power state of the Droplet should not matter when attaching/detaching a Volume. If you run into a problem with this, please contact DO Support with the Droplet and Volume name. https://pkg.go.dev/github.com/digitalocean/godo#DropletCreateRequest https://pkg.go.dev/github.com/digitalocean/godo#DropletCreateVolume

Bobby Iliev
Site Moderator
Site Moderator badge
November 6, 2022

Hi there,

I believe that pre-formatted volumes created via the API will be automatically mounted to the Droplet. So the key thing is to specify the initial_filesystem_type when creating the volume.

For example if you are using Terraform, this would look as follows:

resource "digitalocean_droplet" "foo" {
  name       = "foo"
  size       = "s-1vcpu-1gb"
  image      = "ubuntu-22-04-x64"
  region     = "nyc1"
}

resource "digitalocean_volume" "bar" {
  region                  = "nyc1"
  name                    = "bar"
  size                    = 100
  initial_filesystem_type = "ext4"
  description             = "an example volume"
}

resource "digitalocean_volume_attachment" "foobar" {
  droplet_id = "${digitalocean_droplet.foo.id}"
  volume_id  = "${digitalocean_volume.bar.id}"
}

The resulting volume will be mounted and available at /mnt/bar/ on the Droplet.

If you do not specify the initial_filesystem_type the volume will appear as a raw block device that must be manually formatted and mounted.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up