简体   繁体   English

需要启动每小时工作空间,但 start_workspaces() 和 describe_workspaces() 限制为 25

[英]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.我有以下可用于启动工作区的脚本,但它获得了前 25 个工作区,尽管它是可用或已停止 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.我在我的环境中运行超过 25 个工作区,我试图弄清楚我需要在上面添加什么,以便检查我的环境中的所有工作区 (50<) 并启动 START 中的工作区停止 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

Limit限制

The maximum number of items to return.要返回的最大项目数。
Type: Integer型号:Integer
Valid Range: Minimum value of 1. Maximum value of 25.有效范围:最小值为 1。最大值为 25。
Required: No要求:否

You only get 25 items because it's the maximum number of items you can get.您只能获得 25 件物品,因为这是您可以获得的最大物品数量。 To get all items you have to check in any response whether there NextToken .要获取所有项目,您必须检查是否存在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       

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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