Amazon STS examples using SDK for JavaScript V3 - Amazon SDK for JavaScript
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China.

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

Starting October 1, 2022, Amazon SDK for JavaScript (v3) will end support for Internet Explorer 11 (IE 11). For details, see Announcing the end of support for Internet Explorer 11 in the Amazon SDK for JavaScript (v3).

Amazon STS examples using SDK for JavaScript V3

The following code examples show you how to perform actions and implement common scenarios by using the Amazon SDK for JavaScript V3 with Amazon STS.

Actions are code excerpts that show you how to call individual Amazon STS functions.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple Amazon STS functions.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to assume a role with Amazon STS.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

Create the client.

import { STSClient } from "@aws-sdk/client-sts"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon STS service client object. const stsClient = new STSClient({ region: REGION }); export { stsClient };

Assume the IAM role.

// Import required AWS SDK clients and commands for Node.js import { stsClient } from "./libs/stsClient.js"; import { AssumeRoleCommand, GetCallerIdentityCommand, } from "@aws-sdk/client-sts"; // Set the parameters export const params = { RoleArn: "ARN_OF_ROLE_TO_ASSUME", //ARN_OF_ROLE_TO_ASSUME RoleSessionName: "session1", DurationSeconds: 900, }; export const run = async () => { try { //Assume Role const data = await stsClient.send(new AssumeRoleCommand(params)); return data; const rolecreds = { accessKeyId: data.Credentials.AccessKeyId, secretAccessKey: data.Credentials.SecretAccessKey, sessionToken: data.Credentials.SessionToken, }; //Get Amazon Resource Name (ARN) of current identity try { const stsParams = { credentials: rolecreds }; const stsClient = new STSClient(stsParams); const results = await stsClient.send( new GetCallerIdentityCommand(rolecreds) ); console.log("Success", results); } catch (err) { console.log(err, err.stack); } } catch (err) { console.log("Error", err); } }; run();
  • For API details, see AssumeRole in Amazon SDK for JavaScript API Reference.

SDK for JavaScript V2
Tip

To learn how to set up and run this example, see GitHub.

// Load the AWS SDK for Node.js const AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); var roleToAssume = {RoleArn: 'arn:aws:iam::123456789012:role/RoleName', RoleSessionName: 'session1', DurationSeconds: 900,}; var roleCreds; // Create the STS service object var sts = new AWS.STS({apiVersion: '2011-06-15'}); //Assume Role sts.assumeRole(roleToAssume, function(err, data) { if (err) console.log(err, err.stack); else{ roleCreds = {accessKeyId: data.Credentials.AccessKeyId, secretAccessKey: data.Credentials.SecretAccessKey, sessionToken: data.Credentials.SessionToken}; stsGetCallerIdentity(roleCreds); } }); //Get Arn of current identity function stsGetCallerIdentity(creds) { var stsParams = {credentials: creds }; // Create STS service object var sts = new AWS.STS(stsParams); sts.getCallerIdentity({}, function(err, data) { if (err) { console.log(err, err.stack); } else { console.log(data.Arn); } }); }
  • For API details, see AssumeRole in Amazon SDK for JavaScript API Reference.