如何使用boto3在AWS中列出所有Red Hat的AMI镜像ID?

huangapple go评论80阅读模式
英文:

How to list down all the AMI image IDs for Red Hat in AWS using boto3?

问题

I need the text to be translated into Chinese. Here it is:

我正在处理一个任务,需要验证用户是否使用Cloudtrail日志创建了一个EC2实例,并使用RHEL最新镜像

我能够使用boto3找到用户使用的AMI镜像ID

我想检查用户使用的AMI镜像ID是否存在于RHEL提供的镜像ID列表中。

我应该如何列出RedHat Inc的所有AMI镜像ID

我已经查阅了boto3文档describe_images()

在其中有一个参数Owners,但不确定我应该传递什么来获取Redhat的所有镜像ID。

**附注:**发现Redhat所有者ID是-309956199498,我们可以使用ec2.describe_images()进行查询。

英文:

I'm working on a task where I need to validate if a user has created an EC2 instance with RHEL latest image using Cloudtrail logs for that user.

I was able to find the AMI image IDs used by that user using boto3.

I want to check whether the AMI image ID used by the user is present in the list of image IDs provided by RHEL.

How do I list down all the AMI image IDs for RedHat Inc.

I have gone through the boto3 documentation describe_images()

There's a param Owners in it. But not sure what should I pass in there to get all the image IDs for Redhat.

PS: Found that Redhat owner ID is - 309956199498 and we can query with it using ec2.describe_images()

答案1

得分: 2

以下代码列出了指定区域中所有 RHEL-8 AMI 的 ID:

import boto3

ec2 = boto3.resource('ec2', region_name='us-east-1')
filters = [
    {'Name': 'owner-id', 'Values': ['309956199498']},
    {'Name':'name', 'Values':['RHEL-8*']}
]
images = ec2.images.filter(Filters=filters).all()

for image in images:
    print(image.id)

来源:https://access.redhat.com/solutions/15356.

英文:

The following code lists all RHEL-8 AMI IDs in the specified region:

import boto3
 
ec2 = boto3.resource('ec2', region_name='us-east-1')
filters = [
    {'Name': 'owner-id', 'Values': ['309956199498']}, 
    {'Name':'name', 'Values':['RHEL-8*']}
]
images = ec2.images.filter(Filters=filters).all()

for image in images:
    print(image.id)

Adapted from the CLI command in https://access.redhat.com/solutions/15356.

huangapple
  • 本文由 发表于 2020年1月3日 21:04:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定