1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

List IAM Roles sorted by rarely-used in one-liner

Last updated at Posted at 2021-01-21

「残滓化している IAM Role の掃除をしたいので、候補を抽出したい」
こういった要望が弊社社内で聞こえてまいりましたので作ってみました

aws iam list-roles --query 'Roles[].{RoleName:RoleName}' --output text | xargs -I{} aws iam get-role --role-name {} --query 'Role.{RoleName:RoleName,RoleLastUsedDate:RoleLastUsed.LastUsedDate}' --output text | sort -k1,2

結果

2020-02-12T02:16:00+00:00       AWSServiceRoleForOrganizations
2020-07-06T08:27:16+00:00       Mycompany-LambdaIAMRole-XXXXXXXXXXXX
2020-07-06T08:29:55+00:00       Mycompany-LambdaIAMRole-XXXXXXXXXXXXX
...
2021-01-21T02:50:46+00:00       aws-controltower-CloudWatchLogsRole

RoleLastUsed.LastUsedDate で並べています
AWS CloudShell でも動作OKです:smiley:
(※Roleの数が90弱の環境で確認済み)

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?