I can connect to an ec2 instance in 2 ways:
- via SSH
- using the accessKey, secretKey, token and region via AWS CLI on my personal machine
When I run aws sts get-caller-identity (using the accessKey, secretKey, token FROM the ec2 instance via SSH), I get a slightly different userId, and assumed-role within the ARN. Attached below is an example:
aws sts get-caller-identity via SSH
{
"UserId": "AAAAAAAAAAAAAAAAAAAAA:i-0b999999999999999",
"Account": "739214605182",
"Arn": "arn:aws:sts::739214605182:assumed-role/Test-Role-Ec2/i-0b999999999999999"
}
aws sts get-caller-identity via AWS CLI
{
"UserId": "BBBBBBBBBBBB:aws:ec2-instance:i-0b999999999999999",
"Account": "739214605182",
"Arn": "arn:aws:sts::739214605182:assumed-role/aws:ec2-instance/i-0b999999999999999"
}
It seems like the AWS CLI is using my account credentials, while the ec2 instance is using the IAM Role I designated – which is Test-Role-Ec2
Is it possible for AWS CLI to have the Test-Role-Ec2 IAM Role set?
I thought that if I imported the access key secret key and token, that I would assume the same role (Test-Role-EC2), despite being on AWS CLI.
2
Answers
When you run the AWS CLI tool on your local machine, you are not connecting to the EC2 instance. You are connecting directly to the public AWS API from your local computer. The EC2 instance is not involved in any way with the AWS API calls you make from your local computer.
No, that is an incorrect assumption. Your assumption that the EC2 instance is in any way involved in this process is incorrect.
Your issue may be related to how you’re querying the IMDSv1 within the EC2 instance. The second ARN you’re getting (from the AWS CLI) looks to be the role for a generic EC2 instance. I can get a similar value by querying the following endpoint, importing the keys outside the instance, and running get-caller-identity:
Instead start by querying the IAM info:
That Profile ARN should give you the role name you’re looking for. Use that to query the role’s access keys:
(As a side note: you may want to redact your account ID. They’re not techincally sensitive, but they do open you up to some enumeration and console login attacks)