搭ListRolePolicies配使用 AWS SDK或 CLI - AWS 身分和存取權管理

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

ListRolePolicies配使用 AWS SDK或 CLI

下列程式碼範例會示範如何使用ListRolePolicies

.NET
AWS SDK for .NET
注意

還有更多關於 GitHub。尋找完整的範例,並瞭解如何在 AWS 代碼示例存儲庫

/// <summary> /// List IAM role policies. /// </summary> /// <param name="roleName">The IAM role for which to list IAM policies.</param> /// <returns>A list of IAM policy names.</returns> public async Task<List<string>> ListRolePoliciesAsync(string roleName) { var listRolePoliciesPaginator = _IAMService.Paginators.ListRolePolicies(new ListRolePoliciesRequest { RoleName = roleName }); var policyNames = new List<string>(); await foreach (var response in listRolePoliciesPaginator.Responses) { policyNames.AddRange(response.PolicyNames); } return policyNames; }
  • 有API關詳細資訊,請參閱 ListRolePoliciesAWS SDK for .NET API參考

CLI
AWS CLI

列出附加至IAM角色的策略

下列list-role-policies命令會列出指定IAM角色的權限原則名稱。

aws iam list-role-policies \ --role-name Test-Role

輸出:

{ "PolicyNames": [ "ExamplePolicy" ] }

若要查看連接至角色的信任政策,請使用 get-role 命令。若要查看許可政策的詳細資訊,請使用 get-role-policy 命令。

如需詳細資訊,請IAM參閱在 AWS IAM 使用者指南

Go
SDK對於轉到 V2
注意

還有更多關於 GitHub。尋找完整的範例,並瞭解如何在 AWS 代碼示例存儲庫

// RoleWrapper encapsulates AWS Identity and Access Management (IAM) role actions // used in the examples. // It contains an IAM service client that is used to perform role actions. type RoleWrapper struct { IamClient *iam.Client } // ListRolePolicies lists the inline policies for a role. func (wrapper RoleWrapper) ListRolePolicies(roleName string) ([]string, error) { var policies []string result, err := wrapper.IamClient.ListRolePolicies(context.TODO(), &iam.ListRolePoliciesInput{ RoleName: aws.String(roleName), }) if err != nil { log.Printf("Couldn't list policies for role %v. Here's why: %v\n", roleName, err) } else { policies = result.PolicyNames } return policies, err }
  • 有API關詳細資訊,請參閱 ListRolePoliciesAWS SDK for Go API參考

JavaScript
SDK對於 JavaScript (3)
注意

還有更多關於 GitHub。尋找完整的範例,並瞭解如何在 AWS 代碼示例存儲庫

列出政策。

import { ListRolePoliciesCommand, IAMClient } from "@aws-sdk/client-iam"; const client = new IAMClient({}); /** * A generator function that handles paginated results. * The AWS SDK for JavaScript (v3) provides {@link https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/index.html#paginators | paginator} functions to simplify this. * * @param {string} roleName */ export async function* listRolePolicies(roleName) { const command = new ListRolePoliciesCommand({ RoleName: roleName, MaxItems: 10, }); let response = await client.send(command); while (response.PolicyNames?.length) { for (const policyName of response.PolicyNames) { yield policyName; } if (response.IsTruncated) { response = await client.send( new ListRolePoliciesCommand({ RoleName: roleName, MaxItems: 10, Marker: response.Marker, }), ); } else { break; } } }
  • 有API關詳細資訊,請參閱 ListRolePoliciesAWS SDK for JavaScript API參考

PHP
適用於 PHP 的 SDK
注意

還有更多關於 GitHub。尋找完整的範例,並瞭解如何在 AWS 代碼示例存儲庫

$uuid = uniqid(); $service = new IAMService(); public function listRolePolicies($roleName, $marker = "", $maxItems = 0) { $listRolePoliciesArguments = ['RoleName' => $roleName]; if ($marker) { $listRolePoliciesArguments['Marker'] = $marker; } if ($maxItems) { $listRolePoliciesArguments['MaxItems'] = $maxItems; } return $this->customWaiter(function () use ($listRolePoliciesArguments) { return $this->iamClient->listRolePolicies($listRolePoliciesArguments); }); }
  • 有API關詳細資訊,請參閱 ListRolePoliciesAWS SDK for PHP API參考

PowerShell
適用的工具 PowerShell

範例 1:此範例會傳回IAM角色中內嵌之內嵌原則的名稱清單lamda_exec_role。若要查看內嵌政策的詳細資料,請使用命令Get-IAMRolePolicy

Get-IAMRolePolicyList -RoleName lambda_exec_role

輸出:

oneClick_lambda_exec_role_policy
  • 有API關詳細資訊,請參閱 ListRolePoliciesAWS Tools for PowerShell 指令程式參照

Python
SDK對於 Python(肉毒桿菌 3)
注意

還有更多關於 GitHub。尋找完整的範例,並瞭解如何在 AWS 代碼示例存儲庫

def list_policies(role_name): """ Lists inline policies for a role. :param role_name: The name of the role to query. """ try: role = iam.Role(role_name) for policy in role.policies.all(): logger.info("Got inline policy %s.", policy.name) except ClientError: logger.exception("Couldn't list inline policies for %s.", role_name) raise
  • 有API關詳細資訊,請參閱 ListRolePoliciesAWS SDK對於 Python(肉毒桿 3)API參考。

Ruby
SDK對於紅寶石
注意

還有更多關於 GitHub。尋找完整的範例,並瞭解如何在 AWS 代碼示例存儲庫

# Lists policy ARNs attached to a role # # @param role_name [String] The name of the role # @return [Array<String>] List of policy ARNs def list_attached_policy_arns(role_name) response = @iam_client.list_attached_role_policies(role_name: role_name) response.attached_policies.map(&:policy_arn) rescue Aws::IAM::Errors::ServiceError => e @logger.error("Error listing policies attached to role: #{e.message}") [] end
  • 有API關詳細資訊,請參閱 ListRolePoliciesAWS SDK for Ruby API參考

Rust
SDK對於銹
注意

還有更多關於 GitHub。尋找完整的範例,並瞭解如何在 AWS 代碼示例存儲庫

pub async fn list_role_policies( client: &iamClient, role_name: &str, marker: Option<String>, max_items: Option<i32>, ) -> Result<ListRolePoliciesOutput, SdkError<ListRolePoliciesError>> { let response = client .list_role_policies() .role_name(role_name) .set_marker(marker) .set_max_items(max_items) .send() .await?; Ok(response) }
  • 有API關詳細資訊,請參閱 ListRolePoliciesAWS SDKAPI供鐵鏽參考

Swift
SDK為斯威夫特
注意

還有更多關於 GitHub。尋找完整的範例,並瞭解如何在 AWS 代碼示例存儲庫

import AWSIAM public func listRolePolicies(role: String) async throws -> [String] { var policyList: [String] = [] var marker: String? = nil var isTruncated: Bool repeat { let input = ListRolePoliciesInput( marker: marker, roleName: role ) let output = try await client.listRolePolicies(input: input) guard let policies = output.policyNames else { return policyList } for policy in policies { policyList.append(policy) } marker = output.marker isTruncated = output.isTruncated } while isTruncated == true return policyList }
  • 有API關詳細資訊,請參閱 ListRolePoliciesAWS SDKAPI供斯威夫特參考

有關的完整列表 AWS SDK開發人員指南和代碼示例,請參閱使用此服務 AWS SDK。本主題也包含有關入門的資訊以及舊SDK版的詳細資訊。