Determine Workspace Usage

Amazon Workspaces can quickly add up and if your users aren't using them you may be paying for services that you don't need. So how can you tell if your workspaces are being used.  The manual way is to log into the Workspaces console and check the last activity date.  That may work for a few workspaces, but once you get over 5 or 10 that becomes pretty tedious.

Here's a python script that will run a quick report on your workspace.  A -1 will mean the user has never logged in after that number represents the number of days since the last login, so a zero would mean today.   I have a constant called THRESHOLD_DAYS set to 15, which basically says if you haven't logged in in the last 15 days you should be added to a list to check on.

The output is the a comma separated list of days since use, the workspace_id, and the user. The end of the report will include a summary or the total number of workspaces, the thresh hold count (those who haven't logged-in in X days) and number of users who have never logged in.

The program is written in python and uses boto3. It also uses the boto3 session, which looks for a either default AWS profile or an environment variable of AWS_PROFILE which is configured to the connect your account.  The profile will of course need the necessary security credentials in aws to access workspaces.

# report for AWS Workspace Usage
import boto3
from datetime import date, datetime

# const
THRESHOLD_DAYS = 15

# running counts
running_count = 0
non_used_count = 0
threshold_count = 0


def run_it(message):

print(f'{message}')
aws = boto3.session.Session()
client = aws.client('workspaces')

response = client.describe_workspaces()
workspaces = response['Workspaces']
print_workspace(client, workspaces)
while "NextToken" in response:
response = client.describe_workspaces(NextToken=response["NextToken"])
workspaces = response['Workspaces']
print_workspace(client, workspaces)
print('')
print(f'Total Workspaces: {running_count}')
print(f'Never Been Used Count: {non_used_count}')
print(f'Past threshold Count: {threshold_count} haven\'t been used in the last {THRESHOLD_DAYS} days ')


def print_workspace(client, workspaces):
workspaceIds = [workspace['WorkspaceId'] for workspace in workspaces]
response = client.describe_workspaces_connection_status(WorkspaceIds=workspaceIds)
statuses = response['WorkspacesConnectionStatus']
for status in statuses:
workspaceId = status["WorkspaceId"]
user_name = get_user_for_workspace(workspaceId, workspaces)

if "LastKnownUserConnectionTimestamp" not in status:
# no known usage, so they've never used it
print_usage(-1, workspaceId, user_name)

else:
tmp = status['LastKnownUserConnectionTimestamp']
last_used_date = tmp.replace(tzinfo=None)
today = datetime.now()
delta = today - last_used_date
print_usage(delta.days, workspaceId, user_name)


def print_usage(days, workspace_id, user):
print(f'{days}, {workspace_id} ,{user}')

global running_count
global non_used_count
global threshold_count
global THRESHOLD_DAYS
running_count += 1

if days < 0:
non_used_count += 1
if days > THRESHOLD_DAYS:
threshold_count += 1


def get_user_for_workspace(workspace_id, workspaces):
for workspace in workspaces:
if workspace["WorkspaceId"] == workspace_id:
return workspace["UserName"]

return ""



if __name__ == '__main__':
run_it('AWS Workspace Report')



Image Credits: Photo by Luke Chesser on Unsplash