Hello,

I have a function in my workflow that does some video encoding and needs a CPU optimized droplet to run efficiently.

The thing is I only need it to run a few times a day. So I wanted a way to initiate a task that creates a droplet (possibly a docker container), has it run a task and then destroy it.

I know I can create a cheap droplet and code it with the DigitalOcean API to accept tasks, create a droplet, run the task and then destroy it but I was wondering if there was already something built out that I can use to not reinvent the wheel.

Any help or hints in the right direction would be helpful. Thank you!

1 comment
  • To add to your question, I have a similar situation, but I was wondering if what you are describing is actually permitted on DigitalOcean?

    Droplets aren’t (as far as I know) meant to be short-lived. That being said there is a whole section in the DO marketplace called functions, which as far as I understand is meant for this exact thing - running short-lived tasks. But, I’m no expert on the matter.

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.

×
1 answer

Hi there @mindglitch,

I don’t think that there is built-in functionality to do that via the DigitalOcean control panel directly. Though I think that this is a great idea, sounds like it’d be super useful!

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/

On another note, I have a small bash script that I use for similar tasks.

NOTE: Please, carefully review the whole script before using it.

The prerequisites are:

What the script does is:

  • Creates a new Droplet with the details that you’ve specified
  • Waits for the Droplet to become active
  • Runs a key scan
  • Executes a set of commands on the new Droplet over SSH
  • Deletes the Droplet
#!/bin/bash

##
# Variables
# Add the details of the Droplet that you would like to create
## 

droplet_name='test-droplet'
droplet_size='s-1vcpu-1gb'
droplet_image='ubuntu-18-04-x64'
droplet_region='fra1'
ssh_key_id='your_ssh_key_id'

##
# Droplet info session file
##
temp_droplet=$(mktemp /tmp/temp-droplet.XXXXXX)

##
# Create Droplet
##
function new_droplet() {
    echo "Creating Droplet with the following details:"
    echo "Droplet name: ${droplet_name}"
    echo "Droplet size: ${droplet_size}"
    echo "Droplet image: ${droplet_image}"
    echo "Droplet regon: ${droplet_region}"

    doctl compute droplet create ${droplet_name} --size ${droplet_size} --image ${droplet_image} --region ${droplet_region} --ssh-keys ${ssh_key_id} > ${temp_droplet}
    echo "Waiting 60 seconds for Droplet to be created..."
    sleep 1m

    new_droplet_id=$(cat ${temp_droplet} | tail -1 | awk '{ print $1 }')
    new_droplet_ip=$(doctl compute droplet get ${new_droplet_id} --template "{{ .PublicIPv4}}")
    echo "Droplet IP: ${new_droplet_ip}"
}
new_droplet

##
# Check if Droplet is running
##

function ssh_key_scan() {
    while [ "$status" != "active" ] ; do
        echo "Waiting for status to become active.."
        sleep 5
        status=$(doctl compute droplet get ${new_droplet_id} --template "{{ .Status}}")
    done
    echo "Droplet status is: ${status}. Proceeding to SSH Key Scan"
    ssh-keyscan ${new_droplet_ip} >> ~/.ssh/known_hosts
}
ssh_key_scan

##
# Execute a command
##

function run_commands() {
    echo "Running commands.."
    ssh root@${new_droplet_ip} 'cat /etc/os-release'
}
run_commands

##
# Delete Droplet and session file
##
function clean_up(){
    echo "Deleting Droplet and temp files.."
    doctl compute droplet delete -f ${new_droplet_id}
    rm ${temp_droplet}
}
clean_up

In the run_commands function you can change the command/commands which you would like to execute.

Hope that this helps!
Regards,
Bobby

by Marko Mudrinić
DigitalOcean's web based control panel provides a convenient, point-and-click interface for managing Droplets. There are many times, however, when a command-line tool may be a preferable alternative. doctl, the official DigitalOcean command-line client, leverages the DigitalOcean API to provide access to most account and Droplet features.
Submit an Answer