![授予 EC2 IAM 角色对 S3 存储桶的读取权限](https://linux22.com/image/613154/%E6%8E%88%E4%BA%88%20EC2%20IAM%20%E8%A7%92%E8%89%B2%E5%AF%B9%20S3%20%E5%AD%98%E5%82%A8%E6%A1%B6%E7%9A%84%E8%AF%BB%E5%8F%96%E6%9D%83%E9%99%90.png)
我有一个 AWS Elastic Beanstalk Rails 应用程序,我正在通过配置脚本配置它以从 S3 存储桶中提取一些文件。当我启动该应用程序时,我不断在日志中收到以下错误 (出于安全考虑,存储桶名称已更改):
Failed to retrieve https://s3.amazonaws.com/my.bucket/bootstrap.sh: HTTP Error 403 : <?xml version="1.0" encoding="UTF-8"?>
<Error><Code>AccessDenied</Code><Message>Access Denied</Message>
配置文件:
packages:
yum:
git: []
files:
/opt/elasticbeanstalk/hooks/appdeploy/pre/01a_bootstrap.sh:
mode: "00755"
owner: root
group: root
source: https://s3.amazonaws.com/my.bucket/bootstrap.sh
Elastic Beanstalk 环境设置为aws-elasticbeanstalk-ec2-role
IAM 角色作为其实例角色。此角色具有以下策略:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:Get*",
"s3:List*"
],
"Resource": "arn:aws:s3:::my.bucket/*"
}
]
}
S3存储桶具有以下策略:
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "Stmt1371012493903",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::<account #>:role/aws-elasticbeanstalk-ec2-role"
},
"Action": [
"s3:List*",
"s3:Get*"
],
"Resource": "arn:aws:s3:::my.bucket/*"
}
]
}
我需要进行哪些更改才能让我的 EC2 实例访问我的 S3 存储桶?
答案1
从您的 EC2 实例中,您还必须检索实例元数据中的临时凭证:
curl http://169.254.169.254/latest/meta-data/iam/security-credentials/<your-iam-role-name>
然后,您将使用提供的访问密钥和密钥来访问您的 S3 存储桶。
答案2
如果是跨账户访问,请检查它是否与此处提到的 ACL 标头相关:https://stackoverflow.com/a/34055538/1736679(更多信息请见此问题线索:https://github.com/aws/aws-cli/issues/1674)
另外,请仔细检查您正在运行的环境/用户,以查看是否没有覆盖密钥(1AWS_ACCESS_KEY1等)/etc/environment
或~/.aws/credentials