There are more AWS SDK examples available in the AWS Doc SDK Examples
List IAM roles using an AWS SDK
The following code examples show how to list IAM roles.
- .NET
-
- AWS SDK for .NET
-
Note There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. using System; using Amazon.IdentityManagement; using Amazon.IdentityManagement.Model; var client = new AmazonIdentityManagementServiceClient(); // Without the MaxItems value, the ListRolesAsync method will // return information for up to 100 roles. If there are more // than the MaxItems value or more than 100 roles, the response // value IsTruncated will be true. var request = new ListRolesRequest { MaxItems = 10, }; var response = new ListRolesResponse(); do { response = await client.ListRolesAsync(request); response.Roles.ForEach(role => { Console.WriteLine($"{role.RoleName} - ARN {role.Arn}"); }); // As long as response.IsTruncated is true, set request.Marker equal // to response.Marker and call ListRolesAsync again. if (response.IsTruncated) { request.Marker = response.Marker; } } while (response.IsTruncated);
-
For API details, see ListRoles in AWS SDK for .NET API Reference.
-
- Go
-
- SDK for Go V2
-
Note There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. // 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 } // ListRoles gets up to maxRoles roles. func (wrapper RoleWrapper) ListRoles(maxRoles int32) ([]types.Role, error) { var roles []types.Role result, err := wrapper.IamClient.ListRoles(context.TODO(), &iam.ListRolesInput{MaxItems: aws.Int32(maxRoles)}, ) if err != nil { log.Printf("Couldn't list roles. Here's why: %v\n", err) } else { roles = result.Roles } return roles, err }
-
For API details, see ListRoles
in AWS SDK for Go API Reference.
-
- JavaScript
-
- SDK for JavaScript V3
-
Note There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. Create the client.
import { IAMClient } from "@aws-sdk/client-iam"; // Set the AWS Region. const REGION = "REGION"; // For example, "us-east-1". // Create an IAM service client object. const iamClient = new IAMClient({ region: REGION }); export { iamClient };
List the roles.
// Import required AWS SDK clients and commands for Node.js. import { iamClient } from "./libs/iamClient.js"; import { ListRolesCommand } from "@aws-sdk/client-iam"; // Set the parameters. const params = { Marker: 'MARKER', // This is a string value. MaxItems: 'MAX_ITEMS' // This is a number value. }; const run = async () => { try { const results = await iamClient.send(new ListRolesCommand(params)); console.log("Success", results); return results; } catch (err) { console.log("Error", err); } }; run();
-
For API details, see ListRoles in AWS SDK for JavaScript API Reference.
-
- PHP
-
- SDK for PHP
-
Note There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. $uuid = uniqid(); $service = new IamService(); /** * @param string $pathPrefix * @param string $marker * @param int $maxItems * @return Result * $roles = $service->listRoles(); */ public function listRoles($pathPrefix = "", $marker = "", $maxItems = 0) { $listRolesArguments = []; if ($pathPrefix) { $listRolesArguments["PathPrefix"] = $pathPrefix; } if ($marker) { $listRolesArguments["Marker"] = $marker; } if ($maxItems) { $listRolesArguments["MaxItems"] = $maxItems; } return $this->iamClient->listRoles($listRolesArguments); }
-
For API details, see ListRoles in AWS SDK for PHP API Reference.
-
- Python
-
- SDK for Python (Boto3)
-
Note There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. def list_roles(count): """ Lists the specified number of roles for the account. :param count: The number of roles to list. """ try: roles = list(iam.roles.limit(count=count)) for role in roles: logger.info("Role: %s", role.name) except ClientError: logger.exception("Couldn't list roles for the account.") raise else: return roles
-
For API details, see ListRoles in AWS SDK for Python (Boto3) API Reference.
-
- Ruby
-
- SDK for Ruby
-
Note There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. # Lists up to a specified number of roles for the account. # # @param count [Integer] The maximum number of roles to list. # @return [Array] The names of the listed roles. def list_roles(count) role_names = [] @iam_resource.roles.limit(count).each_with_index do |role, index| puts("\t#{index + 1}: #{role.name}") role_names.append(role.name) end rescue Aws::Errors::ServiceError => e puts("Couldn't list roles for the account. Here's why:") puts("\t#{e.code}: #{e.message}") raise else role_names end
-
For API details, see ListRoles in AWS SDK for Ruby API Reference.
-
- Rust
-
- SDK for Rust
-
Note This documentation is for an SDK in preview release. The SDK is subject to change and should not be used in production.
Note There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. pub async fn list_roles( client: &iamClient, path_prefix: Option<String>, marker: Option<String>, max_items: Option<i32>, ) -> Result<ListRolesOutput, SdkError<ListRolesError>> { let response = client .list_roles() .set_path_prefix(path_prefix) .set_marker(marker) .set_max_items(max_items) .send() .await?; Ok(response) }
-
For API details, see ListRoles
in AWS SDK for Rust API reference.
-
- Swift
-
- SDK for Swift
-
Note This is prerelease documentation for an SDK in preview release. It is subject to change.
Note There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. public func listRoles() async throws -> [String] { var roleList: [String] = [] var marker: String? = nil var isTruncated: Bool repeat { let input = ListRolesInput(marker: marker) let output = try await client.listRoles(input: input) guard let roles = output.roles else { return roleList } for role in roles { if let name = role.roleName { roleList.append(name) } } marker = output.marker isTruncated = output.isTruncated } while isTruncated == true return roleList }
-
For API details, see ListRoles
in AWS SDK for Swift API reference.
-