-1

I am new to Python. I have a script (in AWS Lambda function) that loops over my AWS workspace items and prints out some info, however, it only prints out about 25 of the items instead of 200+ items. So I turned to paginators. But it is not working as the script is not printing out the info. Rather it is printing "uh oh".

Here is the script:

from pprint import pprint
client = boto3.client('workspaces')
final_detail = []
final_detail.append(['ComputerName', 'WorkspaceId', 'IpAddress', 'state', 'UserName', 'BundleId', 'WorkspaceProperties'])
paginator = client.get_paginator('describe_workspaces')

try:
    for workspaces in paginator.paginate():
        for workspaceInfo in workspaces: 
        each_detail = workspaceInfo["ComputerName"], ["WorkspaceId"],["IpAddress"], ["state"], ["UserName"], ["BundleId"], ["WorkspaceProperties"]
        final_detail.append(each_detail)
#print(final_detail)

except = None
print(uh oh)
         
mindcoder
  • 1
  • 1
  • try printing the error message, except Exception as e: print(e) – LuckyTuvshee Jul 19 '22 at 08:45
  • Does this answer your question? [How to use Boto3 pagination](https://stackoverflow.com/questions/39201093/how-to-use-boto3-pagination) – LuckyTuvshee Jul 19 '22 at 08:46
  • @LuckyTuvshee That prints out 'ComputerName'. Something is wrong with the second for loop? – mindcoder Jul 19 '22 at 09:51
  • Why are you iterating over `workspaceInfo["ComputerName"]`? That's a string containing the name of the computer for the current workspace. You should not be iterating over it. Perhaps you meant to iterate over a list of keys such as `['ComputerName', 'WorkspaceId', 'IpAddress', ...]` so you could get each attribute of interest. – jarmod Jul 19 '22 at 22:38
  • Note that your exception handler is not as helpful as it could be. If an exception happens, you need to know what it was. Swallowing the exception and printing 'uh oh' is of limited help to you (or us). – jarmod Jul 20 '22 at 00:59
  • @jarmod, yes! that is exactly what i want. But that still don't solve the TypeError: string indices must be integer problem. I am still getting the erroro even though i have changed it. – mindcoder Jul 20 '22 at 01:02
  • @jarmod, you are right, the code has been updated. Although still the same problem. I am not sure what i am doing wrong. – mindcoder Jul 20 '22 at 01:08

1 Answers1

0

workspaceInfo is an iterator in your code, so you're accessing wrong object. This should work.

paginator = client.get_paginator('describe_workspaces')
for workspaces in paginator.paginate():
    for workspaceInfo in workspaces['Workspaces']: 
        each_detail = workspaceInfo["ComputerName"], workspaceInfo["WorkspaceId"], workspaceInfo["IpAddress"], workspaceInfo["state"], workspaceInfo["UserName"], workspaceInfo["BundleId"], workspaceInfo["WorkspaceProperties"]
        final_detail.append(each_detail)
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
LuckyTuvshee
  • 1,166
  • 8
  • 16
  • I got this error: "TypeError: string indices must be integers" on this line: each_detail = workspaceInfo["ComputerName"],["WorkspaceId"],["IpAddress"],["state"],["UserName"],["BundleId"], ["WorkspaceProperties"] – mindcoder Jul 19 '22 at 10:39
  • @John. That still does not help. Thank you though. I appreciate it. It looks like my workspaceInfo is a string, not a dictionary? – mindcoder Jul 19 '22 at 16:34
  • Okay, try this fix using `workspaces['Workspaces']` -- it's hard for me to debug because I'm not running any Workspaces. – John Rotenstein Jul 19 '22 at 22:32
  • @JohnRotenstein, yeah that does not work, mate. – mindcoder Jul 20 '22 at 01:03
  • To debug the situation, you should print `workspaceInfo` and see the content. You can then fix the code based on what you see. – John Rotenstein Jul 20 '22 at 01:12
  • @JohnRotenstein, i see. So that actually printed "ResponseMetadata." – mindcoder Jul 20 '22 at 01:19