Aws-sdk-js: AWS.ECSCredentials returns undefined accessKeyId and sessionToken

Created on 27 May 2020  路  4Comments  路  Source: aws/aws-sdk-js

Confirm by changing [ ] to [x] below to ensure that it's a bug:

Describe the bug
new AWS.ECSCredentials returns undefined accessKeyId and sessionToken. However, the value of AWS_CONTAINER_CREDENTIALS_RELATIVE_URI is present, and I am able to print out correct AccessKeyId and SecretAccessKey by curl it

See more details on the code below:

const express = require('express');
const AWS = require('aws-sdk');
const fetch = require('node-fetch');

const AWS_CONTAINER_CREDENTIALS_RELATIVE_URI =
  process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI;

const PORT = 8080;
const HOST = '0.0.0.0';

(async () => {
  console.log(AWS_CONTAINER_CREDENTIALS_RELATIVE_URI); // prints correct URI

  const res = await fetch(
    `http://169.254.170.2${AWS_CONTAINER_CREDENTIALS_RELATIVE_URI}`,
  );
  const data = await res.json();
  console.log(data); // prints correct AccessKeyId, SecretAccessKey and Token

  const creds = new AWS.ECSCredentials({
    httpOptions: { timeout: 50000 },
    maxRetries: 10,
  });
  console.log(creds); // prints undefined accessKeyId and sessionToken???

  // App
  const app = express();
  app.get('/', (req, res) => {
    res.send('Hello World');
  });

  app.listen(PORT, HOST);
  console.log(`Running on http://${HOST}:${PORT}`);
})();

Similar issue: https://github.com/aws/aws-sdk-js/issues/1878, but it didn't not solve my case :(

Is the issue in the browser/Node.js?
Node.js

If on Node.js, are you running this on AWS Lambda?
No

Details of the browser/Node.js version
Node version: v14.3.0

SDK version number
v2.684.0

To Reproduce (observed behavior)
Steps to reproduce the behavior (please share code or minimal repo)

Expected behavior
new AWS.ECSCredentials should return correct accessKeyId and sessionToken

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
Add any other context about the problem here.

I have been struggling to get it working for days, any help would be appreciated! Thanks in advance 馃檱

documentation

Most helpful comment

@philipwigg you saved my day!

All 4 comments

I don't totally understand from the documentation what the expected behaviour is but it seems like you need to call .get() before AWS.ECSCredentials actually fetches the credentials.

I tried this code in a Fargate cluster and I can see the creds this way.

```const express = require('express');
const AWS = require('aws-sdk');
const fetch = require('node-fetch');

const AWS_CONTAINER_CREDENTIALS_RELATIVE_URI =
process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI;

const PORT = 8080;
const HOST = '0.0.0.0';

(async () => {

const creds = new AWS.ECSCredentials({
httpOptions: { timeout: 50000 },
maxRetries: 10,
})

creds.get( (err) => {
if (err) return console.log(err)
else {
console.log(creds)
}
})

// App
const app = express();
app.get('/', (req, res) => {
res.send('Hello World');
});

app.listen(PORT, HOST);
console.log(Running on http://${HOST}:${PORT});
})();```

@philipwigg you saved my day!

Reopen this issue because from the documentation (https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS.html#ECSCredentials-property), it didn't mention about we need to call get or getPromise explicitly. I understand that calling these two methods will load credentials, but shouldn't new AWS.ECSCredentials already load credentials in first place?

Marking this as a documentation issue.

Was this page helpful?
0 / 5 - 0 ratings