List IAM groups using an Amazon SDK
The following code examples show how to list IAM groups.
- .NET
-
- Amazon SDK for .NET
-
Note There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository
. /// <summary> /// List IAM groups. /// </summary> /// <returns>A list of IAM groups.</returns> public async Task<List<Group>> ListGroupsAsync() { var groupsPaginator = _IAMService.Paginators.ListGroups(new ListGroupsRequest()); var groups = new List<Group>(); await foreach (var response in groupsPaginator.Responses) { groups.AddRange(response.Groups); } return groups; }
-
For API details, see ListGroups in Amazon 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 Amazon 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 Amazon 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 Amazon 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 Amazon 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 Amazon 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 Amazon 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 Amazon 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 Amazon 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 Amazon 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 Amazon 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 Amazon 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 Amazon 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 Amazon 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 Amazon SDK for Swift API reference.
-
For a complete list of Amazon SDK developer guides and code examples, see Using IAM with an Amazon SDK. This topic also includes information about getting started and details about previous SDK versions.