2
votes

Using the Node sdk for AWS, I'm trying to use the credentials and permissions given by the IAM role that is attached to the EC2 instance that my Node application is running on.

According to the sdk documentation, that can be done using the EC2MetadataCredentials class to assign the configuration properties for the sdk.

In the file that I'm using the sdk in to access a DynamoDB instance, I have the configuration code:

import AWS from 'aws-sdk'

AWS.config.region = 'us-east-1'
AWS.config.credentials = new AWS.EC2MetadataCredentials({
    httpOptions: { timeout: 5000 },
    maxRetries: 10,
    retryDelayOptions: { base: 200 }
})

const dynamodb = new AWS.DynamoDB({
    endpoint: 'https://dynamodb.us-east-1.amazonaws.com',
    apiVersion: '2012-08-10'
})

However, when I trying to visit the web application I always get an error saying:

Uncaught TypeError: d.default.EC2MetadataCredentials is not a constructor

Uncaught TypeError: _awsSdk2.default.EC2MetadataCredentials is not a constructor

Even though that is the exact usage from the documentation! Is there something small that I'm missing?


Update:

Removing the credentials and region definitions from the file result in another error that'll say:

Error: Missing region|credentials in config

1
If you have a IAM role attached to an instance, credentials will be automatically supplied. You don't need to config the credentials. Can you try? - helloV
@helloV I have tried to just remove the credentials assignment, but then I get 'Missing credentials in config' error - m_callens
All your code could be reduced to just 2 lines that should use the EC2 IAM role automatically: import AWS from 'aws-sdk'; const dynamodb = new AWS.DynamoDB(); - Mark B
@MarkB When I remove the region and credential assignments, i get Error: Missing (region|credentials) in config - m_callens
Are you sure you are actually running this on an EC2 server with am IAM role assigned to it? - Mark B

1 Answers

0
votes

I don't know if this is still relevant for you, but you do need to configure the EC2MetadataCredentials as it is not in the default ProviderChain ( search for new AWS.CredentialProviderChain([ in node_loader.js in the sdk).
It seems you might have an old version of aws_sdk as that code works for me:

import AWS from 'aws-sdk';
...
AWS.config.credentials = new AWS.EC2MetadataCredentials();