Perl AWS IAM: List Users

Back to Index

Lists the IAM users that have the specified path prefix. If no path prefix is specified, the operation returns all users in the AWS account. If there are none, the operation returns an empty list. .

Documentation: https://docs.aws.amazon.com/IAM/latest/APIReference/API_ListUsers.html

CURL Command

curl -X GET https://iam.amazonaws.com/ \
  -d "Action=ListUsers" \
  -d "PathPrefix=/" \
  -d "Version=2010-05-08"

Perl Example

use chilkat();

$rest = chilkat::CkRest->new();

$authAws = chilkat::CkAuthAws->new();
$authAws->put_AccessKey("AWS_ACCESS_KEY");
$authAws->put_SecretKey("AWS_SECRET_KEY");
$authAws->put_Region("us-east-1");
$authAws->put_ServiceName("iam");
$rest->SetAuthAws($authAws);

#  URL: https://iam.amazonaws.com/
$bTls = 1;
$port = 443;
$bAutoReconnect = 1;
$success = $rest->Connect("iam.amazonaws.com",$port,$bTls,$bAutoReconnect);
if ($success != 1) {
    print "ConnectFailReason: " . $rest->get_ConnectFailReason() . "\r\n";
    print $rest->lastErrorText() . "\r\n";
    exit;
}

$rest->AddQueryParam("Action","ListUsers");
$rest->AddQueryParam("PathPrefix","/");
$rest->AddQueryParam("Version","2010-05-08");

$sbResponseBody = chilkat::CkStringBuilder->new();
$success = $rest->FullRequestNoBodySb("GET","/",$sbResponseBody);
if ($success != 1) {
    print $rest->lastErrorText() . "\r\n";
    exit;
}

$respStatusCode = $rest->get_ResponseStatusCode();
if ($respStatusCode >= 400) {
    print "Response Status Code = " . $respStatusCode . "\r\n";
    print "Response Header:" . "\r\n";
    print $rest->responseHeader() . "\r\n";
    print "Response Body:" . "\r\n";
    print $sbResponseBody->getAsString() . "\r\n";
    exit;
}

$xmlResponse = chilkat::CkXml->new();
$xmlResponse->LoadSb($sbResponseBody,1);

$ListUsersResponse_xmlns = $xmlResponse->getAttrValue("xmlns");
$i = 0;
$count_i = $xmlResponse->NumChildrenHavingTag("ListUsersResult|Users|member");
while ($i < $count_i) {
    $xmlResponse->put_I($i);
    $UserId = $xmlResponse->getChildContent("ListUsersResult|Users|member[i]|UserId");
    $Path = $xmlResponse->getChildContent("ListUsersResult|Users|member[i]|Path");
    $UserName = $xmlResponse->getChildContent("ListUsersResult|Users|member[i]|UserName");
    $Arn = $xmlResponse->getChildContent("ListUsersResult|Users|member[i]|Arn");
    $CreateDate = $xmlResponse->getChildContent("ListUsersResult|Users|member[i]|CreateDate");
    $PasswordLastUsed = $xmlResponse->getChildContent("ListUsersResult|Users|member[i]|PasswordLastUsed");
    $i = $i + 1;
}

$IsTruncated = $xmlResponse->getChildContent("ListUsersResult|IsTruncated");
$RequestId = $xmlResponse->getChildContent("ResponseMetadata|RequestId");

Sample XML Response Body

<?xml version="1.0" encoding="utf-8" ?>
<ListUsersResponse xmlns="https://iam.amazonaws.com/doc/2010-05-08/">
    <ListUsersResult>
        <Users>
            <member>
                <UserId>AID2MAB8DPLSRHEXAMPLE</UserId>
                <Path>/division_abc/subdivision_xyz/engineering/</Path>
                <UserName>Andrew</UserName>
                <Arn>arn:aws:iam::123456789012:user/division_abc/subdivision_xyz/engineering/Andrew</Arn>
                <CreateDate>2012-09-05T19:38:48Z</CreateDate>
                <PasswordLastUsed>2014-09-08T21:47:36Z</PasswordLastUsed>
            </member>
            <member>
                <UserId>AIDIODR4TAW7CSEXAMPLE</UserId>
                <Path>/division_abc/subdivision_xyz/engineering/</Path>
                <UserName>Jackie</UserName>
                <Arn>arn:aws:iam::123456789012:user/division_abc/subdivision_xyz/engineering/Jackie</Arn>
                <CreateDate>2014-04-09T15:43:45Z</CreateDate>
                <PasswordLastUsed>2014-09-24T16:18:07Z</PasswordLastUsed>
            </member>
        </Users>
        <IsTruncated>false</IsTruncated>
    </ListUsersResult>
    <ResponseMetadata>
        <RequestId>7a62c49f-347e-4fc4-9331-6e8eEXAMPLE</RequestId>
    </ResponseMetadata>
</ListUsersResponse>