Managing IAM account aliases - AWS SDK for JavaScript

Help us improve the AWS SDK for JavaScript version 3 (V3) documentation by providing feedback using the Feedback link, or create an issue or pull request on GitHub.

The AWS SDK for JavaScript V3 API Reference Guide describes in detail all the API operations for the AWS SDK for JavaScript version 3 (V3).

Managing IAM account aliases

JavaScript code example that applies to Node.js execution

This Node.js code example shows:

  • How to manage aliases for your AWS account ID.

The scenario

If you want the URL for your sign-in page to contain your company name or other friendly identifier instead of your AWS account ID, you can create an alias for your AWS account ID. If you create an AWS account alias, your sign-in page URL changes to incorporate the alias.

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

For more information about IAM account aliases, see Your AWS account ID and its alias in the IAM User Guide.

Prerequisite tasks

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

  • Set up the project environment to run these Node TypeScript examples, and install the required AWS SDK for JavaScript and third-party modules. Follow the instructions on GitHub.

  • Create a shared configurations file with your user credentials. For more information about providing a shared credentials file, see Loading credentials in Node.js from the shared credentials file.

Important

These examples demonstrate how to import/export client service objects and command using ECMAScript6 (ES6).

Creating an account alias

Create a libs directory, and create a Node.js module with the file name iamClient.js. Copy and paste the code below into it, which creates the IAM client object. Replace REGION with your AWS Region.

import { IAMClient } from "@aws-sdk/client-iam"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an IAM service client object. const iamClient = new IAMClient({ region: REGION }); export { iamClient };

This example code can be found here on GitHub.

Create a Node.js module with the file name iam_createaccountalias.js. Be sure to configure the SDK as previously shown, including downloading the required clients and packages. Create a JSON object containing the parameters needed to create an account alias, which includes the alias to create. Call the CreateAccountAliasCommand method of the IAM client service object.

Note

Replace ACCOUNT_ALIAS with the alias to create.

// Import required AWS SDK clients and commands for Node.js import { iamClient } from "./libs/iamClient.js"; import { CreateAccountAliasCommand } from "@aws-sdk/client-iam"; // Set the parameters const params = { AccountAlias: "ACCOUNT_ALIAS" }; //ACCOUNT_ALIAS const run = async () => { try { const data = await iamClient.send(new CreateAccountAliasCommand(params)); console.log("Success", data); return data; } catch (err) { console.log("Error", err); } }; run();

To run the example, enter the following at the command prompt.

node iam_createaccountalias.js

This example code can be found here on GitHub.

Listing account aliases

Create a libs directory, and create a Node.js module with the file name iamClient.js. Copy and paste the code below into it, which creates the IAM client object. Replace REGION with your AWS Region.

import { IAMClient } from "@aws-sdk/client-iam"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an IAM service client object. const iamClient = new IAMClient({ region: REGION }); export { iamClient };

This example code can be found here on GitHub.

Create a Node.js module with the file name iam_listaccountaliases.js. Be sure to configure the SDK as client previously shown, including downloading the required clients and packages. Create a JSON object containing the parameters needed to list account aliases, which includes the maximum number of items to return. Call the ListAccountAliasesCommand method of the IAM client service object.

// Import required AWS SDK clients and commands for Node.js import { iamClient } from "./libs/iamClient.js"; import { ListAccountAliasesCommand } from "@aws-sdk/client-iam"; // Set the parameters const params = { MaxItems: 5 }; const run = async () => { try { const data = await iamClient.send(new ListAccountAliasesCommand(params)); console.log("Success", data); return data; } catch (err) { console.log("Error", err); } }; run();

To run the example, enter the following at the command prompt.

node iam_listaccountaliases.js

This example code can be found here on GitHub.

Deleting an account alias

Create a libs directory, and create a Node.js module with the file name iamClient.js. Copy and paste the code below into it, which creates the IAM client object. Replace REGION with your AWS Region.

import { IAMClient } from "@aws-sdk/client-iam"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an IAM service client object. const iamClient = new IAMClient({ region: REGION }); export { iamClient };

This example code can be found here on GitHub.

Create a Node.js module with the file name iam_deleteaccountalias.js. Be sure to configure the SDK as previously shown, including downloading the required clients and packages. Create a JSON object containing the parameters needed to delete an account alias, which includes the alias you want deleted. Call the DeleteAccountAliasCommand method of the IAM service object.

Note

Replace ALIAS with the name of the alias you want to delete.

// Import required AWS SDK clients and commands for Node.js import { iamClient } from "./libs/iamClient.js"; import { DeleteAccountAliasCommand } from "@aws-sdk/client-iam"; // Set the parameters const params = { AccountAlias: "ALIAS" }; // ALIAS const run = async () => { try { const data = await iamClient.send(new DeleteAccountAliasCommand(params)); console.log("Success", data); return data; } catch (err) { console.log("Error", err); } }; run();

To run the example, enter the following at the command prompt.

node iam_deleteaccountalias.js

This example code can be found here on GitHub.