Configuring region in Node.js AWS SDK

128,466

Solution 1

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

var AWS = require('aws-sdk');
AWS.config.update({region:'us-east-1'});

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

Solution 2

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" 
}

Solution 3

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

AWS_SDK_LOAD_CONFIG=1

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

Solution 4

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'});

Solution 5

Same error for me:

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

OPTION 1

  1. set the AWS_REGION environment variable in local system only, to us-east-1 (example)

For Linux:

export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
export AWS_DEFAULT_REGION=us-east-1

For Windows
see: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html

  1. now, no need to set any lambda variables for region
  2. also, no need to use in code, for example:

    • AWS.config.update(...), this is not required
    • AWS.S3(), etc., these will work without any problems. In place of S3, there can be any aws service

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})


OPTION 2

Instead of environment variables, another way is AWS CONFIG file:

On Linux you can create below files:

~/.aws/credentials

[default]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

~/.aws/config

[default]
region=us-west-2
output=json

See https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html

Share:
128,466

Related videos on Youtube

Anejah Daniels
Author by

Anejah Daniels

Updated on April 25, 2022

Comments

  • Anejah Daniels
    Anejah Daniels about 2 years

    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';
    
    AWS.config.update({region:'us-east-1'});
    
    (...)
    
  • HaneTV
    HaneTV over 7 years
    new AWS.DynamoDB({'region': 'us-east-1'}) doesn't work, you need to call AWS.config.update({region:'your region'})
  • WaffleSouffle
    WaffleSouffle over 7 years
    At the moment I'm testing with dynamodb running locally so the behaviour may be different. It certainly works in all the code I'm using in that environment. var dynamodb = new AWS.DynamoDB({ 'region': 'eu-west-1', 'endpoint': 'http://localhost:8000' }); var docClient = new AWS.DynamoDB.DocumentClient({"service": dynamodb}); Should work given it's in the documentation
  • duhseekoh
    duhseekoh about 7 years
    Lol. So The SDK will read from the shared credentials file, but the config thats always paired with it that, forget about it!
  • FredArters
    FredArters almost 7 years
    Hours of searching for why this was failing.. this fixed it.
  • Dimitry K
    Dimitry K over 6 years
    Kudos for highlighting that CLI uses AWS_DEFAULT_REGION and Sdk AWS_REGION. That's something non-obvious and something what bit me in the past. It is highlighted at the bottom of AWS SDK For Javascript Developer Guide - Setting Region but it's not obvious
  • SteckDEV
    SteckDEV over 6 years
    THANK YOU! I had already executed the other SET commands. However this one was necessary to use them in my Node.JS app. set AWS_ACCESS_KEY_ID="KEY ID GOES HERE" set AWS_SECRET_ACCESS_KEY="SECRET KEY GOES HERE" set AWS_REGION="us-east-1"
  • Gareth Oakley
    Gareth Oakley over 6 years
    The SDK can read from ~/.aws/config, but you need to set the environment variable AWS_SDK_LOAD_CONFIG to true
  • jarmod
    jarmod about 6 years
    AWS_SDK_LOAD_CONFIG is supported as of 2.44.0, per the SDK change log.
  • sashok_bg
    sashok_bg over 5 years
    worked perfectly and does not require hardcoding anything in your script: just put process.env.AWS_SDK_LOAD_CONFIG=1; before including AWS
  • Justin Force
    Justin Force over 5 years
    This was my problem. I thought it wouldn't be stupid, and I guess that was my mistake.
  • Atul Kumar
    Atul Kumar about 5 years
    This will set the region to us-east-1 for all resources, use resource specific region while creating object.
  • ffeast
    ffeast about 5 years
    It's just a hack without trying to figure out why nodejs aws client ignores ~\.aws\config settings
  • LJT
    LJT almost 5 years
    Thanks, this was the problem I had when setting httpOptions after instantiating cloudwatch
  • Malcolm Salvador
    Malcolm Salvador over 4 years
    Where do you set this environment variable? what do you mean by local system?
  • Manohar Reddy Poreddy
    Manohar Reddy Poreddy over 4 years
    @MalcolmSalvador I have updated answer for you, see above.
  • Tim Newton
    Tim Newton almost 4 years
    worked great. For information anyone using vscode and bash shell you can add the enviornment variable as follows :- $ export AWS_SDK_LOAD_CONFIG=1
  • pmiranda
    pmiranda over 3 years
    @AtulKumar you are right, this is pretty insecure. In my case, the SQS service is un us-east-1, the rest in us-east-2, and some SNS in Latin America
  • Thomas Lehoux
    Thomas Lehoux almost 3 years
    export AWS_REGION=us-east-1 instead of export AWS_DEFAULT_REGION=us-east-1
  • Tyler2P
    Tyler2P over 2 years
    Your answer could be improved by adding more information on what the code does and how it helps the OP.