ec2的最小IAM策略:RunInstances

时间:2013-11-21 10:48:04

标签: amazon-web-services amazon-ec2 amazon-iam

我正在尝试缩小运行预定义机器映像的最小策略。该图像基于两个快照,我只想要启动“m1.medium”实例类型。

在此基础上,在this pagethis article的帮助下,我制定了以下政策:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Stmt1385026304010",
            "Effect": "Allow",
            "Action": [
                "ec2:RunInstances"
            ],
            "Condition": {
                "StringEquals": {
                    "ec2:InstanceType": "m1.medium"
                }
            },
            "Resource": [
                "arn:aws:ec2:us-east-1::instance/*",
                "arn:aws:ec2:us-east-1::image/ami-f1c3e498",
                "arn:aws:ec2:us-east-1::snapshot/snap-e2f51ffa",
                "arn:aws:ec2:us-east-1::snapshot/snap-18ca2000",
                "arn:aws:ec2:us-east-1::key-pair/shenton",
                "arn:aws:ec2:us-east-1::security-group/sg-6af56d02",
                "arn:aws:ec2:us-east-1::volume/*"
            ]
        }
    ]
}

该策略会缩小确切的映像,快照,安全组和密钥对,同时保持特定实例和卷处于打开状态。

我正在使用CLI工具,如here所述:

aws ec2 run-instances --dry-run \
    --image-id ami-f1c3e498 \
    --key-name shenton \
    --security-group-ids sg-6af56d02 \
    --instance-type m1.medium

~/.aws/config如下:

[default]
output = json
region = us-east-1
aws_access_key_id = ...
aws_secret_access_key = ...

该命令导致通用You are not authorized to perform this operation消息,编码的授权失败消息表明我的所有语句都没有匹配,因此拒绝该操作。

更改为"Resource": "*"显然可以解决问题,但我希望更多地了解上述原因无法解决的问题。我完全意识到这涉及一定程度的猜测工作,所以我欢迎任何想法。

1 个答案:

答案 0 :(得分:29)

亚马逊网络服务公司的Jeff Barr联系过我,他帮助我找出了问题所在。

首先,您需要使用以下语句解码授权失败消息:

  

$ aws sts decode-authorization-message --encoded-message 6gO3mM3p....IkgLj8ekf

确保IAM用户/角色拥有sts:DecodeAuthorizationMessage操作的权限。

响应包含一个DecodedMessage密钥,其中包含另一个JSON编码正文:

{
    "allowed": false,
    "explicitDeny": false,
    "matchedStatements": {
        "items": []
    },
    "failures": {
        "items": []
    },
    "context": {
        "principal": {
            "id": "accesskey",
            "name": "testuser",
            "arn": "arn:aws:iam::account:user/testuser"
        },
        "action": "ec2:RunInstances",
        "resource": "arn:aws:ec2:us-east-1:account:instance/*",
        "conditions": { ... }
    }
}

context => resource下,它会显示尝试与政策匹配的资源;如你所见,它需要一个帐号。因此arn documentation应理解为:

  

除非另有说明,否则区域和帐户是必需的。

在受影响的ARN中添加帐号或*解决了问题:

"Resource": [
    "arn:aws:ec2:us-east-1:*:instance/*",
    "arn:aws:ec2:us-east-1:*:image/ami-f1c3e498",
    "arn:aws:ec2:us-east-1:*:snapshot/snap-e2f51ffa",
    "arn:aws:ec2:us-east-1:*:snapshot/snap-18ca2000",
    "arn:aws:ec2:us-east-1:*:key-pair/shenton",
    "arn:aws:ec2:us-east-1:*:security-group/sg-6af56d02",
    "arn:aws:ec2:us-east-1:*:volume/*"
]