Here is the use case. I have an EC2 instance running the ServiceNow mid-server agent. EC2 instance has IAM_Role attached called "TestIAMRole" and assume role policy is attached to the role. I use this EC2 instance and ServiceNow mid-server agent to invoke VPC endpoints using AWS boto3 endpoints. Python script will generate an authentication token using boto3 SDK and invoke the endpoint.
Python Example:
import boto3
client = boto3.client('sts')
repsonse = client.assume_role(RoleArn='<IAM ROle ARN>',RoleSessionName='AssumeROle01')
credentials=repsonse['Credentials']
Then using Python request, we call VPC endpoints and it works fine.
Since We are running an agent on EC2 instance, we thought of another approach to use EC2 instance metadata using local https endpoint using curl, it gives back access_key, secret_key, and session_token
http://169.254.169.254/latest/meta-data/iam/security-credentials/TestIAMRole
So, we are looking for expert opinion on what is the most secure / recommended from the above two options.
Also, a few questions on optimization
- If we use assume role STS method to retrieve credentials then is it recommended to store those in cache/app till expiry date-time so we can reuse them or should we get new sts credentials every time we make a call to other AWS services? Just trying to save additional STS call each time we invoke AWS service.
- if storing in cache or app is the option, then we can store the key in ServiceNow securely (encrypted format) and can update regularly before expiry date-time.
Please advise if this is the right way.