You can simply loop through using a for loop.
Using for loop you can traverse through all the instances.
Here is a simple program that you can use after configuring your IAM using using AWS CLI.
import boto3
ec2 = boto3.resource('ec2')
for instance in ec2.instances.all():
print (instance.id , instance.state)
Hope this helps!
If you need to know more about Python, join Python online course certification today.
Thanks!