python:迭代这个特定的dict(boto3)

发布于 2025-02-05 07:59:53 字数 1568 浏览 2 评论 0原文

我和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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我很OK 2025-02-12 07:59:53

我不确定您尝试过什么,但是您可以做以下操作(并根据所需的内容进行调整)

d = {
    '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
    }
}
for k in d.keys():
    for elem in d[k]:
        if s in elem:
            print(elem[s])

会返回:

i-022d69fc77a62b787
i-055b3d5cefec5c3f3

I'm not sure exactly what have you tried, but you could do the following (and adjust according to what you need)

d = {
    '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
    }
}
for k in d.keys():
    for elem in d[k]:
        if s in elem:
            print(elem[s])

it will return:

i-022d69fc77a62b787
i-055b3d5cefec5c3f3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文