Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 586
naga4ce Avatar asked May 14 '13 08:05

naga4ce


1 Answers

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.

like image 192
garnaat Avatar answered Sep 28 '22 07:09

garnaat