Report this

What is the reason for this report?

How Can I Automate Droplet Snapshots with Terraform on DigitalOcean?

Posted on October 28, 2024

I’m using Terraform to manage my DigitalOcean infrastructure and want to automate the creation of snapshots for my Droplets.

Ideally, I’d like to schedule snapshots on a regular basis as part of my Terraform configuration so I don’t have to remember to take backups manually.

I already have the automated backups enabled, but I just want to be sure that I have a snapshot before applying some configuration changes to be safe.

Is there a way to do this directly in Terraform with DigitalOcean, or would I need additional tooling?



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!

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.

Hey there! 👋

Currently, DigitalOcean’s Terraform provider doesn’t natively support scheduling Droplet snapshots directly, but you can still set up snapshots using a few creative steps in combination with other tools!

Here are a few ways you can automate Droplet snapshots with Terraform:

  1. While Terraform doesn’t schedule snapshots, you can create snapshots manually before applying any major configuration changes by using do_droplet_snapshot:

    https://registry.terraform.io/providers/digitalocean/digitalocean/latest/docs/resources/droplet_snapshot

    Here’s an example of how you could add this to your configuration:

    resource "digitalocean_droplet" "my_droplet" {
      name   = "example-droplet"
      size   = "s-1vcpu-1gb"
      image  = "ubuntu-20-04-x64"
      region = "nyc3"
    }
    
    resource "digitalocean_droplet_snapshot" "my_snapshot" {
      droplet_id = digitalocean_droplet.my_droplet.id
      name       = "pre-update-snapshot-${timestamp()}"
    }
    

    You’d trigger this manually by running terraform apply right before making any critical changes.

  2. If you’re looking for more automation, you can create a shell script to trigger snapshots via the DigitalOcean API. Here’s a quick example of a cURL command for creating a snapshot:

    curl -X POST -H "Authorization: Bearer $DO_API_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{"type":"snapshot","name":"auto-snapshot-$(date +%Y-%m-%d)"}' \
    "https://api.digitalocean.com/v2/droplets/DROPLET_ID/actions"
    

    You can run this script periodically using a cron job. Just replace DROPLET_ID with your droplet’s ID and set it up in your server’s cron scheduler.

  3. DigitalOcean also offers SnapShooter integration for scheduled snapshots. SnapShooter automates backups and snapshots, and it’s a good choice if you want full automation without extra scripts or manual commands. Plus, SnapShooter keeps backups separate from Droplet automated backups for extra safety.

On the Terraform side again, since snapshots are best used before applying changes, run terraform plan to preview any adjustments. This way, you can be sure everything looks good and has a fresh snapshot in case of any rollback.

Hope this helps!

- Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.