简体   繁体   English

如何从 bash 脚本中获取 aws 账户 ID

[英]How to obtain aws account ID from bash script

So, I would like to obtain my aws account id through a bash script.因此,我想通过 bash 脚本获取我的 aws 帐户 ID。 I know that when I run aws sts get-caller-identity , it returns a json (similar to)我知道当我运行aws sts get-caller-identity时,它返回一个 json (类似于)

{
    "UserId": "JALDKKEABANMFSL:john.doe",
    "Account": "10299301874829",
    "Arn": "arn:aws:sts::10299301874829:assumed-role/admin/john.doe"
}

is there a way for my to either just get the "Account" value from the returned json?有没有办法让我从返回的 json 中获取“帐户”值? I've tried saving the response into a.json file and using jq, but that has been throwing errors.我尝试将响应保存到 .json 文件并使用 jq,但这一直在抛出错误。 Any ideas?有任何想法吗? Thanks!!谢谢!!

The AWS CLI uses JMESPath to access elements of the output: AWS CLI 使用JMESPath访问 output 的元素:

aws sts get-caller-identity --query 'Account' --output text

This will extract the Account value from the returned dictionary and output it in plain text.这将从返回的字典中提取Account值,并以纯文本形式提取 output 。

JMESPath is extremely powerful. JMESPath 非常强大。 I highly recommend looking at the JMESPath Tutorial .我强烈建议您查看JMESPath 教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM