190

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

(...)
Zanon
  • 29,231
  • 20
  • 113
  • 126
Anejah Daniels
  • 1,901
  • 2
  • 10
  • 4

19 Answers19

276

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();
umbrel
  • 4,159
  • 1
  • 15
  • 13
  • 3
    Hours of searching for why this was failing.. this fixed it. – FredArters Aug 03 '17 at 21:27
  • 1
    This will set the region to `us-east-1` for all resources, use resource specific region while creating object. – Atul Kumar Apr 11 '19 at 09:12
  • 3
    It's just a hack without trying to figure out why nodejs aws client ignores ~\.aws\config settings – Oleg Kuralenko May 05 '19 at 20:30
  • Thanks, this was the problem I had when setting httpOptions after instantiating cloudwatch – LJT Aug 07 '19 at 02:57
  • @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 – pmiranda Dec 11 '20 at 21:04
  • Don't hard-code the region in your code. Use the `AWS_DEFAULT_REGION` environment variable instead. – Lqueryvg Oct 11 '22 at 08:27
  • Be aware: When using this approach you will have higher memory usage. You should use `AWS_REGION` instead. In my tests when simply importing just the clients you need resulted in an overall improved RAM usage by up to 600 Mi (according to k8s metric server). – Marvin Nov 15 '22 at 23:06
127

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" 
}
Zanon
  • 29,231
  • 20
  • 113
  • 126
  • 7
    Lol. So The SDK will read from the shared credentials file, but the config thats always paired with it that, forget about it! – duhseekoh Mar 14 '17 at 20:14
  • 13
    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](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-region.html) but it's not obvious – Dimitry K Jan 26 '18 at 19:55
  • 30
    The SDK can read from ~/.aws/config, but you need to set the environment variable `AWS_SDK_LOAD_CONFIG` to `true` – Gareth Oakley Feb 05 '18 at 16:44
  • 5
    This was my problem. I thought it wouldn't be stupid, and I guess that was my mistake. – Justin Force Jan 14 '19 at 19:36
  • Thanks. In my case I want to run a jest test. Always got this error and i tried to set AWS_DEFAULT_REGION like in my pipeline. Now I set the process.env.AWS_REGION in the test context and it works – devnik Mar 21 '23 at 16:23
99

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

Peter Dotchev
  • 2,980
  • 1
  • 25
  • 19
  • 3
    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"``` – SteckDEV Jan 29 '18 at 16:13
  • AWS_SDK_LOAD_CONFIG is supported as of 2.44.0, per the SDK change log. – jarmod Mar 20 '18 at 19:06
  • 3
    worked perfectly and does not require hardcoding anything in your script: just put process.env.AWS_SDK_LOAD_CONFIG=1; before including AWS – sashok_bg Aug 31 '18 at 14:33
  • worked great. For information anyone using vscode and bash shell you can add the enviornment variable as follows :- $ export AWS_SDK_LOAD_CONFIG=1 – Tim Newton Jun 09 '20 at 15:31
15

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

Manohar Reddy Poreddy
  • 25,399
  • 9
  • 157
  • 140
13

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'});
WaffleSouffle
  • 3,293
  • 2
  • 28
  • 27
  • 2
    new AWS.DynamoDB({'region': 'us-east-1'}) doesn't work, you need to call AWS.config.update({region:'your region'}) – HaneTV Dec 22 '16 at 15:41
  • 1
    At the moment I'm testing with dynamodb [running locally](http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.Download.html) 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](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#constructor-property) – WaffleSouffle Dec 22 '16 at 15:54
12
var AWS = require('aws-sdk');

// assign AWS credentials here in following way:

AWS.config.update({
  accessKeyId: 'asdjsadkskdskskdk',
  secretAccessKey: 'sdsadsissdiidicdsi',
  region: 'us-east-1'
});

var dd = new AWS.DynamoDB();
var s3 = new AWS.S3();
BHUVNESH KUMAR
  • 391
  • 4
  • 15
10

I have gone through your code and here you are connecting to AWS services before setting the region, so i suggest you to update the region first and then connect to services or create instance of those as 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';
Himanshu Tongya
  • 111
  • 1
  • 5
10

I'm impressed this hasn't been posted here yet.

Instead of setting the region with AWS.config.update(), you can use

const s3 = new AWS.S3({
  region: "eu-central-1",
});

to make it instance specific.

Sebastian
  • 1,593
  • 4
  • 26
  • 41
  • I wanted to say this too. I'm surprised this has not been mentioned everywhere. This is the official AWS guide: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-region.html – Thomas Cheng Feb 08 '23 at 21:39
6

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

