Friday, April 15, 2022

[SOLVED] Fetch boto3 credentials only from EC2 instance profile

Issue

The boto3 documentation lists the order in which credentials are searched and the credentials are fetched from the EC2 instance metadata service only at the very last.

How do I force boto3 to fetch the credentials only from the EC2 instance profile or the instance metadata service?

I came across this which lets me get the temporary credentials from the metadata service and then I could pass this on to create a boto3 session.

However my question is whether there is a better way to do this? Is it possible to create a boto3 session by specifying the provider to use ie InstanceMetadataProvider - link? I tried searching the docs a lot, but couldn't figure it out.

The reason - the context under which this script runs also has environment variables with AWS keys set which would obviously take precedence, however I need the script to run only with the IAM role assigned to the EC2 instance.


Solution

So I ended up doing this, works as expected. Always uses the temp creds from the instance role. The script is short-lived so the validity of the creds is not an issue.

from botocore.credentials import InstanceMetadataProvider, InstanceMetadataFetcher

provider = InstanceMetadataProvider(iam_role_fetcher=InstanceMetadataFetcher(timeout=1000, num_attempts=2))
creds = provider.load().get_frozen_credentials()
client = boto3.client('ssm', region_name='us-east-1', aws_access_key_id=creds.access_key, aws_secret_access_key=creds.secret_key, aws_session_token=creds.token)

If there is a better way to do, please feel free to post.



Answered By - Vivek Thomas
Answer Checked By - Katrina (WPSolving Volunteer)