简体   繁体   中英

How to validate Amazon access key and secret key is correct?

I wrote a function to validate the AWS keys by just creating the ec2 connection object

import boto.ec2
try:
    ec2Conn = boto.ec2.connect_to_region(region, aws_access_key_id=access_key, aws_secret_access_key=secret_key)
    return ec2Conn
except boto.exception.EC2ResponseError as e:
    print e

But even if the secret key is wrong still it creates the ec2 connection object.

So I validate the access key and secret key by fetching the regions,

region = ec2Conn.get_all_regions()

Is there any method or way rather than the fetching region to validate the access key and secret key?

The only way to verify AWS credentials is to actually use them to sign a request and see if it works. You are correct that simply creating the connection object tells you nothing because it doesn't perform a request. So you have to pick some request that should always work, won't return a huge amount of data, and doesn't create any resources on the server side. I think the get_all_regions() request is a pretty good choice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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