如何使用 boto3 列出附加到角色的 AWS 托管策略

问题描述 投票:0回答:2

我正在尝试使用 boto3 列出附加到角色的策略。我正在使用 list_role_policies 或 get_role_policy 来实现此目的。但它仅列出附加到该角色的内联策略,而不列出附加到该角色的 AWS 托管策略。我们有什么方法可以使用 boto3 列出附加到角色的所有策略(内联以及 AWS 托管)。

下面是使用 list_role_policies 的代码片段

import boto3
from botocore.exceptions import ClientError

ec2=boto3.client('ec2',region_name='ca-central-1')
iam=boto3.client('iam')


response = iam.list_role_policies(RoleName='rolename')
print(response)

谢谢

python-3.x amazon-web-services boto3 amazon-iam
2个回答
1
投票

我认为我们没有这样的东西。需要同时使用“list_role_policies”(“列出内联策略”)和“list_attached_role_policies”(列出托管策略)


0
投票

今天我也必须做同样的事情。这里有一些代码可以让它变得简单:

import boto3
client=boto3.client('iam')

def list_attached_policies(role_name):
    params = {
        "RoleName": role_name 
    }      
    while True:
        policies = client.list_attached_role_policies(**params)
        # print(policies)
        for p in policies["AttachedPolicies"]:
            print(json.dumps(p, indent=2, default=json_datetime_serializer))
        if not policies["IsTruncated"]:
            break
        else:
            params["Marker"] = policies["Marker"]
© www.soinside.com 2019 - 2024. All rights reserved.