Search code examples
pythonamazon-web-servicesboto3amazon-ecs

boto3 ecs client pagination issue


I am trying to list all services in aws ECS cluster using python boto3, it can only list 100 services maximum. So trying with pagination API.

import boto3

session = boto3.Session(profile_name='dev')

client = session.client('ecs')

paginator = client.get_paginator('list_services')


resp = paginator.paginate( cluster='test')

for i in resp:
  print resp

I tried to loop with resp but it does not list all services.. It does provide nextToken in the resp.

Any idea how to use that nextToken and get all services in a ECS cluster using python.


Solution

  • There is a flaw in your loop. Fix it to:

    for i in resp:
      print i
    

    One way to do what you are asking using nextToken:

    resp = paginator.paginate( cluster='test')
    print resp
    while 'nextToken' in resp:
      resp = paginator.paginate( cluster='test', nextToken=resp['nextToken'])
      print resp