Search code examples
javascriptnode.jsaws-sdkaws-sts

trying to use AWS.STS.getSessionToken with node.js not able to retrieve token


I am somewhat new to node js and I am really stuck with this curious error:

This snippet retrieves a set of credentials from AWS:


  const params = {
    DurationSeconds: 3600
  };
  const credentials = await sts
    .getSessionToken(params)
    .promise()
    .then(data => {
      return data.Credentials;
    })
    .catch(err => {
      logger.error('ERROR: ', err);
    });

  logger.debug('Loaded credentials:', credentials);
  logger.debug('Token:', credentials.SessionToken);
  return credentials;
}

However even though it prints on the output that the SessionToken is available on the resulting Credentials object:

debug: debug: Loaded credentials: {"AccessKeyId":"REDACTED","SecretAccessKey":"REDACTED","SessionToken":"FwoGZXIvYXdzEK3//////////wEaDB6VRt+NtN7NL08wRiKBAYkJTVAX4Sx9GZSmm/WEarfCiO5ITnBMh4hQTCX7Yu13WBck+8/CF8XUcicTRUfosVVz5/+FUETRuuUhg3oLySt+ijs5v32BTIo7EU4LI0L69uOYAxGIPSIOpEl635/36Ytl0GNgQvMC1DuwmgTNzm8qA==","Expiration":"2020-06-09T12:33:00.000Z","label":"{\"name\": \"cluster-gtw\"}","timestamp":"2020-06-09 14:33:00"}

It is not able to use it:

error: undefined {"label":"{\"name\": \"cluster-gtw\"}","timestamp":"2020-06-09 14:33:00"}
error: TypeError: Cannot read property 'SessionToken' of undefined

I would very much appreciate your help understanding why SessionToken won't print and Credentials will.


Solution

  • In case anyone ever has this issue, this works:

      logger.debug(credentials['SessionToken']);
      return credentials['SessionToken'];