如何使用 boto 3 根据实例 id 打印实例名称

Dav*_*ave 2 amazon-web-services boto3

如何使用 boto3 根据 InstanceId 检索实例名称?

connection = boto3.resource('ec2')
instances = connection.instances.filter(InstanceIds=[instanceid])
        for instance in instances:
            instance_name=???
Run Code Online (Sandbox Code Playgroud)

Jor*_*ips 6

您需要获取标签Name

def get_name(instance):
    for tag in instance.tags:
        if tag['Key'] == 'Name':
            return tag['Value']
Run Code Online (Sandbox Code Playgroud)