【AWS CLI】IAM関連の情報取得編

記事タイトルとURLをコピーする

こんにちは。AWS CLIが好きな福島です。

前回は、SSO関連のリソース情報を取得するコマンドをご紹介いたしましたが、 今回は、IAM関連のリソース情報を取得するコマンドをご紹介いたします。

SSO関連のリソース情報を取得するコマンドが気になる方は、ぜひこちらをご覧ください。

blog.serverworks.co.jp

f:id:swx-fukushima:20201022005339p:plain

実行環境

今回、コマンドを実行した環境は、以下の通りとなります。
(本記事でご紹介しているコマンドの中には、Linuxのコマンドを利用している箇所があります。)

# uname -a
Linux LAPTOP-CNM26HN6 4.4.0-18362-Microsoft #1049-Microsoft Thu Aug 14 12:01:00 PST 2020 x86_64 x86_64 x86_64 GNU/Linux
#

IAMユーザーの一覧

コマンド ヘッダー無し

aws iam list-users --query "Users[].[UserName,Arn,CreateDate,PasswordLastUsed]" --output text
  • 実行結果
administrator   arn:aws:iam::xxxxxxxxxxxx:user/administrator    2019-10-07T03:06:04+00:00       2020-10-31T01:42:11+00:00
iam-check-user  arn:aws:iam::xxxxxxxxxxxx:user/iam-check-user   2020-05-25T02:51:43+00:00       2020-06-22T10:07:48+00:00
kms-test        arn:aws:iam::xxxxxxxxxxxx:user/kms-test 2020-09-30T14:53:46+00:00       None

コマンド ヘッダー有り

echo "UserName Arn CreateDate PasswordLastUsed" > /tmp/awscli.tmp;\
aws iam list-users --query "Users[].[UserName,Arn,CreateDate,PasswordLastUsed]" --output text | sort >> /tmp/awscli.tmp ;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp
  • 実行結果
UserName        Arn                                            CreateDate                 PasswordLastUsed
administrator   arn:aws:iam::xxxxxxxxxxxx:user/administrator   2019-10-07T03:06:04+00:00  2020-10-31T01:42:11+00:00
iam-check-user  arn:aws:iam::xxxxxxxxxxxx:user/iam-check-user  2020-05-25T02:51:43+00:00  2020-06-22T10:07:48+00:00
kms-test        arn:aws:iam::xxxxxxxxxxxx:user/kms-test        2020-09-30T14:53:46+00:00  None

IAMアクセスキー一覧

コマンド ヘッダー無し

aws iam list-access-keys --query "AccessKeyMetadata[].[UserName,AccessKeyId,Status,CreateDate]" --output text
  • 実行結果
administrator   AKIXXXXXXXXXXXXXXXXX    Inactive        2019-12-17T18:03:19+00:00
administrator   AKIXXXXXXXXXXXXXXXXX    Active  2020-05-04T22:55:08+00:00

コマンド ヘッダー有り

echo "UserName AccessKeyId Status CreateDate" > /tmp/awscli.tmp;\
aws iam list-access-keys --query "AccessKeyMetadata[].[UserName,AccessKeyId,Status,CreateDate]" --output text | sort >> /tmp/awscli.tmp ;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp
  • 実行結果
UserName       AccessKeyId           Status    CreateDate
administrator  AKIXXXXXXXXXXXXXXXXX  Active    2020-05-04T22:55:08+00:00
administrator  AKIXXXXXXXXXXXXXXXXX  Inactive  2019-12-17T18:03:19+00:00

アクセスキーの利用履歴一覧

コマンド ヘッダー有り

echo "AccessKey LastUsedDate  ServiceName Region" > /tmp/awscli.tmp;\
for access_key in $(aws iam list-access-keys --query "AccessKeyMetadata[].AccessKeyId" --output text); 
do
   IAMTEMP=$(aws iam get-access-key-last-used  --access-key-id $access_key --query "AccessKeyLastUsed.[LastUsedDate,ServiceName,Region]" --output text);
   echo "$access_key $IAMTEMP" >> /tmp/awscli.tmp
done;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp
  • 実行結果
AccessKey             LastUsedDate               ServiceName  Region
AKIXXXXXXXXXXXXXXXXX  2019-12-17T18:15:00+00:00  ssm          ap-northeast-1
AKIXXXXXXXXXXXXXXXXX  2020-10-31T03:43:00+00:00  iam          us-east-1

