【发布时间】:2022-01-17 12:51:07
【问题描述】:
我真的在为此苦苦挣扎,AWS 官方文档根本没有帮助!
我设置了一个 S3 存储桶,它允许从几个指定的 IP 地址进行公共访问。这是有效的自定义策略:
{
"Version": "2012-10-17",
"Id": "Policy1111111111",
"Statement": [
{
"Sid": "Stmt111111111",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:*",
"Resource": "arn:aws:s3:::myapp-local-test/*",
"Condition": {
"NotIpAddress": {
"aws:SourceIp": [
"12.122.123.111",
"121.217.73.153"
]
}
}
},
{
"Sid": "Stmt1111111111",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Resource": "arn:aws:s3:::myapp-local-test/*",
"Condition": {
"IpAddress": {
"aws:SourceIp": [
"12.122.123.111",
"121.217.73.153"
]
}
}
},
]
}
现在,除了只允许上面的 2 个 ip 地址访问存储桶中的资源之外,我还希望我的 EC2 实例能够访问它。
我关注了这个文档:https://aws.amazon.com/premiumsupport/knowledge-center/ec2-instance-access-s3-bucket/
我遵循了确切的步骤。
我创建了一个新的 IAM 角色,(arn: "arn:aws:iam::1223123156:role/EC2-to-S3")
我还将该角色附加到我的 EC2 实例。
但在第 6 步中:
6. In your bucket policy, edit or remove any Effect: Deny
statements that are denying the IAM instance profile access to
your bucket. For instructions on editing policies,
see Editing IAM policies.
我该怎么做?它将我引导到另一个关于编辑 IAM 政策的文档,但它没有帮助!!!
如何删除任何拒绝 IAM 实例配置文件访问我的存储桶的“Effect: Deny”语句?
我应该使用什么关键字?
这是我尝试过的:
{
"Version": "2012-10-17",
"Id": "Policy1111111111",
"Statement": [
{
"Sid": "Stmt111111111",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:*",
"Resource": "arn:aws:s3:::myapp-local-test/*",
"Condition": {
"NotIpAddress": {
"aws:SourceIp": [
"12.122.123.111",
"121.217.73.153"
]
},
"StringNotEquals": {
"aws:SourceArn": "arn:aws:iam::1223123156:role/EC2-to-S3"
}
},
{
"Sid": "Stmt1111112222",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Resource": "arn:aws:s3:::myapp-local-test/*",
"Condition": {
"IpAddress": {
"aws:SourceIp": [
"12.122.123.111",
"121.217.73.153"
]
}
}
},
{
"Sid": "Stmt1639460338435",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Resource": "arn:aws:s3:::myapp-local-test/*",
"Condition": {
"StringEquals": {
"aws:SourceArn": "arn:aws:iam::1223123156:role/EC2-to-S3"
}
}
}
]
}
这不起作用。我仍然遇到“拒绝访问”错误。
文档可以更具体一点吗? 为什么使用 aws docs 完成如此基本的任务如此困难??
这终于奏效了:
{
"Version": "2012-10-17",
"Id": "Policy1111111",
"Statement": [
{
"Sid": "Stmt11111",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Resource": "arn:aws:s3:::myapp-local-test/*",
"Condition": {
"IpAddress": {
"aws:SourceIp": [
"12.122.123.111",
"121.217.73.153"
]
}
}
},
{
"Sid": "Stmt1222222222",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::1234556:role/EC2-to-S3"
},
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Resource": "arn:aws:s3:::myapp-local-test/*"
}
]
}
所以诀窍是完全放弃拒绝语句,因为默认情况下所有内容都被拒绝访问。
还有我之前的编辑:
"Statement": [
{
"Sid": "Stmt111111111",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:*",
"Resource": "arn:aws:s3:::myapp-local-test/*",
"Condition": {
"NotIpAddress": {
"aws:SourceIp": [
"12.122.123.111",
"121.217.73.153"
]
},
"StringNotEquals": {
"aws:SourceArn": "arn:aws:iam::1223123156:role/EC2-to-S3"
}
},
StringNotEquals 部分不会删除 iam 角色的默认拒绝。
【问题讨论】:
-
“这不起作用。” - 不具体。为什么它不起作用?有拒绝访问吗?如果是,来自哪里? IP 地址,实例?任何错误。遗憾的是,您的问题并不清楚。
标签: amazon-web-services amazon-s3 amazon-ec2