Skip to content

Instantly share code, notes, and snippets.

@jschoormans
Created December 13, 2023 13:08
Show Gist options
  • Save jschoormans/fe732caf5af2e398beee47b9f261afd2 to your computer and use it in GitHub Desktop.
Save jschoormans/fe732caf5af2e398beee47b9f261afd2 to your computer and use it in GitHub Desktop.
'''
Sends a slack message to #general if there are any lambdalabs instances available
If there are, it will also send a message to #general with the name and price of the instance
If there are GPU instances available, it will also request one
'''
from slack_utils import send_slack_message
import requests
from dotenv import load_dotenv
import os
load_dotenv()
API_KEY = os.getenv('LAMBDALABS_TOKEN')
def instance_request(region, instance_type_name):
"""
Sends a request to launch an instance on Lambda Labs cloud.
Args:
region (str): The region where the instance will be launched.
instance_type_name (str): The name of the instance type.
Returns:
None
"""
payload = {
"region_name": region,
"instance_type_name": instance_type_name,
"ssh_key_names": [
"macbook"
],
"file_system_names": [
],
"quantity": 1,
"name": "training-node-1"
}
print('REQUESTING INSTANCE')
data = requests.post('https://cloud.lambdalabs.com/api/v1/instance-operations/launch',
auth=(API_KEY,
''), json=payload)
print(data.content)
data = requests.get('https://cloud.lambdalabs.com/api/v1/instances',
auth=(API_KEY,
''))
instance_list = data.json()['data']
N_INSTANCES = len(instance_list)
print('CURRENTLY, THERE ARE ' + str(N_INSTANCES) + ' INSTANCES RUNNING')
if N_INSTANCES > 0:
# I HAVE AN INSTANCE RUNNING - I DONT CARE ABOUT AVAILABILITY NOW
exit()
data = requests.get('https://cloud.lambdalabs.com/api/v1/instance-types',
auth=(API_KEY,
''))
available_instances = data.json()['data']
# available_instances is a dict?
# for all the entries in the dict, print the name and the price
availability_count = 0
for entry in available_instances:
capacity = available_instances[entry]['regions_with_capacity_available']
if len(capacity) > 0:
print(entry + ': ' + str(capacity))
price = available_instances[entry]['instance_type']['price_cents_per_hour']
name = available_instances[entry]['instance_type']['description']
region = available_instances[entry]['regions_with_capacity_available'][0]['name']
send_slack_message('#general', 'ℹ️ lambdalabs availability: ' + str(name) + ' (USD' + str(price/100) + ' per hour)')
availability_count += 1
if entry == 'gpu_1x_a10' or entry == 'gpu_1x_a100':
if N_INSTANCES < 1:
send_slack_message('#general', '✅ requesting instance', entry)
instance_request(region, entry)
if availability_count == 0:
send_slack_message('#general', '❌ no lambdalabs instances available')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment