How to configure the region in the AWS js SDK?
Asked Answered
T

3

5

My problem

I am writing a simple js function that reads some information from AWS CloudWatch Logs.

Following the answer at Configuring region in Node.js AWS SDK, and the AWS nodejs SDK documentation, I came up with the following:

Code

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

var cloudwatchlogs = new AWS.CloudWatchLogs();

console.log(AWS.config.region)              // Undefined

AWS.config.region = 'eu-central-1'          // Define the region with dot notation
console.log(AWS.config.region) .            // eu-central-1

AWS.config.update({region:'eu-central-1'}); // Another way to update
console.log(AWS.config.region) .            // eu-central-1


var params = {
  limit: 0,
//   logGroupNamePrefix: 'STRING_VALUE',
//   nextToken: 'STRING_VALUE'
};

// This call is failing
cloudwatchlogs.describeLogGroups(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

Output and error

undefined
eu-central-1
eu-central-1
{ ConfigError: Missing region in config
    at Request.VALIDATE_REGION (/Users/adam/binaris/adam-test-sls/node_modules/aws-sdk/lib/event_listeners.js:91:45)
    at Request.callListeners (/Users/adam/binaris/adam-test-sls/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
    at callNextListener (/Users/adam/binaris/adam-test-sls/node_modules/aws-sdk/lib/sequential_executor.js:95:12)
    at /Users/adam/binaris/adam-test-sls/node_modules/aws-sdk/lib/event_listeners.js:85:9
    at finish (/Users/adam/binaris/adam-test-sls/node_modules/aws-sdk/lib/config.js:315:7)
    at /Users/adam/binaris/adam-test-sls/node_modules/aws-sdk/lib/config.js:333:9
    at SharedIniFileCredentials.get (/Users/adam/binaris/adam-test-sls/node_modules/aws-sdk/lib/credentials.js:126:7)
    at getAsyncCredentials (/Users/adam/binaris/adam-test-sls/node_modules/aws-sdk/lib/config.js:327:24)
    at Config.getCredentials (/Users/adam/binaris/adam-test-sls/node_modules/aws-sdk/lib/config.js:347:9)
    at Request.VALIDATE_CREDENTIALS (/Users/adam/binaris/adam-test-sls/node_modules/aws-sdk/lib/event_listeners.js:80:26)
  message: 'Missing region in config',
  code: 'ConfigError',
  time: 2017-07-11T09:57:55.638Z } ...

Environment

The code is running locally under node v8.1.2.

My question

How can I correctly configure the region in the AWS js SDK?

Addendum

Opened an issue on github and got some response.

Thiouracil answered 11/7, 2017 at 10:46 Comment(2)
This problem persists across the SDK. SQS and Cloudsearch also require region and the SDK docs leave it out.Sickly
See Also: Configuring region in Node.js AWS SDKMendelsohn
P
6

Or, alternatively, you can specify that when creating your cloudwatch object:

var AWS = require('aws-sdk');
var cloudwatchlogs = new AWS.CloudWatchLogs({region: 'eu-central-1'});
Presidio answered 11/7, 2017 at 10:59 Comment(1)
Perfect. Worked like a charm. The AWS js documentation examples are misleading and counterproductive.Thiouracil
P
5

Write code in following way it will work.

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

// assign AWS credentials here in following way:
AWS.config.update({
  accessKeyId: 'asdjsadkskdskskdk',
  secretAccessKey: 'sdsadsissdiidicdsi',
  region: 'eu-central-1'
});
var cloudwatchlogs = new AWS.CloudWatchLogs({apiVersion: '2014-03-28'});
Poindexter answered 15/9, 2017 at 13:56 Comment(0)
R
1

Use following.

AWS.config.update({region: 'eu-central-1'});

You can find more information in following link.

http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-region.html

Roseleeroselia answered 11/7, 2017 at 10:50 Comment(3)
Unfortunately, it does not work - I've tried it in my code.Thiouracil
Strange. Getting the same output or different output?Roseleeroselia
Same, because this line already appears in my code. See above.Thiouracil

© 2022 - 2024 — McMap. All rights reserved.