【发布时间】:2019-01-15 22:36:37
【问题描述】:
当我添加 AWS IAM 角色“AdministratorAccess”时,下面的代码有效 - 但它有风险并且有点矫枉过正......但我怎么知道并只找到必要的角色......它是当我查看控制台中所有可能的角色时,非常混乱且难以了解?
try {
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({region: 'us-east-2'});
var instanceParams = {
ImageId: 'ami-xxxxxxxxxxxx',
InstanceType: 't2.micro',
KeyName: 'xxxxxxxxxx',
SecurityGroups: ['xxxxxxxxxxxxxxx'],
MinCount: 1,
MaxCount: 1
};
// Create a promise on an EC2 service object
var instancePromise = new AWS.EC2({apiVersion: '2016-11-15'}).runInstances(instanceParams).promise();
// Handle promise's fulfilled/rejected states
instancePromise.then(
function (data) {
console.log(data);
var instanceId = data.Instances[0].InstanceId;
console.log("Created instance", instanceId);
// Add tags to the instance
var tagParams = {
Resources: [instanceId], Tags: [
{
Key: 'Name',
Value: 'SDK Sample'
}
]
};
// Create a promise on an EC2 service object
var tagPromise = new AWS.EC2({apiVersion: '2016-11-15'}).createTags(tagParams).promise();
// Handle promise's fulfilled/rejected states
tagPromise.then(
function (data) {
console.log("Instance tagged");
}).catch(
function (err) {
console.error(err, err.stack);
});
}).catch(
function (err) {
console.error(err, err.stack);
});
}
catch(e){
wl.info('Error: ' + e);
}
【问题讨论】:
-
推荐的最佳实践是创建一个 IAM 角色,并在 IAM 角色的策略中添加必要的权限。在您的情况下,您需要
ec2:RunInstances和ec2:CreateTags。 -
好的 - 但是如何添加角色?当我登录 AWS 控制台搜索“RunInstances”或“CreateTags”时,什么都没有显示...?我必须创建/添加自定义规则还是什么?您是如何知道要添加的内容的名称/角色的?
-
请注意,您可以在 runInstances() 调用中提供标签,因此严格来说,您不需要使用单独的 createTags() 调用。
-
有关构建 IAM 策略以管理 EC2 的帮助,请参阅 docs.aws.amazon.com/AWSEC2/latest/UserGuide/… 上的示例,更一般地参阅 docs.aws.amazon.com/AWSEC2/latest/UserGuide/…。
标签: amazon-web-services amazon-ec2 aws-sdk aws-iam