dro*_*nar 4 amazon-ec2 amazon-web-services boto3 amazon-ami
我想获得我拥有的所有ami图像.我试过以下的东西:
ec2 = boto3.resource('ec2')
owner_id = 'owner_id'
filters = [{'Name': 'owner-id', 'Values': [owner_id]}]
images = ec2.images.filter(Filters=filters).all()
Run Code Online (Sandbox Code Playgroud)
但我需要将owner_id explicid放在代码中.从aws凭证自动执行此操作是否是任何解决方案?
小智 5
您应该能够为所有者使用self.这就是我使用的.
boto3conn = boto3.resource("ec2", region_name="us-west-2")
images = boto3conn.images.filter(Owners=['self'])
Run Code Online (Sandbox Code Playgroud)
您可以使用STS API来获取此信息。
我发现这篇文章谈论它:geting the current user account-id in boto3
所以你需要的代码是:
ec2 = boto3.resource('ec2', region_name='us-east-1')
owner_id = boto3.client('sts').get_caller_identity().get('Account')
filters = [{'Name': 'owner-id', 'Values': [owner_id]}]
images = ec2.images.filter(Filters=filters).all()
Run Code Online (Sandbox Code Playgroud)
确保将区域名称更改为正确的名称,或者如果您已在环境中的其他位置设置了该名称,则将其保留。