List IAM groups using an AWS SDK - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

List IAM groups using an AWS SDK

The following code examples show how to list IAM groups.

.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(); var request = new ListGroupsRequest { MaxItems = 10, }; var response = await client.ListGroupsAsync(request); do { response.Groups.ForEach(group => { Console.WriteLine($"{group.GroupName} created on: {group.CreateDate}"); }); if (response.IsTruncated) { request.Marker = response.Marker; response = await client.ListGroupsAsync(request); } } while (response.IsTruncated);
  • For API details, see ListGroups 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.

// GroupWrapper encapsulates AWS Identity and Access Management (IAM) group actions // used in the examples. // It contains an IAM service client that is used to perform group actions. type GroupWrapper struct { IamClient *iam.Client } // ListGroups lists up to maxGroups number of groups. func (wrapper GroupWrapper) ListGroups(maxGroups int32) ([]types.Group, error) { var groups []types.Group result, err := wrapper.IamClient.ListGroups(context.TODO(), &iam.ListGroupsInput{ MaxItems: aws.Int32(maxGroups), }) if err != nil { log.Printf("Couldn't list groups. Here's why: %v\n", err) } else { groups = result.Groups } return groups, err }
  • For API details, see ListGroups 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 groups.

// Import required AWS SDK clients and commands for Node.js. import { iamClient } from "./libs/iamClient.js"; import {ListGroupsCommand} from "@aws-sdk/client-iam"; // Set the parameters. export const params = { RoleName: 'ROLE_NAME', /* This is a number value. Required */ Marker: 'MARKER', /* This is a string value. Optional */ MaxItems: 'MAX_ITEMS' /* This is a number value. Optional */ }; export const run = async () => { try { const data = await iamClient.send(new ListGroupsCommand({})); console.log("Success", data.Groups); } catch (err) { console.log("Error", err); } } run();
  • For API details, see ListGroups 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(); public function listGroups($pathPrefix = "", $marker = "", $maxItems = 0) { $listGroupsArguments = []; if ($pathPrefix) { $listGroupsArguments["PathPrefix"] = $pathPrefix; } if ($marker) { $listGroupsArguments["Marker"] = $marker; } if ($maxItems) { $listGroupsArguments["MaxItems"] = $maxItems; } return $this->iamClient->listGroups($listGroupsArguments); }
  • For API details, see ListGroups 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_groups(count): """ Lists the specified number of groups for the account. :param count: The number of groups to list. """ try: for group in iam.groups.limit(count): logger.info("Group: %s", group.name) except ClientError: logger.exception("Couldn't list groups for the account.") raise
  • For API details, see ListGroups 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 groups for the account. # # @param count [Integer] The maximum number of groups to list. def list_groups(count) @iam_resource.groups.limit(count).each do |group| puts("\t#{group.name}") end rescue Aws::Errors::ServiceError => e puts("Couldn't list groups for the account. Here's why:") puts("\t#{e.code}: #{e.message}") raise end
  • For API details, see ListGroups 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_groups( client: &iamClient, path_prefix: Option<String>, marker: Option<String>, max_items: Option<i32>, ) -> Result<ListGroupsOutput, SdkError<ListGroupsError>> { let response = client .list_groups() .set_path_prefix(path_prefix) .set_marker(marker) .set_max_items(max_items) .send() .await?; Ok(response) }
  • For API details, see ListGroups 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 listGroups() async throws -> [String] { var groupList: [String] = [] var marker: String? = nil var isTruncated: Bool repeat { let input = ListGroupsInput(marker: marker) let output = try await client.listGroups(input: input) guard let groups = output.groups else { return groupList } for group in groups { if let name = group.groupName { groupList.append(name) } } marker = output.marker isTruncated = output.isTruncated } while isTruncated == true return groupList }
  • For API details, see ListGroups in AWS SDK for Swift API reference.