1

I have created a stacks in Cloud formation. When try to get all the stacks through aws cli its working but i tried to get all the stack via boto3 API in python. Here, it didn't collect all the stacks information. few of the stacks information missed. I compared both cli and boto3 api results.

I used below commands to list all the stacks

CLI

aws cloudformation list-stacks --stack-status-filter CREATE_COMPLETE

boto3 API

client = boto3.client('cloudformation')
stacks = client.list_stacks(StackStatusFilter=["CREATE_COMPLETE"])
Maurice
  • 11,482
  • 2
  • 25
  • 45
Rajesh R
  • 11
  • 1

1 Answers1

0

The ListStacks API is paginated, i.e. it only returns a fixed result size (1MB) until you need to retrieve the next page of the results. The CLI handles pagination automatically for you - in boto3 you have to implement it yourself.

In boto3 you can use a paginator like this to get all pages:

import boto3

client = boto3.client("cloudformation")

paginator = client.get_paginator("list_stacks")

response_iterator = paginator.paginate(
    StackStatusFilter=["CREATE_COMPLETE"],
)

# This contains stack summaries of all stacks in CREATE_COMPLETE
stack_summaries = [
    response["StackSummaries"] for response in response_iterator
]

# Instead of printing you can do something with the stacks :)
print(f"Found {len(stack_summaries)} stacks in CREATE_COMPLETE")
Maurice
  • 11,482
  • 2
  • 25
  • 45