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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With