Find Unused and/or Rarely Used Amazon Workspaces
We all know atbout the famous 3C - Cloud Cost Creep, how cloud costs can get out of control and you get that shock when you receive the love letter at the end of the month - aka "Monthly bill" :)
One major area is leakage is Compute that is left running and Amazon Workspaces are no difference in that matter. The following Python script checks unused Workspaces so you can clean them up.
import boto3
from datetime import datetime, timedelta
boto3.setup_default_session(profile_name='vicroads')
ws = boto3.client('workspaces')
### calculate time diffrence
def calculate_age(date):
now = datetime.utcnow().date()
then = date.date()
age = now - then
return age.days
### return Workspace UserName
def user_name(wsid):
items = ws.describe_workspaces()
for item in items['Workspaces']:
if item['WorkspaceId'] == wsid:
return item['UserName']
# Body
days= 50
items = ws.describe_workspaces_connection_status()
dis_num=0
List_Idle=[]
never_num=0
List_Never=[]
for item in items['WorkspacesConnectionStatus']:
lastknown = item.get('LastKnownUserConnectionTimestamp')
if item['ConnectionState'] == "DISCONNECTED":
if lastknown is not None and calculate_age(lastknown) > days:
dis_num +=1
List_Idle.append("User name: "+user_name(item['WorkspaceId'])+" with workspace id ("+item['WorkspaceId']+") has been idle for "+str(calculate_age(lastknown))+".")
if lastknown is None:
never_num +=1
List_Never.append("User name: "+user_name(item['WorkspaceId'])+" with workspace id ("+item['WorkspaceId']+") has not been used yet.")
if dis_num > 0:
print ("==========================================================")
print (dis_num," workspaces has been disconnected for more than ", days,":")
print ("==========================================================")
for i in List_Idle:
print (i,"\n")
if never_num > 0:
print ("==========================================================")
print (never_num," workspaces has never been used:")
print ("==========================================================")
for j in List_Never:
print (j,"\n")