javascript - mock - aws.config.update example

Configuring region in Node.js AWS SDK (6)

How about changing the order of statements? Update AWS config before instantiating s3 and dd

var AWS = require('aws-sdk');

var dd = new AWS.DynamoDB();
var s3 = new AWS.S3();

Can someone explain how to fix a missing config error with Node.js? I've 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';



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

To solve this, you have three options:

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

  2. Use an environment variable. While the CLI uses AWS_DEFAULT_REGION , the Node SDK uses AWS_REGION .

  3. Load from a JSON file using AWS.config.loadFromPath('./config.json');

JSON format:

    "accessKeyId": "akid", 
    "secretAccessKey": "secret", 
    "region": "us-east-1" 

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



Same error for me:

After doing a lot of trials
I have settled on the below:

  1. set the AWS_REGION environment variable in local system only, to us-east-1 (example)
  2. now, no need to set any lambda variables for region
  3. also, no need to use in code, for example:

    AWS.config.update(...) this is not required
    AWS.XYZ() this will work without any problems XYZ is any aws service like S3 or other

[[ In a rare case ]] if somewhere some defaults are assumed in code and you are forced to send region, then use {'region': process.env.AWS_REGION})

You can specify the region when creating the dynamodb connection (haven't tried s3 but that should work too).

var AWS = require('aws-sdk');
var dd = new AWS.DynamoDB({'region': 'us-east-1'});

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

var AWS = require('aws-sdk')

module.exports = {
    getClient: function(region) {
        AWS.config.update({ region: region })
        return new AWS.S3()

and consume it as,

 var s3Client = s3.getClient(config.region)

the idea is to Update AWS config before instantiating s3