Home > front end >  How to get AWS account ID from curl in powershell
How to get AWS account ID from curl in powershell

Time:07-29

Does anyone know how to get the AWS account id in Powershell? I would like to do the this from the curl command, and have this running well under Linux from here:

https://stackoverflow.com/questions/51597492/how-to-get-aws-account-number-id-based-on-ec2-instance-which-is-hosted-in-amazo#:~:text=You can obtain the account,/instance-identity/document.&text=This gives me "Unable to,running "aws configure"."

Specifically this:

curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | sed -nE 's/.*"accountId"\s*:\s*"(.*)".*/\1/p'

And I can use the curl command in windows get get the same output but without parsing like this

curl http://169.254.169.254/latest/dynamic/instance-identity/document

but I have no idea how to parse and get just the accountId number, or the instanceId if I need that.

I have tried the Select statement, but get no meaningful results.

CodePudding user response:

For the account ID, you can use:

$account_id = ((curl http://169.254.169.254/latest/dynamic/instance-identity/document).Content | ConvertFrom-Json).accountId

For the instance ID, you can use:

$instance_id = (curl http://169.254.169.254/latest/meta-data/instance-id).Content
  • Related