[英]Python boto3 (AWS EC2) list Nested JSON Data
我使用boto3來獲取這樣的所有實例的列表。
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
我想為每個實例的所有值創建一個字符串。 即每個實例都應該有自己的字符串和自己的鍵。 我的目標是將這些數據放入 Prometheus。
我一直在解析嵌套的"Tags": [
將所有值和 output 全部轉換為一個字符串
我的代碼
#!/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'])
先感謝您
以下是代碼,您可能需要將邏輯更改為 append 數據到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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.