5

我的问题

我正在编写一个简单的 js 函数,它从 AWS CloudWatch Logs 读取一些信息。

按照在 Node.js AWS SDKAWS nodejs SDK 文档中配置区域的答案,我想出了以下内容:

代码

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

输出和错误

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

环境

代码在本地运行node v8.1.2

我的问题

如何在 AWS js 开发工具包中正确配置区域?

附录

在github上打开了一个问题并得到了一些回复。

4

3 回答 3

6

或者,您可以在创建 cloudwatch 对象时指定:

var AWS = require('aws-sdk');
var cloudwatchlogs = new AWS.CloudWatchLogs({region: 'eu-central-1'});
于 2017-07-11T10:59:50.347 回答
5

按照以下方式编写代码它将起作用。

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'});
于 2017-09-15T13:56:54.317 回答
1

使用以下。

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

您可以在以下链接中找到更多信息。

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

于 2017-07-11T10:50:03.460 回答