Question

How to make create droplet idemoptent?

Is there a built-in method to make droplet creation idempotent? For example, if you run this command to create a droplet twice…

curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
  -d '{"name":"example.com","region":"nyc3","size":"s-1vcpu-1gb","image":"ubuntu-20-04-x64","ssh_keys":[289794,"3b:16:e4:bf:8b:00:8b:b8:59:8c:a9:d3:f0:19:fa:45"],"backups":true,"ipv6":true,"monitoring":true,"tags":["env:prod","web"],"user_data":"#cloud-config\nruncmd:\n  - touch /test.txt\n","vpc_uuid":"760e09ef-dc84-11e8-981e-3cfdfeaae000"}' \
  "https://api.digitalocean.com/v2/droplets"

…two droplets will be created. Is there an option to run the command twice, and prevent a second droplet from being created?

If the answer is yes, how does one accomplish this? If the answer is no, is there a recommended approach to implement droplet uniqueness?


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.

Bobby Iliev
Site Moderator
Site Moderator badge
September 4, 2023
Accepted Answer

Hey @amorphid,

I believe that this is not available as of the time being via the DigitalOcean API.

The best thing to do to get your voice heard regarding this would be to head over to our Product Ideas board and post a new idea, including as much information as possible for what you’d like to see implemented.

https://ideas.digitalocean.com/

But in the meantime, what you could do is to use the ‘List All Droplets’ API endpoint and check if a Droplet with the specific name already exists.

Here is a quick script as an example:

#!/bin/bash

# Your Droplet name:
DROPLET_NAME="example.com"

# Fetch the list of Droplets
DROPLETS=$(curl -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" "https://api.digitalocean.com/v2/droplets?per_page=200")

# Check if the Droplet with the desired name already exists
DROPLET_EXISTS=$(echo $DROPLETS | jq ".droplets[] | select(.name == \"$DROPLET_NAME\")")

# Only create the droplet if it doesn't exist
if [ -z "$DROPLET_EXISTS" ]; then
    # Your curl command to create the droplet
else
    echo "Droplet with name $DROPLET_NAME already exists."
fi

Alternatively, you could use the doctl CLI and build a similar script as well.

Hope that helps!

- Bobby.

Try DigitalOcean for free

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

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel