Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding my AWS account ID using JavaScript

How do I find my AWS account ID using JavaScript/NodeJS?

Should work when explicitly providing keys for a root-account or IAM user. Should also work when invoked inside of an ec2-instance which is configured with instance-profile (no keys).

like image 720
Michael Yakobi Avatar asked Feb 22 '16 20:02

Michael Yakobi


People also ask

How do I Find my AWS account ID?

You can find the account ID for your AWS account using the following methods. Finding Your Account ID using the console. In the navigation bar, choose Support, and then Support Center. Your currently signed-in 12-digit account number (ID) appears in the Support Center navigation pane. Finding Your Account ID using the AWS CLI

How to sign in to an AWS account as an IAM user?

To sign in to an AWS account as an IAM user, you must have an account alias or an account ID for the AWS account. If you are signed in to the AWS Management Console or have configured the AWS CLI or an AWS SDK with your account credentials, you can find the account alias or account ID for the AWS account.

How do I sign in to the AWS Management Console?

If you're an AWS Identity and Access Management (IAM) user, you can sign in to the AWS Management Console using either the account ID or account alias. An alpha-numeric identifier, such as 79a59df900b949e55d96a1e698fbacedfd6e09d98eacf8f8d5218e7cd47ef2be , that is an obfuscated form of the AWS account ID.

How to recover AWS access key ID and secret access key?

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.


1 Answers

The best way is via "Security Token Service":

var AWS = require('aws-sdk');
// Load credentials and set region from JSON file
AWS.config.loadFromPath('./config.json');

var sts = new AWS.STS();
sts.getCallerIdentity({}, function(err, data) {
   if (err) {
      console.log("Error", err);
   } else {
      console.log(JSON.stringify(data.Account));
   }
});

This would print the account ID with a simple call.

like image 147
Facundo Victor Avatar answered Sep 29 '22 01:09

Facundo Victor