Menu
AWS SDK for JavaScript
Developer Guide for SDK v2.152.0

Managing IAM Users


                                        Relationship between JavaScript environments, the SDK, and Lambda

This Node.js example shows you how to retrieve a list of IAM users, create and delete users, and update a user name.

The Scenario

In this example, a series of Node.js modules are used to create and manage users in IAM. The Node.js modules use the SDK for JavaScript to create, delete, and update users using these methods of the AWS.IAM client class:

For more information about IAM users, see IAM Users in the IAM User Guide.

Prerequisite Tasks

To set up and run this example, you must first complete these tasks:

Configuring the SDK

Configure the SDK for JavaScript by creating a global configuration object then setting the region for your code. In this example, the region is set to us-west-2.

Copy
// Load the SDK for JavaScript var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'us-west-2'});

Creating a User

Create a Node.js module with the file name iam_createuser.js. Be sure to configure the SDK as previously shown. To access IAM, create an AWS.IAM service object. Create a JSON object containing the parameters needed, which consists of the user name you want to use for the new user as a command-line parameter.

Call the getUser method of the AWS.IAM service object to see if the user name already exists. If the user name does not currently exist, call the createUser method to create it. If the name already exists, write a message to that effect to the console.

Copy
// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the IAM service object var iam = new AWS.IAM({apiVersion: '2010-05-08'}); var params = { UserName: process.argv[2] }; iam.getUser(params, function(err, data) { if (err && err.code === 'NoSuchEntity') { iam.createUser(params, function(err, data) { if (err) { throw err; } else { console.log("Success", data); } }); } else { console.log("User " + process.argv[2] + " already exists", data.User.UserId); } });

To run the example, type the following at the command line.

Copy
node iam_createuser.js USER_NAME

This sample code can be found here on GitHub.

Listing Users in Your Account

Create a Node.js module with the file name iam_listusers.js. Be sure to configure the SDK as previously shown. To access IAM, create an AWS.IAM service object. Create a JSON object containing the parameters needed to list your users, limiting the number returned by setting the MaxItems parameter to 10. Call the listUsers method of the AWS.IAM service object. Write the first user's name and creation date to the console.

Copy
// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the IAM service object var iam = new AWS.IAM({apiVersion: '2010-05-08'}); var params = { MaxItems: 10 }; iam.listUsers(params, function(err, data) { if (err) { throw err; } else { var users = data.Users || []; users.forEach(function(user) { console.log("User " + user.UserName + " created", user.CreateDate); }); } });

To run the example, type the following at the command line.

Copy
node iam_listusers.js

This sample code can be found here on GitHub.

Updating a User's Name

Create a Node.js module with the file name iam_updateuser.js. Be sure to configure the SDK as previously shown. To access IAM, create an AWS.IAM service object. Create a JSON object containing the parameters needed to list your users, specifying both the current and new user names as command-line parameters. Call the updateUser method of the AWS.IAM service object.

Copy
// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the IAM service object var iam = new AWS.IAM({apiVersion: '2010-05-08'}); var params = { UserName: process.argv[2], NewUserName: process.argv[3] }; iam.updateUser(params, function(err, data) { if (err) { throw err; } else { console.log("Success", data); } });

To run the example, type the following at the command line, specifying the user's current name followed by the new user name.

Copy
node iam_updateuser.js ORIGINAL_USERNAME NEW_USERNAME

This sample code can be found here on GitHub.

Deleting a User

Create a Node.js module with the file name iam_deleteuser.js. Be sure to configure the SDK as previously shown. To access IAM, create an AWS.IAM service object. Create a JSON object containing the parameters needed, which consists of the user name you want to delete as a command-line parameter.

Call the getUser method of the AWS.IAM service object to see if the user name already exists. If the user name does not currently exist, write a message to that effect to the console. If the user exists, call the deleteUser method to delete it.

Copy
// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the IAM service object var iam = new AWS.IAM({apiVersion: '2010-05-08'}); var params = { UserName: process.argv[2] }; iam.getUser(params, function(err, data) { if (err && err.code === 'NoSuchEntity') { console.error("User " + process.argv[2] + " does not exist."); throw err; } else { iam.deleteUser(params, function(err, data) { if (err) { throw err; } else { console.log("User " + process.argv[2] + " deleted."); } }); } });

To run the example, type the following at the command line.

Copy
node iam_deleteuser.js USER_NAME

This sample code can be found here on GitHub.