简体   繁体   中英

Need to boot hourly workspaces but start_workspaces() and describe_workspaces() limited to 25

I have the below script that works on booting workspaces, but it gets the first 25 Workspaces, despite being AVAILABLE or STOPPED state.

I am running more than 25 Workspaces in my environment and I am trying to figure out what is there I need to add on it, in order to check all workspaces in my environment (50<) and initiate START to the ones that are in a STOPPED state.

I look forward hearing your feedback.

Thanks

 import boto3 workspaces = boto3.client('workspaces') def lambda_handler(event, context): workspaces_client_list = workspaces.describe_workspaces() for workspaces_info in workspaces_client_list['Workspaces']: workspace_id = workspaces_info['WorkspaceId'] workspace_state = workspaces_info['State'] if workspace_state == 'STOPPED': start_workspaces(workspace_id) def start_workspaces(workspace_id): workspaces.start_workspaces( StartWorkspaceRequests = [ { 'WorkspaceId': workspace_id }, ] )

The documentation states that you can provide a Limit in the request parameters

Limit

The maximum number of items to return.
Type: Integer
Valid Range: Minimum value of 1. Maximum value of 25.
Required: No

You only get 25 items because it's the maximum number of items you can get. To get all items you have to check in any response whether there NextToken . If there is a next token you have to use it for the next request, you iterate this until there is no next token left.

def lambda_handler(event, context):
   workspaces_client_list = get_all_workspaces()

   for workspaces_info in workspaces_client_list:
     workspace_id = workspaces_info['WorkspaceId']
     workspace_state = workspaces_info['State']

     if workspace_state == 'STOPPED':
       start_workspaces(workspace_id)

def get_all_workspaces():
   response = workspaces.describe_workspaces()
   workspaces_client_list = response['Workspaces']
   while "NextToken" in response:
    response = workspaces.describe_workspaces(NextToken=response['NextToken'])
    workspaces_client_list.extend(response['Workspaces'])    
    
   return workspaces_client_list       

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM