2

How to I list all the log groups in Cloudwatch using Boto3. When I try the below syntax. I get error.

client = boto3.client('logs')

response = client.describe_log_groups(limit=51)

validation error detected: Value '51' at 'limit' failed to satisfy constraint: Member must have value less than or equal to 50

Based on documentation we could go above 50

limit (integer) -- The maximum number of items returned. If you don't specify a value, the default is up to 50 items.

kumar
  • 8,207
  • 20
  • 85
  • 176
  • similar to this post here: https://stackoverflow.com/questions/72338687/boto3-cloudwatch-log-returns-only-100-loggroups – nate Mar 08 '23 at 19:38

2 Answers2

5

When in doubt, always go to the API documentation, which says the following:

Valid Range: Minimum value of 1. Maximum value of 50.

To solve your problem, you need to use a paginator:

paginator = logs_client.get_paginator('describe_log_groups')
for page in paginator.paginate():
    for group in page['logGroups']:
        print(group)
Parsifal
  • 3,928
  • 5
  • 9
0

The paginate() function includes a build_full_result() method, eliminating the for page in paginator.paginate():

paginator = logs_client.get_paginator('describe_log_groups')
log_groups = paginator.paginate().build_full_result()
for group in page['logGroups']:
    print(group)
allymn
  • 138
  • 11