MFAデバイス一覧

物理MFAデバイス一覧

コマンド ヘッダー無し

aws iam list-mfa-devices --query "MFADevices[].[UserName,SerialNumber,EnableDate]" --output text
  • 実行結果
administrator   arn:aws:iam::xxxxxxxxxxxx:mfa/administrator     2020-04-05T00:46:06+00:00

コマンド ヘッダー有り

echo "UserName SerialNumber EnableDate" > /tmp/awscli.tmp;\
aws iam list-mfa-devices --query "MFADevices[].[UserName,SerialNumber,EnableDate]" --output text | sort >> /tmp/awscli.tmp ;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp
  • 実行結果
UserName       SerialNumber                                 EnableDate
administrator  arn:aws:iam::xxxxxxxxxxxx:mfa/administrator  2020-04-05T00:46:06+00:00

仮想MFAデバイス一覧

コマンド ヘッダー無し

aws iam list-virtual-mfa-devices --query "VirtualMFADevices[].[SerialNumber,User.Arn,User.PasswordLastUsed,EnableDate]" --output text
  • 実行結果
arn:aws:iam::xxxxxxxxxxxx:mfa/tech-kadai-iam    None    None    None
arn:aws:iam::xxxxxxxxxxxx:mfa/root-account-mfa-device   arn:aws:iam::xxxxxxxxxxxx:root  2020-06-12T06:21:52+00:00       2020-06-12T06:23:06+00:00
arn:aws:iam::xxxxxxxxxxxx:mfa/administrator     arn:aws:iam::xxxxxxxxxxxx:user/administrator    2020-10-31T01:42:11+00:00       2020-04-05T00:46:06+00:00

コマンド ヘッダー有り

echo "SerialNumber Arn PasswordLastUsed EnableDate" > /tmp/awscli.tmp;\
aws iam list-virtual-mfa-devices --query "VirtualMFADevices[].[SerialNumber,User.Arn,User.PasswordLastUsed,EnableDate]" --output text | sort >> /tmp/awscli.tmp ;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp
  • 実行結果
SerialNumber                                           Arn                                           PasswordLastUsed           EnableDate
arn:aws:iam::xxxxxxxxxxxx:mfa/administrator            arn:aws:iam::xxxxxxxxxxxx:user/administrator  2020-10-31T01:42:11+00:00  2020-04-05T00:46:06+00:00
arn:aws:iam::xxxxxxxxxxxx:mfa/root-account-mfa-device  arn:aws:iam::xxxxxxxxxxxx:root                2020-06-12T06:21:52+00:00  2020-06-12T06:23:06+00:00
arn:aws:iam::xxxxxxxxxxxx:mfa/tech-kadai-iam           None                                          None                       None

IAMグループの一覧

コマンド ヘッダー無し

aws iam list-groups --query "Groups[].[GroupName,GroupId,Arn,CreateDate]" --output text
  • 実行結果
sw-readonly     AKIXXXXXXXXXXXXXXXXX  arn:aws:iam::xxxxxxxxxxxx:group/sw-readonly     2019-10-07T08:18:44+00:00
test    AKIXXXXXXXXXXXXXXXXX  arn:aws:iam::xxxxxxxxxxxx:group/test    2020-06-15T01:34:55+00:00
test02  AKIXXXXXXXXXXXXXXXXX  arn:aws:iam::xxxxxxxxxxxx:group/test02  2020-06-15T01:35:56+00:00

コマンド ヘッダー有り

echo "GroupName GroupId Arn CreateDate" > /tmp/awscli.tmp;\
aws iam list-groups --query "Groups[].[GroupName,GroupId,Arn,CreateDate]" --output text | sort >> /tmp/awscli.tmp ;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp
  • 実行結果
GroupName    GroupId                Arn                                          CreateDate
sw-readonly  AKIXXXXXXXXXXXXXXXXX arn:aws:iam::xxxxxxxxxxxx:group/sw-readonly  2019-10-07T08:18:44+00:00
test         AKIXXXXXXXXXXXXXXXXX arn:aws:iam::xxxxxxxxxxxx:group/test         2020-06-15T01:34:55+00:00
test02       AKIXXXXXXXXXXXXXXXXX arn:aws:iam::xxxxxxxxxxxx:group/test02       2020-06-15T01:35:56+00:00

コマンド ヘッダー有り(ユーザーが所属しているグループを表示)

