简体   繁体   English

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

[英]Configuration Error: Missing Region in Config (AWS)

I am getting a configuration error when I run my node application.运行节点应用程序时出现配置错误。 The error is:错误是:

{ 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',

The code I have is:我的代码是:

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

This is running on an express server using ejs and node.这是在使用 ejs 和节点的快速服务器上运行的。 I am using the Amazon JavaScript SDK.我正在使用 Amazon JavaScript SDK。

I attempted the solution here:我在这里尝试了解决方案:

AWSCognito Missing region in config error AWSCognito 配置错误中缺少区域

This did not help.这没有帮助。

EDIT 1:编辑 1:

As requested Below, I have attacheed full code:根据下面的要求,我附上了完整的代码:

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

Add region in your config like this:在您的配置中添加区域,如下所示:

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

Please add it on the very top level (above the line var ec2 = new AWS.EC2(); )请将它添加到最顶层(在var ec2 = new AWS.EC2();行上方)

The correct code of Oregon region is us-west-2 .You have set it as us-west-2a in two places.俄勒冈地区的正确代码是us-west-2 。您在两个地方将其设置为us-west-2a

While mentioning identity pool id,correct the code as below and try:在提到身份池 ID 时,请按以下方式更正代码并尝试:

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

If you are not passing the region for the API CognitoIdentityCredentials , then it will pull the data from AWS.config .如果您没有为 API CognitoIdentityCredentials传递区域,那么它将从AWS.config 中提取数据。

Also, In your code, while initializing AWS.CONFIG with region name, You have used us-west-2a .此外,在您的代码中,在使用区域名称初始化AWS.CONFIG时,您使用了us-west-2a Correct it, if you are going to use it instead of AWS.config.update .如果您打算使用它而不是 AWS.config.update ,请更正它。

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

Update更新

I have identified another issue.我发现了另一个问题。 The issue is that, you are initializing the AWS.config with var myConfig = new AWS.Config() but you are NOT updating the AWS.config class back with it.问题是,您正在使用var myConfig = new AWS.Config()初始化AWS.config ,但您没有用它更新AWS.config类。 The missing code is : AWS.config = myConfig .缺少的代码是: AWS.config = myConfig

As you are not updating it back and also you are not updating the existing default class using AWS.config.update({region:'us-west-2'});由于您没有更新它,也没有使用AWS.config.update({region:'us-west-2'});更新现有的默认类AWS.config.update({region:'us-west-2'}); as well, it throws the Missing region in config error.同样,它会Missing region in config错误中引发Missing region in config

Correct Code snippet正确的代码片段

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

The answers above are correct.上面的答案是正确的。 However, for aws-sdk-js-v3 you can't set AWS.config globally.但是,对于aws-sdk-js-v3您不能全局设置AWS.config Thus the workaround would be:因此,解决方法是:

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

Also if you're getting error about unreachable "169.254.169.254:80/latest/meta-data/iam/security-credentials/" during you CI build, you might want to set fake credentials in a same way:此外,如果您在 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'

In your case, it's the order of execution (that's why side effects are bad).在您的情况下,这是执行顺序(这就是副作用不好的原因)。

Wrong:错误:

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

Correct:正确的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有 Angular 4 的 AWS Cognito:错误:配置中缺少区域 - AWS Cognito with Angular 4: Error: Missing region in config AWS QLDB [错误] [Node.js QLDB 示例代码] 无法创建分类帐:ConfigError: Missing region in config - AWS QLDB [ERROR] [Node.js QLDB Sample Code] Unable to create the ledger: ConfigError: Missing region in config aws-sdk-js DynamoDB引发错误:Request.VALIDATE_REGION的配置中缺少凭据 - aws-sdk-js DynamoDB throwing Error: Missing credentials in config at Request.VALIDATE_REGION 区域缺失错误 - AWS Amplify React - Region is missing error - AWS Amplify React AWS Cognito-ConfigError:JavaScript SDK的配置中缺少区域 - AWS Cognito - ConfigError: Missing region in config for JavaScript SDK Amazon AWS 错误:config node.js 中缺少凭据 - Amazon AWS Error: Missing credentials in config node.js 如何为 AWS CloudFormation 堆栈和资源实施特定于区域的配置 - How to implement region specific configuration for AWS CloudFormation stack and resources ap-south-1区域上的AWS SignatureDoesNotMatch错误 - AWS SignatureDoesNotMatch error on ap-south-1 region 由于缺少AWS区域,因此放大AWSAnalyticsProvider记录会话失败 - Amplify AWSAnalyticsProvider record session fails due to missing AWS region 错误:配置中缺少凭据 - /nodeapp/node_modules/aws-sdk/lib/request.js:31 - Error: Missing credentials in config - /nodeapp/node_modules/aws-sdk/lib/request.js:31
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM