Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Powershell to retrieve AWS account number

Does anyone know how to get AWS account number using AWS Powershell? Doesn't look like there's an API available for that.

like image 334
minisch Avatar asked Jun 05 '15 00:06

minisch


2 Answers

Nice and simple

(Get-STSCallerIdentity).Account

...or

(Get-STSCallerIdentity -Region [your_aws_region]).Account

like image 88
demonicdaron Avatar answered Oct 04 '22 04:10

demonicdaron


I was unable to comment on the other provided answer, so I'll have to offer my own solution as a slight modification.

I do believe the OwnerId on all groups will be the Account Id. However you may not have a "default" group. I recommend leaving out the -GroupNames "default". Also, I'm showing my example using a SAML token, as that is our case coming in with AD authorization.

$awsAccountNumber = (get-ec2securitygroup -ProfileName saml -Region us-west-2)[0].OwnerId

Hopefully that will be of some use.

like image 30
Evan Nadeau Avatar answered Oct 04 '22 03:10

Evan Nadeau