Latest AMI ID in a given Region using a set of fil

2019-07-21 08:19发布

I am trying to run the code in the below, however I have errors while executing, could you please advise on the changes.

latest-ami

    [ec2-user@ip-XXXXXXXX ~]$ python latest-ami.py us-west-2
{u'Images': [], 'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': '01ef1583-e7bc-4279-9081-28cbf9345f7c', 'HTTPHeaders': {'date': 'Wed, 01 Aug 2018 12:00:01 GMT', 'content-length': '219', 'content-type': 'text/xml;charset=UTF-8', 'server': 'AmazonEC2'}}}
[ec2-user@ip-172-31-19-75 ~]$ python latest-ami.py us-west-1
{u'Images': [], 'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': 'e61cdeb3-e44b-4e4e-bf4e-ed4681529757', 'HTTPHeaders': {'date': 'Wed, 01 Aug 2018 12:06:00 GMT', 'content-length': '219', 'content-type': 'text/xml;charset=UTF-8', 'server': 'AmazonEC2'}}}
[ec2-user@ip-XXXXXXXXXXX ~]$

I am expecting something like below as the output:

$ latest-ami.py us-west-2
ami-b04e92d0

My code now looks like below the last three lines from the original latest-ami

source_image = newest_image(response['Images'])
#print(source_image['ImageId'])
print(response)

filters are like below:

filters = [ {
        'Name': 'name',
        'Values': ['amzn-ami-hvm-*']
    },{
        'Name': 'description',
        'Values': ['Amazon Linux AMI*']
    },{
        'Name': 'architecture',
        'Values': ['x86_64']
    },{
        'Name': 'owner-alias',
        'Values': ['amazon']
    },{
        'Name': 'owner-id',
        'Values': ['000000000000']
    },{
        'Name': 'state',
        'Values': ['available']
    },{
        'Name': 'root-device-type',
        'Values': ['ebs']
    },{
        'Name': 'virtualization-type',
        'Values': ['hvm']
    },{
        'Name': 'hypervisor',
        'Values': ['xen']
    },{
        'Name': 'image-type',
        'Values': ['machine']
    } ]

response = client.describe_images(Owners=['amazon'], Filters=filters)

1条回答
狗以群分
2楼-- · 2019-07-21 09:01

Your script is looking for a parameter value from command line when you execute the script.

Line 8: region = sys.argv[1]

sys.argv stores the command line values provided during the script run along with script name.

sys.argv[0] : Script name

sys.argv[1]: First command line value

Try running your code like this: python latest-ami.py us-east-1

Depending on what region u want to run your code on change the value i provided for region.

To get the image id: print(source_image['ImageId']) add this command to your code

查看更多
登录 后发表回答