Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing more than 100 stacks using boto3

We need to list all the stacks that are in CREATE_COMPLETE state. In our AWS account we have >400 such stacks. We have the following code written for this:

stack_session = session.client('cloudformation')
list_stacks = stack_session.list_stacks(StackStatusFilter=['CREATE_COMPLETE'])

However this lists only the first 100 stacks. We want to know how we can get all the stacks? We are using the python boto3 library.

like image 435
krishna_mee2004 Avatar asked Aug 30 '16 13:08

krishna_mee2004


1 Answers

I got this working using pagination. The code I wrote is below:

stack_session = session.client('cloudformation')
paginator = stack_session.get_paginator('list_stacks')
response_iterator = paginator.paginate(StackStatusFilter=['CREATE_COMPLETE'])
for page in response_iterator:
    stack = page['StackSummaries']
    for output in stack:
        print output['StackName']

That printed all the 451 stacks that we needed.

like image 168
krishna_mee2004 Avatar answered Nov 02 '22 03:11

krishna_mee2004