Creating a Digitalocean droplet via Python
After taking the week off for Canada Day in which I spent time camping with my family I get back to it. This time I’m working on step one of the master plan. As a reminder ChatGPT said my first task should be.
1. Initial Setup:
Set up a Digital Ocean instance.
Install Docker on the instance.
Create a Docker container for running your application.
So let’s start there. Signing up for Digitalocean is easy, but they
require a nominal fee to act as verification. Once that’s done I can
get to work. I want to create an instance so I ask I would like to
focus on Master Plan Step 1. I have a Digital Ocean account and would
like to setup an instance. Which instance type would you recommend for
a small tests?
The response is quite verbose.
For small tests and initial setup, you can start with a smaller
instance type on Digital Ocean. The exact instance type you choose
depends on the resource requirements of your application and the
expected workload. However, for a basic setup and testing purposes,
the following instance type can be a good starting point:
**Droplet Type: Standard**
- Droplet Size: Basic
- CPU: 1 vCPU
- RAM: 1 GB
- Storage: 25 GB SSD
- Bandwidth: 1 TB
This instance type provides a balanced configuration with a single
virtual CPU, 1 GB of RAM, and 25 GB of SSD storage. It should be
sufficient for running a basic application and performing initial
tests. The 1 TB bandwidth should also be adequate for low to moderate
traffic during the testing phase.
As your application and workload grow, you can consider scaling up to
higher performance instances to meet the demands. Digital Ocean offers
a variety of droplet sizes with different CPU, RAM, storage, and
bandwidth specifications, allowing you to choose the most suitable
option based on your requirements.
Remember to consider the specific resource needs of your application,
such as database requirements and expected concurrent users, when
selecting an instance type.
I look through the various options. I’m using the Toronto region (TOR1). What’s interesting is that the $4 option is not available. Only the $6 one is available. I’m not interested in looking for a region that supports $4 instances right now so I’ll go with this.
It’s always possible to create instances manually from the web
interface, but I’d rather build an automated way to do this. So I ask
ChatGPT, Does Digital Ocean have an API to create droplets?
To which
I get a reply that starts with.
Yes, Digital Ocean provides an API that allows you to programmatically
create and manage droplets (instances) along with other resources. The
Digital Ocean API enables you to automate the process of creating,
configuring, and managing your infrastructure.
To create droplets using the Digital Ocean API, you can make use of
the "Droplets" endpoint. Here's an overview of the steps involved:
This continues for a while, but it confirms what I suspected, there
are automatic ways of tell Digital Ocean to do things. But does it
understand my language of choice? Are there Python 3 libraries for
this API?
Yes, there are Python libraries available that can help you interact
with the Digital Ocean API. Here are a few popular libraries you can
use to work with the Digital Ocean API in Python:
1. **digitalocean**: This is the official Python library provided by
Digital Ocean. It offers a convenient way to interact with various
Digital Ocean resources, including droplets, images, volumes, and
more. You can find the library on GitHub and refer to its
documentation for usage examples and detailed information.
2. **python-digitalocean**: This is another popular Python library for
interacting with the Digital Ocean API. It provides a simple and
intuitive interface to create, manage, and monitor Digital Ocean
resources. The library offers features such as droplet creation,
deletion, and resizing, domain management, and more. You can find
the library on GitHub and refer to its documentation for usage
instructions.
3. **dopy**: Dopy is a lightweight Python library for the Digital
Ocean API. It offers methods to interact with droplets, images, SSH
keys, and other resources. The library supports various operations
like creating droplets, listing droplets, and managing SSH
keys. You can find the library on GitHub and refer to its
documentation for examples and usage details.
These libraries provide abstraction layers over the Digital Ocean API,
making it easier to interact with the API endpoints and handle the
request/response handling. You can choose the library that best fits
your needs and development preferences.
Remember to install the chosen library using a package manager like
pip before you can start using it in your Python project.
If that’s the case then I just need a script to help me. Assume I
have a Python 3 virtual environment with the digitalocean library
installed. Write me a script that will create a droplet of the type
you described.
Certainly! Here's a Python script that utilizes the `digitalocean`
library to create a droplet on Digital Ocean based on the
specifications you mentioned:
python
import digitalocean
# Set up your Digital Ocean API token
api_token = 'YOUR_DIGITALOCEAN_API_TOKEN'
# Create a Droplet instance
manager = digitalocean.Manager(token=api_token)
# Define the parameters for creating the droplet
droplet_params = {
'name': 'my-droplet', # Set your desired droplet name
'region': 'nyc1', # Set your desired region
'size': 's-1vcpu-1gb', # Set the droplet size
'image': 'ubuntu-20-04-x64', # Set the droplet image
'ssh_keys': None, # Set SSH keys if required
'backups': False,
'ipv6': False,
'user_data': None,
'private_networking': None,
'volumes': None
}
# Create the droplet
droplet = digitalocean.Droplet(token=api_token, **droplet_params)
droplet.create()
print(f"Creating droplet with ID: {droplet.id}")
Before running the script, make sure you replace
`'YOUR_DIGITALOCEAN_API_TOKEN'` with your actual Digital Ocean API
token. Additionally, you can modify the parameters in the
`droplet_params` dictionary to match your specific requirements, such
as setting the droplet name, region, size, and image.
Once you execute the script, it will create a droplet with the
provided specifications on Digital Ocean and print the ID of the newly
created droplet.
Note: Ensure that you have the `digitalocean` library installed in
your Python virtual environment. If not, you can install it using `pip
install digitalocean`.
Here’s where things break down. When I try to install the digitalocean
library via pip
it’s not working. Turns out that there is no such
library. I even ask it for the URL for the package and get
https://pypi.org/project/digitalocean/, which is not a valid address.
I ask it to create the same script for the next library option,
python-digitalocean
, and get back essentially the same script. This
time the pip
install works. And, after getting a digitalocean API
key and placing it in my environment I end up with:
import digitalocean
import os
# Set up your Digital Ocean API token
api_token = os.environ.get('DIGITALOCEAN_APIKEY')
# Create a Droplet instance
manager = digitalocean.Manager(token=api_token)
# Define the parameters for creating the droplet
droplet_params = {
'name': 'ubuntu-s-1vcpu-1gb-tor1-01', # Set your desired droplet name
'region': 'tor1', # Set your desired region
'size': 's-1vcpu-1gb', # Set the droplet size
'image': 'ubuntu-22-04-x64', # Set the droplet image
'ssh_keys': ['ee:83:71:83:0d:38:48:fe:5e:ee:2e:72:69:83:7b:d5'], # Set SSH keys if required
'backups': False,
'ipv6': False,
'user_data': None,
'private_networking': None,
'volumes': None
}
# Create the droplet
droplet = digitalocean.Droplet(token=api_token, **droplet_params)
droplet.create()
print(f"Creating droplet with ID: {droplet.id}")
This runs perfectly.
At this point I’m mostly done for the night and need to shutdown, but of course I write a script for that.
import digitalocean
import os
import sys
# Set up your Digital Ocean API token
api_token = os.environ.get('DIGITALOCEAN_APIKEY')
# Get the droplet
droplet = digitalocean.Droplet.get_object(api_token=api_token, droplet_id=sys.argv[1])
droplet.destroy()
So we can now create and destroy a simple droplet instance on Digital Ocean. Next we will move on to install Docker in the instance.