To load the AWS config, 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.

Tony
  • 656
  • 8
  • 20
3

To the comment above, you can always it run from your local global config file ~./aws/config by adding the following:

process.env.AWS_SDK_LOAD_CONFIG="true";

This will load your local global config file and use whatever credentials/account you are in which is really handy when iterating through multiple accounts / roles.

2

You can resolve this issue right in your project directory.

  1. npm i -D dotenv.
  2. Create .env file in root of our project.
  3. Set environment variable AWS_SDK_LOAD_CONFIG=1 in that .env file.
  4. const {config} = require("dotenv"); in the same file where you configure connection to DynamoDB.
  5. config() before you new AWS.DynamoDB().

P.S. As someone have mentioned before, problem is that Node doesn't get data from your aws.config file

CoderDesu
  • 697
  • 1
  • 5
  • 14
2

It's 2022 and this is the top result on Google for "Missing region in config".

For those getting this error when using the AWS Node SDK in combination with profiles (i.e. ~/.aws/config and ~/.aws/credentials), the solution is to load the credentials for the profile name (via AWS.SharedIniFileCredentials), and then separately get the region for the profile name using the @aws-sdk/shared-ini-file-loader.

i.e.

import AWS from "aws-sdk";
import { loadSharedConfigFiles } from "@aws-sdk/shared-ini-file-loader";

const profileName = "default"; // change this to whatever profile name you want

loadSharedConfigFiles().then((awsConfig) => {
  // console.log(awsConfig);
  const region = awsConfig?.configFile?.[profileName]?.region;

  const credentials = new AWS.SharedIniFileCredentials({
    profile: profileName,
  });

  // now use the credentials and the profile's region however you want
  const pinpoint = new AWS.Pinpoint({
    credentials: credentials,
    region: region,
  });
  pinpoint.getApps({}, function (err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else console.log(data); // successful response
  });
});
cobberboy
  • 5,598
  • 2
  • 25
  • 22
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

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
1

I know I am EXTREMELY late to the party, but I have an additional solution which worked for me.

It might be worth passing credentials to each resource directly.

let lambda = AWS.Lambda({region: "us-east-1"});
let credentials = new AWS.SharedIniFileCredentials({
    profile: PROFILE_NAME,   
});

lambda.config.credentials = credentials;
lachlanv
  • 33
  • 1
  • 3
1

A team member of mine experienced this issue when trying to set up SNS Text Messaging with the AWS Node SDK.

We were getting this error when we run the process:

ConfigError: Invalid region in config

Here's how he solved it:

The initial way the AWS credentials are referenced are:

AWS_ACCESS_KEY='AKIAR5NCt72I72Nrt267'
AWS_SECRET_ACCESS_KEY='DhnqpuPdfV9nwFufsKJLJsydfJb7HNjPb5suwpvM'
AWS_REGION='us-west-1'

He simply removed the quotes '' around the credentials, so we had this:

AWS_ACCESS_KEY=AKIAR5NCt72I72Nrt267
AWS_SECRET_ACCESS_KEY=DhnqpuPdfV9nwFufsKJLJsydfJb7HNjPb5suwpvM
AWS_REGION=us-west-1

And it worked fine afterward.

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
0

Best Practice would be to utilize an Amazon Cognito Identity pool.

Create an IAM Policy that defines the access to the resource you want. (Least Access Privilege)

Then create an Amazon Cognito Identity Pool allowing unauthenticated identities. Then attached the IAM Policy you created to the Unauthenticated Role for the Identity Pool.

Once that is setup you use the following code:

   AWS.config.region = 'us-east-1';
    AWS.config.credentials = new AWS.CognitoIdentityCredentials({
        IdentityPoolId: 'IdentityPoolIdHere',
    });

Amazon Cognito assumes the IAM Role specified in unauthenticated identities where Amazon STS is utilized in the background which then populates config with temporary credentials with accessibility as defined in the attached IAM Policy for the IAM Role.

Leon Africa
  • 509
  • 6
  • 11
0
var AWS = require("aws-sdk");

AWS.config.getCredentials(function(err) {
  if (err) console.log(err.stack);
  // credentials not loaded
  else {
    console.log("Access key:", AWS.config.credentials.accessKeyId);
  }
});
  • Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Feb 05 '22 at 09:58
0

In my case, I was trying to use it in a React.JS app following this tutorial.

I needed to move the config data to the same file where I was calling the DocumentClient instead of having the config in my index.js file.

Ian
  • 1,746
  • 1
  • 15
  • 28
0
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 then you can use the following .


var s3Client = s3.getClient(config.region)
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 29 '22 at 10:54