我正在尝试创建EC2实例,然后将其添加到Auto Scaling组。我在尝试进行身份验证时遇到了很多问题。我正在寻找一种简单的方法来使用我的访问密钥来验证请求,以简单地启动实例。到目前为止我尝试了什么:
//Authenticate AWS:
var myCredentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId:'us-west-2:IdentityPoolID'
});
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: false
},
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
}
});
我知道这段代码错了。我只是不知道要解决它。我得到的错误是:
CredentialsError:配置中缺少凭据
非常感谢任何帮助。
答案 0 :(得分:2)
完全删除此部分代码:
//Authenticate AWS:
var myCredentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId:'us-west-2:IdentityPoolID'
});
var myConfig = new AWS.Config({
credentials: myCredentials, region: 'us-west-2'
});
AWS.config = myConfig
改变这个:
var ec2 = new AWS.EC2();
对此:
var ec2 = new AWS.EC2({region: 'us-west-2'});
然后在Setting Credentials in Node.js文档中阅读此页面。特别是,您需要执行以下操作之一:
~/.aws/credentials
文件。如果您安装了the AWS CLI,则可以使用aws configure
命令完成此操作。