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
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)
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