echo "UserName Groups" > /tmp/awscli.tmp
aws iam list-users --query "Users[].[UserName]" --output text | sort | while read line;\
do
   echo ${line} > /tmp/awscli-group.tmp;\
   aws iam list-groups-for-user --user-name ${line} --query "Groups[].[GroupName]" --output text >> /tmp/awscli-group.tmp;\
   cat /tmp/awscli-group.tmp | tr "\n" " " | sed 's/$/\n/g' >> /tmp/awscli.tmp;\
done
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp /tmp/awscli-group.tmp
  • 実行結果
UserName        Groups
administrator   sw-readonly  test02
iam-check-user  sw-readonly  test02
kms-test        sw-readonly  test02
test            sw-readonly  test

IAMロールの一覧

コマンド ヘッダー無し

aws iam list-roles --query "Roles[].[RoleName,Arn,CreateDate,MaxSessionDuration]" --output text
  • 実行結果
acm-role        arn:aws:iam::xxxxxxxxxxxx:role/acm-role 2020-10-29T16:33:47+00:00       3600
alias-test-role-b9k50d3g        arn:aws:iam::xxxxxxxxxxxx:role/service-role/alias-test-role-b9k50d3g    2020-06-22T11:53:41+00:00       3600
Amazon-GlueServiceRoleForSSM    arn:aws:iam::xxxxxxxxxxxx:role/service-role/Amazon-GlueServiceRoleForSSM        2020-03-19T23:45:37+00:00       3600

コマンド ヘッダー有り

echo "RoleName Arn CreateDate MaxSessionDuration" > /tmp/awscli.tmp;\
aws iam list-roles --query "Roles[].[RoleName,Arn,CreateDate,MaxSessionDuration]" --output text | sort >> /tmp/awscli.tmp ;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp
  • 実行結果
RoleName                                                          Arn                                                                                                                            CreateDate                 MaxSessionDuration
AWSCodePipelineServiceRole-ap-northeast-1-MyFirstPipeline         arn:aws:iam::xxxxxxxxxxxx:role/service-role/AWSCodePipelineServiceRole-ap-northeast-1-MyFirstPipeline                          2020-03-27T00:33:10+00:00  3600
AWSCodePipelineServiceRole-ap-northeast-1-fk-user-ecsdemo-front   arn:aws:iam::xxxxxxxxxxxx:role/service-role/AWSCodePipelineServiceRole-ap-northeast-1-fk-user-ecsdemo-front                    2020-03-28T12:01:04+00:00  3600
AWSCodePipelineServiceRole-ap-northeast-1-kazuya-ecsdemo-fronte   arn:aws:iam::xxxxxxxxxxxx:role/service-role/AWSCodePipelineServiceRole-ap-northeast-1-kazuya-ecsdemo-fronte                    2020-05-06T15:36:08+00:00  3600

IAMロールの信頼関係

コマンド ヘッダー無し

aws iam list-roles --query "Roles[].[RoleName,AssumeRolePolicyDocument][]"
  • 実行結果
[
    "acm-role",
    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Principal": {
                    "Service": "ec2.amazonaws.com"
                },
                "Action": "sts:AssumeRole"
            }
        ]
    },
    "alias-test-role-b9k50d3g",
    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Principal": {
                    "Service": "lambda.amazonaws.com"
                },
                "Action": "sts:AssumeRole"
            }
        ]
    },

マネージド・カスタムポリシー一覧

基本情報

コマンド ヘッダー無し

aws iam list-policies --query "Policies[].[PolicyName,DefaultVersionId,AttachmentCount,UpdateDate]"  --output text
  • 実行結果
aaaaaaa v2      1       2020-10-22T08:30:37+00:00
AccessS3BucketForAppTest        v1      0       2020-03-03T10:20:44+00:00
AllowStorageGatewayAssumeBucketAccessRole1f359411-1df9-4aea-bd7f-f2fa6da1c06a   v1      1       2019-10-27T11:07:01+00:00

コマンド ヘッダー有り

echo "PolicyName DefaultVersionId AttachmentCount UpdateDate" > /tmp/awscli.tmp;\
aws iam list-policies --query "Policies[].[PolicyName,DefaultVersionId,AttachmentCount,UpdateDate]"  --output text | sort >> /tmp/awscli.tmp ;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp
  • 実行結果
