Scope Setup in the Node.js AWS SDK

Can someone explain how to fix a missing configuration error with Node.js? I followed all the examples from the aws doc page , but I still get this error no matter what.

{ [ConfigError: Missing region in config] message: 'Missing region in config', code: 'ConfigError', time: Wed Jun 24 2015 21:39:58 GMT-0400 (EDT) }>{ thumbnail: { fieldname: 'thumbnail', originalname: 'testDoc.pdf', name: 'testDoc.pdf', encoding: '7bit', mimetype: 'application/pdf', path: 'uploads/testDoc.pdf', extension: 'pdf', size: 24, truncated: false, buffer: null } } POST / 200 81.530 ms - - 

Here is my code:

 var express = require('express'); var router = express.Router(); var AWS = require('aws-sdk'); var dd = new AWS.DynamoDB(); var s3 = new AWS.S3(); var bucketName = 'my-bucket'; AWS.config.update({region:'us-east-1'}); (...) 
+91
javascript amazon-web-services aws-sdk
Jun 25 '15 at 1:54
source share
9 answers

What about changing the order of statements? Upgrade your AWS configuration to create s3 and dd instances

 var AWS = require('aws-sdk'); AWS.config.update({region:'us-east-1'}); var dd = new AWS.DynamoDB(); var s3 = new AWS.S3(); 
+175
Jul 28 '15 at 18:41
source share

I had the same "Missing region in config" problem, and in my case it was that, unlike the CLI or the Python SDK, the Node SDK would not read from the ~\.aws\config file.

To solve this problem, you have three options:

  • Configure it programmatically (hard-coded): AWS.config.update({region:'your-region'});

  • Use an environment variable. Although the CLI uses AWS_DEFAULT_REGION , the Node SDK uses AWS_REGION .

  • Download from a JSON file using AWS.config.loadFromPath('./config.json');

JSON format:

 { "accessKeyId": "akid", "secretAccessKey": "secret", "region": "us-east-1" } 
+68
Sep 19 '16 at 0:46
source share

If you are working with the AWS CLI, you probably have a default region defined in ~ / .aws / config. Unfortunately, the AWS SDK for JavaScript does not load it by default. To load it, define env var

 AWS_SDK_LOAD_CONFIG=1 

See https://github.com/aws/aws-sdk-js/pull/1391

+46
Sep 16 '17 at 5:35
source share

You can specify the scope when creating the dynamodb connection (did not try s3, but this should work too).

 var AWS = require('aws-sdk'); var dd = new AWS.DynamoDB({'region': 'us-east-1'}); 
+11
Dec 14 '16 at 18:25
source share

Same error for me:

After many tests, I settled on the following:

  1. set the AWS_REGION environment AWS_REGION only on the local system, to us-east-1 (example)
  2. Now there is no need to set lambda variables for the region
  3. in addition, there is no need to use in the code, for example:

    • AWS.config.update(...) , this is not necessary
    • AWS.XYZ() , this will work without problems. XYZ is any AWS service such as S3 or another

In the rare case , if somewhere in the code some default values ​​are assumed, and you are forced to send a region, use {'region': process.env.AWS_REGION})

+6
Mar 28 '17 at 18:13
source share
 var AWS = require('aws-sdk'); 

// assign the AWS credentials as follows:

 AWS.config.update({ accessKeyId: 'asdjsadkskdskskdk', secretAccessKey: 'sdsadsissdiidicdsi', region: 'us-east-1' }); var dd = new AWS.DynamoDB(); var s3 = new AWS.S3(); 
+6
Sep 15 '17 at 14:03
source share

This may not be the right way to do this, but I have all my configs in a separate JSON file. And this fixes the problem for me.

To load the AWS configuration, I do this:

 var awsConfig = config.aws; AWS.config.region = awsConfig.region; AWS.config.credentials = { accessKeyId: awsConfig.accessKeyId, secretAccessKey: awsConfig.secretAccessKey } 

config.aws is just a JSON file.

+4
Dec 13 '16 at 16:04
source share

I looked at your code and here you connect to AWS services before setting up a region, so I suggest you first update the region, and then connect to the services or create an instance of them, as shown below -

 var express = require('express'); var router = express.Router(); var AWS = require('aws-sdk'); AWS.config.update({region:'us-east-1'}); var dd = new AWS.DynamoDB(); var s3 = new AWS.S3(); var bucketName = 'my-bucket'; 
+4
Nov 15 '18 at 9:22
source share

You can create a common module and use it based on the region you want

 var AWS = require('aws-sdk') module.exports = { getClient: function(region) { AWS.config.update({ region: region }) return new AWS.S3() } } 

and consume it like that

  var s3Client = s3.getClient(config.region) 

the idea is to upgrade your AWS configuration to create an s3 instance

+1
Oct 31 '17 at 19:37
source share



All Articles