Now that you have the API URL, you will start interacting with the API through its REST interface. This can be done directly through the Python Requests library or the PCluster Client library. You will use the latter for the reminder of this workshop.
For this step you will install the PCluster Client library and run a sample code to list your clusters programmatically.
pip3 install git+https://github.com/aws/aws-parallelcluster.git#subdirectory=api/client/src --use-feature=2020-resolver --user
pip3 install "aws-parallelcluster>=3" --use-feature=2020-resolver --user
list_clusters.py
.cat > list_clusters.py << EOF
#!/usr/bin/env python3
# utility libraries
import os
import json
from pprint import pprint
# pcluster client
import pcluster_client
from pcluster_client.api import cluster_operations_api,\
cluster_instances_api,\
cluster_compute_fleet_api
from pcluster_client.model.update_compute_fleet_request_content import UpdateComputeFleetRequestContent
from pcluster_client.model.requested_compute_fleet_status import RequestedComputeFleetStatus
# configure the client, use the API URL retrieved from the AWS ParallelCluster API sack output
configuration = pcluster_client.Configuration(
host = os.getenv("API_URL")
)
with pcluster_client.ApiClient(configuration) as api_client:
# Create an instance of the Cluster, Instances and Compute Fleet API classes
api_cluster_operations = cluster_operations_api.ClusterOperationsApi(api_client)
api_cluster_instances = cluster_instances_api.ClusterInstancesApi(api_client)
print("Getting the list of clusters")
clusters = api_cluster_operations.list_clusters()['clusters']
print(f"You have {len(clusters)}")
for cluster in clusters:
print(cluster['cluster_name'])
instances = api_cluster_instances.describe_cluster_instances(cluster['cluster_name'])
pprint(instances["instances"])
EOF
python3 list_clusters.py
pcluster list-clusters --query "clusters[0].clusterName"
Now that you understand how to retrieve information about your clusters, let’s use this knowledge to stop unused clusters.