amazon-web-services - AWS 开发工具包运行实例和 IAM 角色
问题描述
当我添加 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);
}
解决方案
首先,您可以通过 sdk 查看您正在调用的 api,以提示您需要哪些权限,即 ec2:RunInstance 和 ec2:CreateTags。
您首先创建一个策略,然后选择服务,然后附加权限(RunInstances 和 CreateTags)
然后,您创建一个附加了该策略的角色。
然后您可以将该角色附加到您的 Lambda
推荐阅读
- c++ - 从循环中获取-858993460错误意味着将数据复制到txt文件中
- python - 如何获取所有链接表单页面?
- python - 删除同一目录中多个 CSV 文件中的前两列和最后一列
- apache-spark - CDAP spark phoenix 应用程序连接问题
- objective-c - 移除对象后,观察者保留在调度表中
- debugging - Intel x86 上的除法溢出
- unity3d - LoadRawTextureData 与 LoadImage
- reactjs - 我收到 getInputData 未定义的错误
- settings - androidx 设置库和平板电脑横向模式 2 窗格布局
- nuxt.js - NUXTJS 如何设置从页面到布局的道具?