loop all objects in aws config using boto3

Viewed 447

i was new to aws boto3. Goal : i want pull all compliant& non-compliant rule from aws-config service and write in txt file as non-compliant_config.txt

The problem is when i when run this code , its not iterating through all the rules(my guess it stops at first 25)

import boto3
#counter to count total number of rules(objects) this code iterated.
count = 0 

client = boto3.client('config')
response = client.describe_config_rules()

for i in response['ConfigRules']:
    count = count +1 
    print(i)

print(count)

last line in my code print(count) says 25

Final point : i want to print(or write to txt file) all the AWS config-rules. Note : i also tried to look at boto3 pagination documentation, but i cant understand it to how to use it.

2 Answers

Probably you have to use NextToken from describe_config_rules, to keep iterating further. Alternatively you can use get_paginator('describe_config_rules') as explained in the docs.

import boto3
client = boto3.client('config')
count = 0 

paginator = client.get_paginator('describe_config_rules')

rules = paginator.paginate()

for rule in rules:
    for j in i['ConfigRules'] :
        count = count + 1
        print(j)

This code with loop through all the AWS config rules.

solution credits : https://stackoverflow.com/a/70647301/7995771

Related