AWS ECS Docker 容器 Boto3 IAM 权限
Posted
技术标签:
【中文标题】AWS ECS Docker 容器 Boto3 IAM 权限【英文标题】:AWS ECS Docker Container Boto3 IAM Permissions 【发布时间】:2016-12-26 01:36:29 【问题描述】:我正在尝试使用 AWS ECS 在 docker 容器内运行 boto3 python 脚本。我的脚本需要访问 SQS(获取和删除消息)和 Lambda(搜索和运行权限)。
为了让 docker 容器在我的本地机器上运行,我可以使用以下 docker run 命令将我的 aws 凭据传递到 docker 容器中。
docker run -v ~/.aws:/root/.aws
最近ECS宣布:
Amazon ECS now supports IAM roles for tasks. When you specify an IAM role for a task, its containers can then use the latest versions of the AWS CLI or SDKs to make API requests to authorized AWS services. Learn More
我将任务 IAM 角色附加到任务,但在运行任务时出现以下错误:
Unable to run task
ECS was unable to assume the role that was provided for this task. Please verify that the role being passed has the proper trust relationship and permissions and that your IAM user has permissions to pass this role.
任何想法将不胜感激。
【问题讨论】:
Boto3 尚不支持 ECS 任务的 IAM 角色 - docs.aws.amazon.com/AmazonECS/latest/developerguide/… (我也很难学会...尝试部署一些东西,但没有用,然后我想通了) 感谢您的回复。很高兴知道 IAM 角色不适用于 Boto3。我正在研究这篇文章作为解决方案,但无法让它发挥作用。 docs.aws.amazon.com/AmazonECS/latest/developerguide/… 【参考方案1】:Boto 现在似乎支持 IAM 任务角色,但无论如何,当 Boto 客户端尝试发出请求而不是尝试启动任务时,这将是一个问题。
这里的问题在错误消息中定义。要么:
1) 您的用户没有为任务角色定义的 iam:PassRole 权限。这可以通过编辑您的用户策略来添加类似于以下的语句:
"Effect": "Allow",
"Action": "iam:PassRole",
"Resource": "arn:aws:iam::<account>:role/<role name>"
2) 您尝试分配给任务的任务角色没有正确的信任关系。将以下信任策略添加到 ECS 任务角色,以确保它可以由任务承担。
"Version": "2008-10-17",
"Statement": [
"Effect": "Allow",
"Principal":
"Service": "ecs-tasks.amazonaws.com"
,
"Action": "sts:AssumeRole"
]
【讨论】:
【参考方案2】:Boto3 将 botocore 库用于其 API 函数,并且它允许为每个 Boto3 版本提供一系列 botocore 版本,因此即使您拥有最新的 Boto3 版本,也可能没有最新的 botocore。
自版本 1.4.37 起,Botocore 支持任务的 ECS IAM 角色,因此如果您将环境中的底层 botocore 更新到至少该版本,您应该能够使用任务的 ECS IAM 角色功能。
【讨论】:
以上是关于AWS ECS Docker 容器 Boto3 IAM 权限的主要内容,如果未能解决你的问题,请参考以下文章
我们可以在 AWS ECS docker 容器上挂载 EFS 吗?
Docker 容器在使用 AWS ECR 的 AWS ECS 中不起作用