python 使用boto3列出正在运行的EC2实例的示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 使用boto3列出正在运行的EC2实例的示例相关的知识,希望对你有一定的参考价值。

import boto3

ec2 = boto3.resource('ec2')

def lambda_handler(event, context):
    # create filter for instances in running state
    filters = [
        {
            'Name': 'instance-state-name', 
            'Values': ['running']
        }
    ]
    
    # filter the instances based on filters() above
    instances = ec2.instances.filter(Filters=filters)

    # instantiate empty array
    RunningInstances = []

    for instance in instances:
        # for each instance, append to array and print instance id
        RunningInstances.append(instance.id)
        print instance.id

以上是关于python 使用boto3列出正在运行的EC2实例的示例的主要内容,如果未能解决你的问题,请参考以下文章