-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathget-access-key-last-used.js
More file actions
37 lines (29 loc) · 1.05 KB
/
get-access-key-last-used.js
File metadata and controls
37 lines (29 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { fileURLToPath } from "node:url";
// snippet-start:[iam.JavaScript.keys.getAccessKeyLastUsedV3]
import { GetAccessKeyLastUsedCommand, IAMClient } from "@aws-sdk/client-iam";
const client = new IAMClient({});
/**
*
* @param {string} accessKeyId
*/
export const getAccessKeyLastUsed = async (accessKeyId) => {
const command = new GetAccessKeyLastUsedCommand({
AccessKeyId: accessKeyId,
});
const response = await client.send(command);
if (response.AccessKeyLastUsed?.LastUsedDate) {
console.log(`
${accessKeyId} was last used by ${response.UserName} via
the ${response.AccessKeyLastUsed.ServiceName} service on
${response.AccessKeyLastUsed.LastUsedDate.toISOString()}
`);
}
return response;
};
// snippet-end:[iam.JavaScript.keys.getAccessKeyLastUsedV3]
// Invoke main function if this file was run directly.
if (process.argv[1] === fileURLToPath(import.meta.url)) {
getAccessKeyLastUsed("ACCESS_KEY_ID");
}