我应该授予 IAM 什么权限,以便它可以创建实例并管理它们?

What permissions should I grant an IAM so that it may create instances and manage them?

我想创建一个 IAM 用户,允许其创建新实例并完全管理它们(包括终止它们),但无权访问任何其他实例。我尝试了以下操作(iam 用户名为 auto-provision):

{
  "Version": "2012-10-17",
  "Statement": [
        {
            "Effect": "Allow",
            "Action": "ec2:Describe*",
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "cloudwatch:ListMetrics",
                "cloudwatch:GetMetricStatistics",
                "cloudwatch:Describe*"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "ec2:RunInstances"
            ],
            "Resource":"*"
        },
        {
            "Effect":"Allow",
            "Action":["ec2:*"],
            "Resource":"*",
            "Condition": {
                "StringEquals": {
                    "ec2:Owner": "auto-provision"
                }
            }
        }

  ]
}

这没有按预期工作。用户可以很好地创建实例,但无权管理它。我假设我在最后一条语句中的 Condition 不起作用,因为 "owner" 是整个帐户而不是用户?

我找到了解决此问题的方法,并在评论中提出了 Assaf Lavie 的一些建议。

首先,我创建了一个名为 auto-provision-placement-group 的归置组。然后我将策略文件更改为:

{
  "Version": "2012-10-17",
  "Statement": [
        {
            "Effect": "Allow",
            "Action": "ec2:Describe*",
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "cloudwatch:ListMetrics",
                "cloudwatch:GetMetricStatistics",
                "cloudwatch:Describe*"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "ec2:RunInstances",
                "ec2:CreateTags",
                "ec2:DeleteTags"
            ],
            "Resource":"*"
        },
        {
            "Effect":"Allow",
            "Action":["ec2:*"],
            "Resource":"*",
            "Condition": {
                "StringEquals": {
                    "ec2:PlacementGroup": "arn:aws:ec2:eu-west-1:111111111111:placement-group/auto-provision-placement-group"
                }
            }
        }

  ]
}

启动新实例时,我分配了相关的归置组。例如。 (使用 Ruby 雾):

fog.compute.servers.create(:placement_group => 'auto-provision-placement-group', ...)

服务器被创建到放置组中,然后用于控制对大多数功能的访问。我故意允许完全访问标签,因为我用它来识别机器的所有权。放置组然后成为我的 "access control".

一个黑客,但它有效。