gpt4 book ai didi

amazon-web-services - ec2 :RunInstances 的最小 IAM 策略

转载 作者:行者123 更新时间:2023-12-03 13:53:40 27 4
gpt4 key购买 nike

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

基于此并在 this page 的帮助下和 this 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/*"
]
}
]
}

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

我正在使用如下 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": "*"显然解决了这个问题,但我想更多地了解为什么上述方法不起作用。我完全意识到这涉及到一定程度的猜测工作,所以我欢迎任何想法。

最佳答案

Amazon Web Services 的 Jeff Barr 联系了我,他很乐意帮助我找出问题所在。

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

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



确保 IAM 用户/角色具有 sts:DecodeAuthorizationMessage 的权限行动。

响应包含 DecodedMessage包含另一个 JSON 编码主体的 key :
{
"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因此应理解为:

Unless otherwise specified, the region and account are required.



添加帐号或 *在受影响的 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/*"
]

关于amazon-web-services - ec2 :RunInstances 的最小 IAM 策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20118810/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com