Find Account ID And Username From AWS Access Key ID And Secret Access Key

Although I work for AWS, I don't work for the product teams for the technologies mentioned in this post. However, I make every effort to ensure the accuracy of the contents; my position is not official.

If you have many AWS accounts, sometimes you may forget the account id and username for a pair of access key IDs and secret access keys. Fortunately, you can recover it. After all, you can authenticate with AWS with just access key ID and secret access key. So, there must be a way to link them to your AWS Accounts. 

The following is the C# code that can recover the account id and username,
  1. //Need to reference AWSSDK.SecurityToken nuget package  
  2. using Amazon.Runtime;  
  3. using Amazon.SecurityToken;  
  4. using Amazon.SecurityToken.Model;  
  5. using System;  
  6. using System.Threading.Tasks;  
  7.   
  8. namespace CrendentialsTest  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             MainAsync().Wait();  
  15.             Console.ReadKey();  
  16.         }  
  17.   
  18.         static async Task MainAsync()  
  19.         {  
  20.             BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials("MyAccessKeyID""MySecretAccessKey");  
  21.             AmazonSecurityTokenServiceClient stsClient = new AmazonSecurityTokenServiceClient(basicAWSCredentials);  
  22.             var getCallerIdentityResponse = await stsClient.GetCallerIdentityAsync(new GetCallerIdentityRequest());  
  23.             Console.WriteLine(getCallerIdentityResponse.Arn);  
  24.         }  
  25.     }  
  26. }  
Happy computing!
Ebook Download
View all
Learn
View all