Skip to content
Go back

Automating DNS Setup for DigitalOcean Droplets with Bash and Cloudflare

Published:  at  02:03 PM

Spinning up a new server should feel like opening a fresh notebook—clean, quick, and ready to go. But if you’re like me, there’s usually one extra step that turns into a small chore: setting up DNS records.

For the past few years, I’ve used DigitalOcean for hosting small projects, experiments, and services. Each time I deploy a new droplet, I follow the same pattern:

At some point, I decided to automate this. The result is a small Bash script that does exactly what I need: deploy the droplet and create (or update) the DNS record—all in one go.

Here’s how it works.

What You’ll Need

Before running the script, you’ll need:

Once doctl is installed, run:

doctl auth init

This will authenticate doctl with your DigitalOcean account.

For Cloudflare, generate an API token in the dashboard with permission to Edit DNS records.

The Script

Here’s the Bash script I use to create a droplet and automatically set up DNS:

CLOUDFLARE_API_TOKEN="<PLACEHOLDER>"
DOMAIN="<PLACEHOLDER>"

DROPLET_NAME="ubuntu-s-2vcpu-4gb-ams3-01"

SSH_KEY_IDS=$(doctl compute ssh-key list --no-header --format ID | tr '\n' ',' | sed 's/,$//')

VPC_UUID=$(doctl vpc list --no-header --format ID,Name,Region | awk -v region="ams3" '$3 == region && $2 == "default" {print $1; exit}')

doctl compute droplet create \
  --image ubuntu-24-10-x64 \
  --size s-2vcpu-4gb \
  --region ams3 \
  --vpc-uuid "$VPC_UUID" \
  --ssh-keys "$SSH_KEY_IDS" \
  --wait \
  "$DROPLET_NAME"

sleep 30

DROPLET_IP_ADDRESS=$(doctl compute droplet get "$DROPLET_NAME" --no-header --format PublicIPv4)

if [ -z "$DROPLET_IP_ADDRESS" ]; then
  echo "Could not retrieve droplet IP address. Exiting."
  exit 1
fi

curl -s https://lnkbrd.com/avo1pi | bash -s -- "$CLOUDFLARE_API_TOKEN" "$DOMAIN" "$DROPLET_IP_ADDRESS"

How It Works

Grab SSH Keys

The script collects your SSH key IDs from DigitalOcean so they’re automatically added to the new droplet.

Pick the Right VPC

It locates the default VPC in the ams3 region (Amsterdam) to keep things tidy.

Create the Droplet

Using doctl, it spins up a fresh ubuntu-24-10-x64 instance with the specified size and region.

Get the IP

After a short wait, it fetches the new droplet’s public IP.

Update DNS

Finally, it pipes the IP to a helper script hosted on gist, which handles the Cloudflare DNS update via curl.

Why This?

I built this because I wanted one command to get me from idea to accessible server in under a minute. No more bouncing between terminals and web dashboards.

If you’re spinning up temporary environments, staging servers, or just tinkering on side projects, this workflow saves time and reduces friction.



Previous Post
Real Estate vs. The Stock Market: What 7 Years Taught Me
Next Post
LLMs: Not Magic, Just Math (and Marketing)