I am trying to write a Python code in Lambda function which will start the stopped workspaces when the alarm triggers. The response type is dict.
But I am getting the error. Below is my code and error.
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"
]
}
If you look at the call's documentation, it says that it expects keyword StartWorkspaceRequests
, which is in itself a list of dicts:
{
'WorkspaceId': 'string'
},
The call doesn't accept arguments (just a value being passed without corresponding keyword). You need to adapt your call to conform to the format expected by 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)