python:迭代这个特定的dict(boto3)
我和python和boto3相当新。我从所有ASG中获取所有EC2实例。
我需要获得的价值是'instanceId':
这就是我从AWS 中获得dict的方式:
client = boto3.client('autoscaling')
output = client.describe_auto_scaling_instances()
我尝试过许多没有结果的变化(键错误):
for x in output:
print(x[0][0])
for x in output.values():
print(x)
这是我目前的回答:
{
'AutoScalingInstances': [{
'InstanceId': 'i-022d69fc77a62b787',
'InstanceType': 't3.medium',
'AutoScalingGroupName': 'sarasa',
'AvailabilityZone': 'us-east-1c',
'LifecycleState': 'InService',
'HealthStatus': 'HEALTHY',
'LaunchConfigurationName': 'sarasa',
'ProtectedFromScaleIn': False
}, {
'InstanceId': 'i-055b3d5cefec5c3f3',
'InstanceType': 't3.small',
'AutoScalingGroupName': 'pepe',
'AvailabilityZone': 'us-east-1a',
'LifecycleState': 'InService',
'HealthStatus': 'HEALTHY',
'LaunchConfigurationName': 'pepe',
'ProtectedFromScaleIn': False
}],
'ResponseMetadata': {
'RequestId': '6333c5-e9c7-47e7-9060-asdadadad',
'HTTPStatusCode': 200,
'HTTPHeaders': {
'x-amzn-requestid': '6333c5-e9c7-47e7-9060-asdadadad',
'content-type': 'text/xml',
'content-length': '4399',
'vary': 'accept-encoding',
'date': 'Tue, 07 Jun 2022 13:00:12 GMT'
},
'RetryAttempts': 0
}
}
i'm fairly new with python and BOTO3. I'm getting all the EC2 instances from all my ASG.
The value I need to get is 'InstanceId':
This is how i get the dict from AWS:
client = boto3.client('autoscaling')
output = client.describe_auto_scaling_instances()
I've tried with many variations of this without results (key error):
for x in output:
print(x[0][0])
for x in output.values():
print(x)
This is my current response:
{
'AutoScalingInstances': [{
'InstanceId': 'i-022d69fc77a62b787',
'InstanceType': 't3.medium',
'AutoScalingGroupName': 'sarasa',
'AvailabilityZone': 'us-east-1c',
'LifecycleState': 'InService',
'HealthStatus': 'HEALTHY',
'LaunchConfigurationName': 'sarasa',
'ProtectedFromScaleIn': False
}, {
'InstanceId': 'i-055b3d5cefec5c3f3',
'InstanceType': 't3.small',
'AutoScalingGroupName': 'pepe',
'AvailabilityZone': 'us-east-1a',
'LifecycleState': 'InService',
'HealthStatus': 'HEALTHY',
'LaunchConfigurationName': 'pepe',
'ProtectedFromScaleIn': False
}],
'ResponseMetadata': {
'RequestId': '6333c5-e9c7-47e7-9060-asdadadad',
'HTTPStatusCode': 200,
'HTTPHeaders': {
'x-amzn-requestid': '6333c5-e9c7-47e7-9060-asdadadad',
'content-type': 'text/xml',
'content-length': '4399',
'vary': 'accept-encoding',
'date': 'Tue, 07 Jun 2022 13:00:12 GMT'
},
'RetryAttempts': 0
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您尝试过什么,但是您可以做以下操作(并根据所需的内容进行调整)
会返回:
I'm not sure exactly what have you tried, but you could do the following (and adjust according to what you need)
it will return: