我正在尝试创建一个允许对资源执行某些操作的 IAM 策略,但前提是从特定的 Elastic Beanstalk 应用程序或容器使用访问密钥。有谁知道如何做到这一点?我试过了,但无济于事:
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "s3:*",
"Resource": "arn:aws:s3:::example-bucket/*",
"Condition": {
"ArnEquals": {
"aws:SourceArn": "arn:aws:elasticbeanstalk:..."
}
}
通过 ip 限制反而有效,但 Elastic Beanstalk 环境经常更改它们创建的服务器的 ip,因此这不是一个可行的选择:
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "s3:*",
"Resource": "arn:aws:s3:::example-bucket/*",
"Condition": {
"IpAddress": {
"aws:SourceIp": "..."
}
}
仅当它们源自特定的 Elastic Beanstalk 环境时才允许 S3 操作:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowS3AccessFromEB",
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Resource": "arn:aws:s3:::example-bucket/*",
"Condition": {
"StringEquals": {
"aws:Referer": "https://<your-environment-name>.<region>.elasticbeanstalk.com"
}
}
}
]
}
将“example-bucket”替换为您的 S3 存储桶的名称,将“..elasticbeanstalk.com”替换为您的 Elastic Beanstalk 环境的 URL。此策略允许用户对指定 S3 存储桶中的对象执行“GetObject”和“PutObject”操作,前提是这些对象来自指定的 Elastic Beanstalk 环境。
我不熟悉 Elastic Beanstalk,但您可以尝试以下两件事。
您的 Elastic Beanstalk 实例是否在 VPC 中?如果是这样,您可以为 S3 创建一个 VPC 端点,通过该端点进行所有 S3 调用,并使用
aws:SourceVpc
条件键:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": "arn:aws:s3:::example-bucket/*",
"Condition": {
"StringEquals": {
"aws:SourceVpc": "vpc-1234abcd"
}
}
}
]
}
不幸的是,
aws:SourceArn
仅针对服务主体进行的调用定义。据推测,您对 S3 的调用是由角色进行的。但是,Elastic Beanstalk 的服务主体必须调用 sts:AssumeRole
以获取这些角色凭证。您可以考虑将它们放在控制对您角色的访问的信任策略上,而不是对您对 S3 的调用施加aws:SourceArn
约束:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "elasticbeanstalk.amazonaws.com"
},
"Action": "sts:AssumeRole",
"Condition": {
"ArnLike": {
"aws:SourceArn": "arn:aws:elasticbeanstalk:..."
}
}
}
]
}