Configuring region in Node.js AWS SDK
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:
Configure it programmatically (hard-coded):
AWS.config.update({region:'your-region'});
Use an environment variable. While the CLI uses
AWS_DEFAULT_REGION
, the Node SDK usesAWS_REGION
.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
- set the
AWS_REGION
environment variable in local system only, tous-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
- now, no need to set any lambda variables for region
-
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
Related videos on Youtube
Anejah Daniels
Updated on April 25, 2022Comments
-
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 over 7 yearsnew AWS.DynamoDB({'region': 'us-east-1'}) doesn't work, you need to call AWS.config.update({region:'your region'})
-
WaffleSouffle over 7 yearsAt 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 about 7 yearsLol. So The SDK will read from the shared credentials file, but the config thats always paired with it that, forget about it!
-
FredArters almost 7 yearsHours of searching for why this was failing.. this fixed it.
-
Dimitry K over 6 yearsKudos for highlighting that CLI uses
AWS_DEFAULT_REGION
and SdkAWS_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 over 6 yearsTHANK 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 over 6 yearsThe SDK can read from ~/.aws/config, but you need to set the environment variable
AWS_SDK_LOAD_CONFIG
totrue
-
jarmod about 6 yearsAWS_SDK_LOAD_CONFIG is supported as of 2.44.0, per the SDK change log.
-
sashok_bg over 5 yearsworked perfectly and does not require hardcoding anything in your script: just put process.env.AWS_SDK_LOAD_CONFIG=1; before including AWS
-
Justin Force over 5 yearsThis was my problem. I thought it wouldn't be stupid, and I guess that was my mistake.
-
Atul Kumar about 5 yearsThis will set the region to
us-east-1
for all resources, use resource specific region while creating object. -
ffeast about 5 yearsIt's just a hack without trying to figure out why nodejs aws client ignores ~\.aws\config settings
-
LJT almost 5 yearsThanks, this was the problem I had when setting httpOptions after instantiating cloudwatch
-
Malcolm Salvador over 4 yearsWhere do you set this
environment variable
? what do you mean bylocal system
? -
Manohar Reddy Poreddy over 4 years@MalcolmSalvador I have updated answer for you, see above.
-
Tim Newton almost 4 yearsworked great. For information anyone using vscode and bash shell you can add the enviornment variable as follows :- $ export AWS_SDK_LOAD_CONFIG=1
-
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 almost 3 years
export AWS_REGION=us-east-1
instead ofexport AWS_DEFAULT_REGION=us-east-1
-
Tyler2P over 2 yearsYour answer could be improved by adding more information on what the code does and how it helps the OP.