配置错误:配置中缺少区域(AWS)

时间:2017-10-30 06:16:44

标签: javascript amazon-web-services region aws-sdk-js

运行节点应用程序时出现配置错误。错误是:

{ ConfigError: Missing region in config
at Request.VALIDATE_REGION (C:\Users\chris\Documents\AWS API 
TEST\myapp\node_modules\aws-sdk\dist\aws-sdk-react-native.js:12027:47)
at Request.callListeners (C:\Users\chris\Documents\AWS API 
TEST\myapp\node_modules\aws-sdk\dist\aws-sdk-react-native.js:11804:22)
at callNextListener (C:\Users\chris\Documents\AWS API 
TEST\myapp\node_modules\aws-sdk\dist\aws-sdk-react-native.js:11794:14)
at C:\Users\chris\Documents\AWS API TEST\myapp\node_modules\aws-
sdk\dist\aws-sdk-react-native.js:12021:11
at finish (C:\Users\chris\Documents\AWS API TEST\myapp\node_modules\aws-
sdk\dist\aws-sdk-react-native.js:10842:9)
at Config.getCredentials (C:\Users\chris\Documents\AWS API 
TEST\myapp\node_modules\aws-sdk\dist\aws-sdk-react-native.js:10887:9)
at Request.VALIDATE_CREDENTIALS (C:\Users\chris\Documents\AWS API 
TEST\myapp\node_modules\aws-sdk\dist\aws-sdk-react-native.js:12016:28)
at Request.callListeners (C:\Users\chris\Documents\AWS API 
TEST\myapp\node_modules\aws-sdk\dist\aws-sdk-react-native.js:11800:20)
at Request.emit (C:\Users\chris\Documents\AWS API 
TEST\myapp\node_modules\aws-sdk\dist\aws-sdk-react-native.js:11776:12)
at Request.emit (C:\Users\chris\Documents\AWS API 
TEST\myapp\node_modules\aws-sdk\dist\aws-sdk-react-native.js:13792:16)
message: 'Missing region in config',
code: 'ConfigError',

我的代码是:

//Authenticate AWS:
var myCredentials = new AWS.CognitoIdentityCredentials({IdentityPoolId:'us-west-2a:"Identity Pool ID"'});
var myConfig = new AWS.Config({
          credentials: myCredentials, region: 'us-west-2a'
});

这是使用ejs和node在快速服务器上运行的。我正在使用Amazon JavaScript SDK。

我在这里尝试了解决方案:

  

AWSCognito Missing region in config error

这没有帮助。

编辑1:

根据要求,我已附上完整的代码:

var express = require('express');
var AWS = require('aws-sdk');
var EC2 = require('aws-ec2')('access key', 'secret');
var scale = require('aws-scale');
var router = express.Router();

//Authenticate AWS:
var myCredentials = new AWS.CognitoIdentityCredentials({
  IdentityPoolId:'us-west-2:b0efe3c7-e4c9-420f-836b-6d776e2a9271'
}); 

var myConfig = new AWS.Config({
  credentials: myCredentials, region: 'us-west-2'
});

AWS.config = myConfig

var minInst = 1;
var maxInst = 3;

var ec2 = new AWS.EC2();
//Set up parameters for EC2 Instances:
var params = {
  ImageId: 'ami-6e1a0117',
  MaxCount: minInst,
  MinCount: maxInst, 
  InstanceInitiatedShutdownBehavior: 'terminate',
  InstanceType: 't2.micro',
  Monitoring: {
    Enabled: true 
  },
  NetworkInterfaces: [{
    AssociatePublicIpAddress: true,
    DeleteOnTermination: true,
  }],
  Placement: {
    AvailabilityZone: 'us-west-2',
  },
  SecurityGroupIds: [
    'sg-b0307ccd',
  ],
  SecurityGroups: [
    'CAB432Assignment2SG',
  ],

};
ec2.runInstances(params, function(err, data) {
  if (err){
    console.log(err, err.stack); //An error occurred
  }
  else{
    console.log(data); //Successful Response
  }
});

4 个答案:

答案 0 :(得分:3)

请在配置中添加区域,如下所示

AWS.config.update({
region: "REGION" //Here add you region
});

请将其添加到最顶层(行上方' var ec2 = new AWS.EC2();')

答案 1 :(得分:3)

以上答案是正确的。 但是,对于 aws-sdk-js-v3,您不能全局设置 AWS.config。 因此,解决方法是:

process.env.AWS_REGION = 'us-west-2'

此外,如果您在 CI 构建期间收到有关无法访问 "169.254.169.254:80/latest/meta-data/iam/security-credentials/" 的错误,您可能希望以相同的方式设置假凭据:

process.env.AWS_REGION = 'us-west-2'
process.env.AWS_ACCESS_KEY_ID = 'test-key'
process.env.AWS_SECRET_ACCESS_KEY = 'test-secret'

答案 2 :(得分:2)

Oregon 区域的正确代码是func putOnStars() { let rect = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)) rect.backgroundColor = .yellow view.addSubview(rect) let baseLayer = CALayer() baseLayer.contents = UIImage(named: "stars")?.cgImage baseLayer.contentsGravity = kCAGravityCenter rect.layer.addSublayer(baseLayer) } 。您已在两个地方将其设置为us-west-2

在提及身份池ID时,请更正以下代码并尝试:

us-west-2a

如果您没有为API CognitoIdentityCredentials 传递 区域 ,那么它将从 AWS.config <中提取数据/ em>的

此外,在您的代码中,在使用 region 名称初始化 AWS.CONFIG 时,您已使用AWS.config.update({region:'us-west-2'}); var myCredentials = new AWS.CognitoIdentityCredentials({ IdentityPoolId:'us-west-2:"Identity Pool ID"' }); 。如果您打算使用 而不是AWS.config.update ,请更正它。

us-west-2a

修改-1: 我发现了另一个问题。问题是,您正在使用var myConfig = new AWS.Config({ credentials: myCredentials, region: 'us-west-2' }); 初始化 AWS.config 但您 不更新 AWS。用它来配置类。缺少的代码是:var myConfig = new AWS.Config()

由于您没有更新它,而且您也没有使用AWS.config = myConfig更新现有的默认类,因此会引发AWS.config.update({region:'us-west-2'});错误。

Missing region in config

Correct Code snippet:

答案 3 :(得分:0)

就您而言,这是执行顺序(这就是副作用不好的原因)。

错误:

var myCredentials = new AWS.CognitoIdentityCredentials({
  IdentityPoolId:'us-west-2:b0efe3c7-e4c9-420f-836b-6d776e2a9271'
}); 

var myConfig = new AWS.Config({
  credentials: myCredentials, region: 'us-west-2'
});

正确:

// update the config
var myConfig = new AWS.Config({
  credentials: myCredentials, region: 'us-west-2'
});

// then create the instance
var myCredentials = new AWS.CognitoIdentityCredentials({
  IdentityPoolId:'us-west-2:b0efe3c7-e4c9-420f-836b-6d776e2a9271'
});