Guide on retrieving the AWS IAM user in string/json format using STS GetCallerIdentity Caller JS/TS

I am attempting to retrieve the AWS IAM Arn user using the STS GetCallerIdentity API provided by Amazon. The following code successfully logs the correct data in the console. Nevertheless, I am encountering difficulty returning the data as a string or JSON format since it is nested inside a parameter.

Is there a way to extract the required information stored in the data callback?

Any suggestions or advice on how to achieve this would be greatly appreciated!!

export function GetIamUser() {
    const sts = new AWS.STS();

    sts.getCallerIdentity({}, function (err, data) {
        if (err) console.log(err, err.stack);
        else console.log(data);
    });
}

https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/STS.html#getCallerIdentity-property

When I execute the aforementioned method, the output is as follows:

   data = {
    Account: "123456789012", 
    Arn: "arn:aws:iam::123456789012:user/user", 
    UserId: "AKIAI44QH8DHBEXAMPLE"
   }

UPDATE: I have already attempted to retrieve the function callback using the method below, but the value returned is consistently undefined

export function GetArnUser() {
    var user;
    sts.getCallerIdentity({}, function (err, data) {
        if (err) console.log(err, err.stack);
        else console.log(data);
        user = data.Arn;
    });
    return user;
}

Answer №1

The issue at hand is related to asynchronous programming. In this case, you are utilizing callbacks, which requires passing a callback function to GetArnUser(), as illustrated below

export function GetArnUser(cb) {
    var user;
    sts.getCallerIdentity({}, function (err, data) {
        if (err) console.log(err, err.stack);
        else {
            console.log(data);
            user = data.Arn;
            cb(user)
        }
    });
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The fuse box was to blame for triggering a GET request to http://localhost:4444/, resulting in the error message

I encountered an issue with fuse-box and P5.js where I received a GET http://localhost:4444/ net::ERR_CONNECTION_REFUSED. The complete code can be accessed on GitHub. The fuse.js file contains the following configuration: const { FuseBox, WebIndexPlugin ...

Capturing the process exit event in Express: A guide

process.on('exit', async () => { console.log('updating') await campaignHelper.setIsStartedAsFalse() console.log('exit') process.exit(1) }) This code snippet is designed to trigger an update in the database before t ...

What is the best way to capture source code and store it in a text file?

Can you assist me in extracting the source code of a webpage and saving it into a text file? My goal: Instead of going through the process of right-clicking on the page, selecting view source code, copying, and pasting into a text file... I want to simpl ...

Guide on inserting a dropdown menu following a specific count of <li> elements using Javascript or Jquery

I am currently working on an HTML project <div class="ktmsg"> <ul> <li class="a1"> <a title="Link Tools" href="#"> … </a> </li> <li class="a2"> <a title="Link Tools" href="#"> ...

TSLint Errors Update: The configuration provided cannot locate implementations for the following rules

After upgrading my tslint to version 4.0.2, I encountered numerous errors like the ones shown below: Could not find implementations for the following rules specified in the configuration: directive-selector-name component-selector-name directi ...

Utilizing shared components across a Next.js application within a monorepo

Utilizing a monorepo to share types, DTOs, and other isomorphic app components from backend services (Nest.js) within the same mono repo has presented some challenges for me. In my setup, both the next.js app and nest.js app (which itself is a nest.js mono ...

How can I update two divs simultaneously with just one ajax call?

Is there a way to update 2 divs using only one ajax request? The code for updating through ajax is in ajax-update.php. <?php include("config.inc.php"); include("user.inc.php"); $id = $_GET['id']; $item = New item($id); $result = $item->it ...

Encountering an error when attempting to show user details on a webpage using Angular and Ionic with Promise functionality

On my app's AccountSettingsPage, I am fetching user data from a SQLite DB and displaying it on an Ionic page. However, I encountered the following error: Error: TypeError: Cannot read property 'name' of undefined at Object.eval [as upd ...

Error in TypeScript: Typography type does not accept 'string' type as valid

As I attempt to implement the Typography component from material-ui using TypeScript, I encounter a perplexing error message TypeScript is throwing an error: Type 'string' is not assignable to type 'ComponentClass<HTMLAttributes<HTMLE ...

Having difficulty transmitting data through SocketIO in NodeJS

My current project involves sending streams of data through SocketIO sockets. I'm working on a Node TS application that will handle large files, so the standard Buffer object won't cut it. Here's the code I have so far: Client Side socket. ...

Can you explain the distinction between using angular.copy() and simply using an assignment (=) to assign values

When a button is clicked, I want to assign certain values using the event parameter. Here is the code: $scope.update = function(context) { $scope.master = context; }; The $scope.master has been assigned the values of user. Recently, I came across th ...

Is there a way to add text to a table row using knockout?

I have been tasked with incorporating knockout js into my application. The table structure is as follows: <table> <tr> <th> Name </th> <th> Category </th> ...

Encountering issues with scope: Unable to retrieve value and receiving an error message stating 'Cannot assign value to undefined property'

var mainApp = angular.module("Main", []); mainApp.controller("CtrlMain", [ function ($scope) { $scope.amount = 545 }]);` var app = angular.module("Main", []); app.controller("MainCtrl", [ function ($scope) { $scope.value = 545 ...

Creating typescript as an optional feature for your ionic 2 applications

When incorporating a new JS library into an Angular component of an ionic 2 application, the initial step involves installing types through typings. I had the understanding that typescript was not a requirement for ionic 2. Is there a way to configure Io ...

Guide to accessing the request object within the entity class in NestJS

Currently tackling a project in nestJs that requires adding audit columns to certain entities. I have set up user information on a request object, which seems to be working fine when printed in the controller. However, I am facing issues with implementing ...

Angular list with a repeating group of radio buttons

I have a set of 'options', which consists of the following: {Id: 1, Label: "option 1"}, {Id: 2, Label: "option 2"} Additionally, I have a list of 'products' structured as follows: {Id: 1, Name: "Name 1", Recommend: options[0]}, {Id: ...

Error: React cannot read property 'any' since it is undefined

I am brand new to React and currently in the process of building a small app by following a tutorial on Udemy. The app includes a form, and while trying to import the redux-form library into my project, I encountered the following console error: https://i ...

Issue with Promise not resolving in Node when using Edge

As I explore the best way to utilize my C# dlls with Edgejs for Node, I encountered a situation where one proxy function in Node appears like this (a class method in Typescript): readSettings(args: ReadSettingsParams) : Promise<response> { let $ ...

Ionic is experiencing issues with CORS

I've been attempting to send a JSON to a PHP script on a server, and even though I have added CORS headers in the PHP script, the console is still throwing an error. Error Message XMLHttpRequest cannot load . No 'Access-Control-Allow-Origin&a ...

The raw password value must be provided and cannot be left blank

I am currently learning Java Springboot and working on creating a todo app with React (TypeScript) and Springboot. As I attempt to signup, an error occurs stating "rawPassword cannot be null" from Springboot. My frontend is running on localhost:3000 and b ...