简体   繁体   English

如何使用 boto3 获取我的 ec2 实例的 arn

[英]How to get arn of my ec2 instance using boto3

I want the arn of all my ec2 instances.我想要我所有 ec2 实例的 arn。 The describe_instances() does not give out the instances. describe_instances() 不会给出实例。 Is there any other method or some way that may list out all the arns as well.是否有任何其他方法或某种方式也可以列出所有的arns。 I need them to be stored in a database,also I'm working on sample instances for now,this eventually needs to work for multiple accounts as well.我需要将它们存储在数据库中,而且我现在正在处理示例实例,这最终也需要为多个帐户工作。 So, a solution that will work throughout will be helpful.因此,一个贯穿始终的解决方案将很有帮助。

You can recreate it yourself:您可以自己重新创建它:

arn:aws:ec2:<REGION>:<ACCOUNT_ID>:instance/<instance-id>

For this purpose, I think you can even use * as and <ACCOUNT_ID>, and it will work.为此,我认为您甚至可以使用 * as 和 <ACCOUNT_ID>,它会起作用。

for more info you can see this question有关更多信息,您可以看到这个问题

Actually instances['Reservations'][0]['Instances'] may not have all instances.实际上 instances['Reservations'][0]['Instances'] 可能没有所有实例。 Instances are grouped together by security groups.Different security groups means many list elements will be there.实例按安全组分组在一起。不同的安全组意味着会有许多列表元素。 To get every instance in that region, you need to use the code below.要获取该区域中的每个实例,您需要使用以下代码。

Note: ['Reservations'][0]['Instances'] doesn't list all the instances, It only gives you the instances which are grouped by the first security group.注意:['Reservations'][0]['Instances'] 没有列出所有实例,它只提供按第一个安全组分组的实例。 If there are many groups you won't get all instances.如果有很多组,您将不会获得所有实例。

import boto3
region = 'ap-south-1'

ec2 = boto3.client('ec2', region_name=region)

def list_instances():
    instance_ids = []
    response = ec2.describe_instances(Filters=[{'Name': 'instance-type', 'Values': ["t2.micro", "t3.micro"]}])
    instances_full_details = response['Reservations']
    for instance_detail in instances_full_details:
        group_instances = instance_detail['Instances']

        for instance in group_instances:
            instance_id = instance['InstanceId']
            instance_ids.append(instance_id)
    return instance_ids

instance_ids = list_instances()
print(instance_ids)

You can construct it yourself.您可以自己构建它。 In short it is:简而言之就是:

sts = boto3.client('sts')

region = boto3.Session().region_name

instance_id='id-3324234'

account_id = sts.get_caller_identity()['Account']

instance_arn=f"arn:aws:ec2:{region}:{account_id}:instance/{instance_id}"

print(instance_arn)

But to be pedantic and get AWS partition as well it would be:但要学究起来并获得 AWS分区,它将是:

def get_partition_for_region(session):
    # based on https://github.com/boto/botocore/pull/1715
    region_name = session.region_name
    partitions = session.get_available_partitions()
    for partition in partitions:
        regions = session.get_available_regions('stepfunctions', partition)
        if region_name in regions:
            return partition
    # use the default aws partition in case nothing is found
    return 'aws'
  
partition = get_partition_for_region(boto3.Session())

sts = boto3.client('sts')

region = boto3.Session().region_name

instance_id='id-3324234'

account_id = sts.get_caller_identity()['Account']

instance_arn=f"arn:{partition}:ec2:{region}:{account_id}:instance/{instance_id}"

print(instance_arn)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM