theborngeek
theborngeek

Reputation: 151

Python Boto3 for ECS Help needed

I'm trying to list all the clusters in my AWS ECS account. I have approximately 13 Clusters running.

The below code prints only one cluster, whereas I want to print all the clusters. Is there a for loop which I can use?

Below prints only one cluster:

import boto3
client = boto3.client('ecs')
response = client.list_clusters(
    maxResults=50
)
print(response)

Below for looping doesn't work and throws an error

import boto3
client = boto3.client('ecs')
for response in client.list_cluster():
    print(response)

Any lead would highly be appreciated.

Upvotes: 0

Views: 991

Answers (1)

Raf
Raf

Reputation: 10097

If you look at the list_clusters api, the response syntax is:

 Response Syntax

{
    'clusterArns': [
        'string',
    ],
    'nextToken': 'string'
}

which means you'll get back a list([]) of ARNs which are unique resource identifiers in AWS.

Use describe_clusters api to then then get a description, as @jordanm said:

import boto3
client = boto3.client('ecs', region_name='us-east-2')
clusters = client.list_clusters(
    maxResults=50
)
clusters_arns = clusters['clusterArns']

clusters_descriptions = client.describe_clusters(
    clusters=clusters_arns
)

for cluster in clusters_descriptions['clusters']:
    print(cluster['clusterName'])

result is something like:

prod_nam
eu_nam
someothercluster

Upvotes: 1

Related Questions