Reputation: 569
I use boto3 to get a list of all instances like this.
id: i-fa512784, zone: us-east-1, state: running, name: redis, env: staging-db, app: php
id: i-fa112784, zone: us-east-1, state: running, name: redis, env: production, app: php
I would like to create a single string for all values per instance. I.e. each of instances should have own string with own keys. My goal is to put this data into Prometheus.
I have got stuck on parsing nested "Tags": [
to get all values and output all of them into one string
My code
#!/usr/bin/python3
import boto3.utils
boto3.setup_default_session(profile_name='profile')
client = boto3.client('ec2')
response = client.describe_instances(
MaxResults=10,
)
for r in response['Reservations']:
for i in r['Instances']:
for tags in i['Tags']:
print ('id:',i['InstanceId'], 'zone:',i['Placement']['AvailabilityZone'], 'state:',i['State']['Name'])
Thank you in advance
Upvotes: 1
Views: 766
Reputation: 1532
Following is the code, you might need to change logic to append data to tag_values_list
;
#!/usr/bin/python3
import boto3.utils
boto3.setup_default_session(profile_name='profile')
client = boto3.client('ec2')
response = client.describe_instances(
MaxResults=10,
)
for r in response['Reservations']:
for i in r['Instances']:
tag_values_list = []
for tags in i['Tags']:
for key, value in tags.items():
tag_values_list.append(value)
print('id:', i['InstanceId'], 'zone:', i['Placement']['AvailabilityZone'], 'state:', i['State']['Name'],
'tags:',
tag_values_list)
Upvotes: 3