0

我正在尝试在 Lambda 函数中编写 Python 代码,该代码将在警报触发时启动停止的工作区。响应类型是 dict。

但我得到了错误。下面是我的代码和错误。

import json
import boto3

client = boto3.client('workspaces')
    
def lambda_handler(event, context):
    response = client.describe_workspaces(
        DirectoryId='d-966714f114'
    )
        
    #print(response)
    
    print("hello")

    for i in response['Workspaces']:
        if(i['State']== 'STOPPED'):
            print(i['WorkspaceId'])
            client.start_workspaces(i['WorkspaceId'])
{
    "errorMessage": "start_workspaces() only accepts keyword arguments.",
    "errorType": "TypeError",
    "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 21, in lambda_handler\n    client.start_workspaces(i)\n",
    "  File \"/var/runtime/botocore/client.py\", line 354, in _api_call\n    raise TypeError(\n"
    ]
}
4

1 回答 1

2

如果您查看调用的文档,它会说它需要关键字StartWorkspaceRequests,它本身就是一个字典列表:

{
    'WorkspaceId': 'string'
},

该调用不接受参数(只是传递的值没有相应的关键字)。您需要调整您的调用以符合 boto3 预期的格式。

import json
import boto3

client = boto3.client('workspaces')
    
def lambda_handler(event, context):
    response = client.describe_workspaces(
        DirectoryId='d-966714f114'
    )
        
    workspaces_to_start = []
         
    for i in response['Workspaces']:
        if(i['State']== 'STOPPED'):
            workspaces_to_start.append({'WorkspaceId': i['WorkspaceId']})

    if workspaces_to_start:
        client.start_workspaces(StartWorkspaceRequests=workspaces_to_start)
于 2021-07-15T09:05:10.820 回答