- Published on
Temporary IAM Credentials from EC2 Instance Metadata using Python
- Authors
- Name
- Ruan Bekker
- @ruanbekker
From a Best Practice Perspective its good not having to pass sensitive information around, and especially not hard coding them.
Best Practice: Security
One good way is to use SSM with KMS to Encrypt/Decrypt them, but since EC2 has a Metadata Service available, we can make use of that to retrieve temporary credentials. One requirement though, is that the instance will require an IAM Role where the code will be executed on. The IAM Role also needs to have sufficient privileges to be able to execute, whatever you need to do.
The 12 Factor Methodology however states to use config in your environment variables, but from the application logic, its easy to save it in our environment.
Scenario: Applications on AWS EC2
When you run applications on Amazon EC2 the nodes has access to the EC2 Metadata Service, so in this case our IAM Role has a Policy that authorizes GetItem on our DynamoDB table, therefore we can define our code with no sensitive information, as the code will do all the work to get the credentials and use the credentials to access DynamoDB.
Use Temporary Credentials to Read from DynamoDB using botocore
In this example we will get the temporary credentials from the metadata service, then define the temporary credentials in our session to authorize our request against dynamodb to read from our table:
>>> import boto3
>>> from botocore.utils import InstanceMetadataFetcher
>>> from botocore.credentials import InstanceMetadataProvider
>>> provider = InstanceMetadataProvider(iam_role_fetcher=InstanceMetadataFetcher(timeout=1000, num_attempts=2))
>>> creds = provider.load()
>>> session = boto3.Session(
aws_access_key_id=creds.access_key,
aws_secret_access_key=creds.secret_key,
aws_session_token=creds.token
)
>>> ddb = session.client('dynamodb')
>>> response = ddb.get_item(
TableName='my-dynamodb-table',
Key={
'node_type': {
'S': 'primary_manager'
}
}
)
>>> print(response['Item']['ip']['S'])
'10.0.0.32
Also, when you are logged onto the EC2 instance, you can use curl to see the temporary credentials information:
$ iam_role_name=$(curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/)
$ curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/${iam_role_name}
{
"Code" : "Success",
"LastUpdated" : "2018-05-09T14:25:48Z",
"Type" : "AWS-HMAC",
"AccessKeyId" : "",
"SecretAccessKey" : "",
"Token" : "",
"Expiration" : "2018-05-09T20:46:55Z"
}
Another method is boto3 Session:
You can also use boto3.Session to achieve this:
>>> session = boto3.Session(region_name='eu-west-1')
>>> credentials = session.get_credentials()
>>> credentials = credentials.get_frozen_credentials()
>>> credentials.access_key
u'ABC...'
>>> credentials.secret_key
u'DEF...'
>>> credentials.token
u'ZXC...'
>>> access_key = credentials.access_key
>>> secret_key = credentials.secret_key
>>> ddb = session.client('dynamodb')
Thank You
Thanks for reading, feel free to check out my website, and subscribe to my newsletter or follow me at @ruanbekker on Twitter.
- Linktree: https://go.ruan.dev/links
- Patreon: https://go.ruan.dev/patreon