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

Posted

技术标签:

【中文标题】配置错误:Config 中缺少区域 (AWS)【英文标题】:Configuration Error: Missing Region in Config (AWS) 【发布时间】:2018-04-11 01:38:05 【问题描述】:

我在运行节点应用程序时遇到配置错误。错误是:

 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 和节点的快速服务器上运行的。我正在使用 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
  
);

【问题讨论】:

另见:Configuring region in Node.js AWS SDK 【参考方案1】:

像这样在你的配置中添加区域:

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

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

【讨论】:

当我添加这个时它给了我一个凭据错误,我该如何添加凭据呢?【参考方案2】:

俄勒冈地区的正确代码是us-west-2。您在两个地方都设置为us-west-2a

在提及身份池ID的同时,更正如下代码并尝试:

AWS.config.update(region:'us-west-2');
var myCredentials = new AWS.CognitoIdentityCredentials(
    IdentityPoolId:'us-west-2:"Identity Pool ID"'
);

如果您没有为 API CognitoIdentityCredentials 传递 区域,那么它将从 AWS.config。

另外,在您的代码中,在使用 region 名称初始化 AWS.CONFIG 时,您使用了 us-west-2a。更正它,如果您打算使用它而不是 AWS.config.update

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

更新

我发现了另一个问题。问题是,您正在使用 var myConfig = new AWS.Config() 初始化 AWS.config,但您不更新 AWS.config 类回来了。缺少的代码是:AWS.config = myConfig

由于您没有将其更新回来,并且您也没有使用 AWS.config.update(region:'us-west-2'); 更新现有的默认类,因此它会引发 Missing region in config 错误。

正确的代码 sn-p

var AWS = require('aws-sdk');
var myCredentials = new AWS.CognitoIdentityCredentials(
    IdentityPoolId:'us-west-2:identity-pool-id'
); 
var myConfig = new AWS.Config(
    credentials: myCredentials, region: 'us-west-2'
);
AWS.config = myConfig

【讨论】:

我已经更正了它,但我仍然得到同样的错误。 @ChristopherLittlewood 我已经更新了答案部分。 我现在收到错误消息:missing credentials in config 当我向下滚动时,它显示message: 'XMLHttpRequest is not defined'。我已将您更正的解决方案复制到我的代码中。我唯一改变的是我的身份池 ID。 当我注释掉启动 EC2 机器的请求时,它似乎没有收到此错误。 这太令人惊讶了!!能够成功请求新的 EC2 实例。 config initializationec2.runInstances() 代码块都在同一个 .js 脚本中,对吗?你能附上你的整个代码吗?【参考方案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'

【讨论】:

【参考方案4】:

在您的情况下,这是执行顺序(这就是副作用不好的原因)。

错误:

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

【讨论】:

以上是关于配置错误:Config 中缺少区域 (AWS)的主要内容,如果未能解决你的问题,请参考以下文章

terraform 中的 aws_config_remediation_configuration 资源中缺少参数

如何修复 aws 区域错误“ValueError:必须使用 SageMaker 支持的区域设置本地 AWS 配置”

CredentialsError:nodejs aws-sdk 中的配置中缺少凭据

Amazon Web Services Elastic Beanstalk Wordpress 配置文件下载缺少 efs-create.config

尝试将对象放入 s3 存储桶时,配置中缺少凭据

AWS - nodejs SDK - CognitoIdentityServiceProvider.initiateAuth - CredentialsError:配置中缺少凭证