元ネタは、結構前に流れてきたこちらのツイート。
1/ Hey all! If you were watching #containersfromthecouch earlier today, I mentioned an aws cli command that you can run to get insight into what #AWS resources were accessed from a particular IAM role/user/group/policy.
— Adam Keller (@realadamjkeller) August 24, 2020
参考にしていたけどたまにしかやらずに忘れるのでメモとして残しておく。
やり方
AWS IAM のGetServiceLastAccessedDetail
API を使う。
AWS CLI だと、まず、以下のような感じで引数に確認したい IAM の ARN を指定して実行する。以下では Role としている。
$ aws iam generate-service-last-accessed-details --arn arn:aws:iam::123456789012:role/foo
Job id
が返ってくるので、この値を利用して再度 GetServiceLastAccessedDetail
を実行する。今度はJob id
を引数にして実行する。
$ aws iam get-service-last-accessed-details --job-id ********-xxxx-****-xxxx-************
これで対象 IAM が過去にアクセスした AWS リソースの履歴一覧が表示される。
元ツイートだとjq
コマンドを使っていい感じに表示する方法も紹介されてた。
$ aws iam get-service-last-accessed-details --job-id ********-xxxx-****-xxxx-************ | jq '.ServicesLastAccessed[] | select( .TotalAuthenticatedEntities > 0) | .ServiceName'
4/ Next, I will use that job id to gather the details for review. Notice that I added some jq goodness in there to parse the results to only return the services that my user has made calls to. Also, one will likely have to use pagination here to get all of the data. pic.twitter.com/zEmSmnoY4k
— Adam Keller (@realadamjkeller) August 24, 2020
なんかワンライナーとかで良い感じにしたいけど何も考えていない。