Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get access_key and secret_key from boto3? [duplicate]

When I launch an EC2 instance with an IAM role I can use boto3 on that EC2instance and not have to specify aws access and secret keys because boto3 reads them automatically.

>>> import boto3
>>> s3 = boto3.resource("s3")
>>> list(s3.buckets.all())[0]
s3.Bucket(name='my-bucket-name')

Question

I'm wondering if there is any way to get the access key and secret key from boto3? For example, how can I print them on to the standard console using print

like image 675
Anthony Avatar asked Dec 21 '16 19:12

Anthony


1 Answers

There sure is (docs):

from boto3 import Session

session = Session()
credentials = session.get_credentials()
# Credentials are refreshable, so accessing your access key / secret key
# separately can lead to a race condition. Use this to get an actual matched
# set.
current_credentials = credentials.get_frozen_credentials()

# I would not recommend actually printing these. Generally unsafe.
print(current_credentials.access_key)
print(current_credentials.secret_key)
print(current_credentials.token)
like image 67
Jordon Phillips Avatar answered Nov 17 '22 14:11

Jordon Phillips