我正试图在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_handlern client.start_workspaces(i)n",
" File "/var/runtime/botocore/client.py", line 354, in _api_calln raise TypeError(n"
]
}
如果你查看调用的文档,它说它需要关键字StartWorkspaceRequests
,它本身就是一个dicts:列表
{
'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)