TeaCupApp
TeaCupApp

Reputation: 11452

How to get user-id or AWS account id via AWS PHP SDK

Is there anyway to get UserId or a.k.a. AWS Account ID?

I have checked the documentation but could not locate (...or probably it doesn't exists...) a method call which returns the UserId.

Upvotes: 2

Views: 5636

Answers (3)

Steve N
Steve N

Reputation: 1391

I haven't used the PHP SDK, but AWS have added a GetCallerIdentity API call in STS which can give you both the User ID and the Account ID. It's available here:

https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sts-2011-06-15.html#getcalleridentity

$result = $client->getCallerIdentity([/* ... */]);

Returns:

[
  'Account' => '<string>',
  'Arn' => '<string>',
  'UserId' => '<string>',
]

Upvotes: 5

Paul Rysevets
Paul Rysevets

Reputation: 39

You CAN, actually, extract account-id from User's ARN. Without creating any "additional" resources, as IAM User is already an Resource.

GetUser call on IAM Client. Then parse the ARN string by pattern.

Upvotes: 1

Ryan Parman
Ryan Parman

Reputation: 6945

Sort of. There's no way to get it without creating a resource. Many resources have an ARN associated with it. You can extract the Account ID from the ARN, but you'd have to create a resource first.

Upvotes: 4

Related Questions