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;
}