import boto3 client = boto3.client('ecs') response = client.list_clusters() response1 = client.list_container_instances( cluster='Cluster1234', ) print(response, response1)
The response from list_container_instances give you a list of container instance ARNs in containerInstanceArns
.
You can then pass that as containerInstances
to describe_container_instances to get a list of container instances and their underlying EC2 instance IDs in containerInstances[*].ec2InstanceId
.
You can then pass those EC2 instance IDs as InstanceIds
to describe_instances which will give you, among other things, their IP addresses.
There may be a more concise way to do this, but I'm not immediately aware of it.
Here's an example:
import boto3
ecs = boto3.client('ecs', region_name='us-east-1')
ec2 = boto3.client('ec2', region_name='us-east-1')
rc = ecs.list_clusters()
for cluster in rc['clusterArns']:
ci = ecs.list_container_instances(cluster=cluster)
if len(ci['containerInstanceArns']) > 0:
r2 = ecs.describe_container_instances(
cluster=cluster,
containerInstances=ci['containerInstanceArns'])
ids = [x['ec2InstanceId'] for x in r2['containerInstances']]
r3 = ec2.describe_instances(InstanceIds=ids)
for r in r3['Reservations']:
for i in r['Instances']:
print("{0}: instance {1}, state {2}, AMI {3}, private IP {4}, public IP {5}".format(
cluster.split('/')[-1],
i['InstanceId'],
i['State']['Name'],
i['ImageId'],
i['PrivateIpAddress'] if 'PrivateIpAddress' in i else 'n/a',
i['PublicIpAddress'] if 'PublicIpAddress' in i else 'n/a'))
This will yield an output something like this:
cluster-101: instance i-01234e5ea85f30ba7, state running, AMI ami-045f1b3f87ed83659, private IP 10.0.0.222, public IP 54.166.303.1
cluster-101: instance i-023452ab72c755e01, state running, AMI ami-045f1b3f87ed83659, private IP 10.0.1.333, public IP 54.166.202.2
cluster-101: instance i-03456645cc5d9b19d, state running, AMI ami-045f1b3f87ed83659, private IP 10.0.1.444, public IP 54.166.101.3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.