PolicyName                                                                      DefaultVersionId  AttachmentCount  UpdateDate
APIGatewayServiceRolePolicy                                                     v8                0                2020-02-25T20:24:49+00:00
AWS-Chatbot-NotificationsOnly-Policy-c938da66-7801-4fcf-ab2b-2411332f2579       v1                1                2020-04-27T07:22:33+00:00
AWSAccountActivityAccess                                                        v1                0                2015-02-06T18:41:18+00:00

詳細情報

コマンド ヘッダー有り

aws iam list-policies --query "Policies[].[Arn,DefaultVersionId]" --output text | while read line;
do    
    ARN=$(echo ${line} | awk '{print $1}');
    VERSION=$(echo ${line} | awk '{print $2}');
    echo "PolicyName:${ARN#*/}";
    aws iam get-policy-version --policy-arn $ARN --version-id $VERSION --query "PolicyVersion.Document.Statement";
done
  • 実行結果
PolicyName:aaaaaaa
[
    {
        "Sid": "VisualEditor0",
        "Effect": "Allow",
        "Action": "ec2:*",
        "Resource": "*"
    }
]
PolicyName:AccessS3BucketForAppTest
[
    {
        "Action": [
            "s3:*"
        ],
        "Resource": [
            "arn:aws:s3:::xxxxxxxxxxxx",
            "arn:aws:s3:::xxxxxxxxxxxx/*"
        ],
        "Effect": "Allow"
    }
]

インラインポリシー一覧

全ユーザーのインラインポリシー詳細

aws iam list-users --query "Users[].[UserName]" --output text | while read user;
do
   echo "UserName:$user"
   for policy in $(aws iam list-user-policies --user-name $user --query "PolicyNames" --output text); 
   do
      echo "InlinePolicyName:$policy"
      aws iam get-user-policy --user-name $user --policy-name $policy --query "PolicyDocument"
   done
done
  • 実行結果
UserName:administrator
InlinePolicyName:me-switch-role
{
    "Version": "2012-10-17",
    "Statement": {
        "Effect": "Allow",
        "Action": "sts:AssumeRole",
        "Resource": "arn:aws:iam::xxxxxxxxxxxx:role/policy-check-role"
    }
}
UserName:iam-check-user
InlinePolicyName:ssmdescribe
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "s3:ListBucket",
            "Resource": "arn:aws:s3:::fk-test-xxx"
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:GetObject",
                "s3:AbortMultipartUpload",
                "s3:DeleteObject",
                "s3:GetObjectVersion",
                "s3:ListMultipartUploadParts"
            ],
            "Resource": "arn:aws:s3:::fk-test-xxxxxxx/*"
        }
    ]
}
UserName:kms-test
UserName:test

全グループのインラインポリシー詳細

aws iam list-groups --query "Groups[].[GroupName]" --output text | while read group;
do
   echo "GroupName:$group"
   for policy in $(aws iam list-group-policies --group-name $group --query "PolicyNames" --output text); 
   do
      echo "InlinePolicyName:$policy"
      aws iam get-group-policy --group-name $group --policy-name $policy --query "PolicyDocument"
   done
done
  • 実行結果
GroupName:sw-readonly
InlinePolicyName:test
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "appflow:*",
            "Resource": "*"
        }
    ]
}
GroupName:test
GroupName:test02

全IAMロールのインラインポリシー詳細

aws iam list-roles --query "Roles[].[RoleName]" --output text | while read role;
do
   echo "RoleName:$role"
   for policy in $(aws iam list-role-policies --role-name $role --query "PolicyNames" --output text); 
   do
      echo "InlinePolicyName:$policy"
      aws iam get-role-policy --role-nam $role --policy-name $policy --query "PolicyDocument"
   done
done
  • 実行結果
RoleName:acm-role
InlinePolicyName:acm-role-policy
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObject"
            ],
            "Resource": [
                "arn:aws:s3:::aws-ec2-enclave-xxxxxxxx-ap-northeast-1-prod/*"
            ]
        },
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "kms:Decrypt"
            ],
            "Resource": "arn:aws:kms:ap-northeast-1:*:key/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        }
    ]
}
InlinePolicyName:ass
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "access-analyzer:*",
            "Resource": "*"
        }
    ]
}

おわりに

今回は、IAM関連のリソース情報を取得するコマンドをご紹介いたしました。
次回もAWS CLI関連のコマンドをご紹介したいと思います。

福島 和弥 (記事一覧)

SRE2課

2019/10 入社

AWS CLIが好きです。