通过AWS Command-Line Interface (CLI) 启动 Amazon EC2 实例的最低权限就是RunInstances:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "RunInstances",
"Effect": "Allow",
"Action": "ec2:RunInstances",
"Resource": "*"
}
]
}
使用该策略,我使用此 AWS CLI 命令启动了一个实例:
aws ec2 run-instances --image-id ami-xxx --key-name my-key --security-group-id sg-xxx --instance-type t2.nano
如何调试权限错误
如果您添加任何其他参数,则该命令可能会失败。
比如我加了这个参数:
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=Temp}]'
这失败了:
调用 RunInstances 操作时发生错误 (UnauthorizedOperation):您无权执行此操作。编码授权失败消息:xxx
然后,我使用以下方法解密了编码的失败消息(使用管理员 IAM 用户):
aws sts decode-authorization-message --encoded-message xxx
返回:
{
"allowed": false,
"explicitDeny": false,
"matchedStatements": {
"items": []
},
"failures": {
"items": []
},
"context": {
"principal": {
"id": "AIDAxxx",
"name": "my-user",
"arn": "arn:aws:iam::123456789012:user/my-user"
},
"action": "ec2:CreateTags",
"resource": "arn:aws:ec2:ap-southeast-2:123456789012:instance/*",
"conditions": {
"items": [
{
"key": "aws:Resource",
"values": {
"items": [
{
"value": "instance/*"
}
]
}
},
...
]
}
}
}
很明显问题出在ec2:CreateTags,因为我的命令要求将标签添加到实例中。因此,我要么需要添加这些权限,要么从RunInstances 命令中删除标签参数。
琐事
您有没有想过为什么将命令称为RunInstances 而不是LaunchInstances 或CreateInstances?
(我认为)这是因为在 Amazon EC2 的早期,只有实例存储(没有 Amazon Elastic Block Storage (EBS))。因此,无法停止实例,因为这会丢失磁盘内容并且无法再次启动实例。因此,命令是RunInstances 和TerminateInstances。
这些天来,我们可以StartInstances 和StopInstances。但是,RunInstances 的旧术语仍然存在,这对于新用户来说总是有点令人困惑,因为它是指 launch 还是 start 并不明显。