AWS ECS Docker容器Boto3 IAM权限

我正在尝试使用AWS ECS在Docker容器内运行boto3 python脚本。 我的脚本需要访问SQS(获取和删除消息)和Lambda(权限search和运行)。

为了让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

我将一个任务IAMangular色附加到任务上,但是在运行任务时出现以下错误:

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.

任何想法,将不胜感激。

看起来Boto现在支持IAM任务angular色,但无论如何,当Boto客户端试图发出请求时,不会发生这种情况,而不是在尝试启动任务时。

这里的问题在错误信息中定义。 或者:

1)您的用户没有为任务angular色定义的iam:PassRole权限。 这可以通过编辑您的用户的策略添加类似于以下的语句来添加:

{ "Effect": "Allow", "Action": "iam:PassRole", "Resource": "arn:aws:iam::<account>:role/<role name>" }

2)您试图分配给任务的任务angular色没有适当的信任关系。 将以下信任策略添加到ECS任务angular色,以确保该任务可以承担该策略。

{ "Version": "2008-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "ecs-tasks.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }

Boto3为其API函数使用botocore库,并且它允许为每个Boto3版本提供一系列botocore版本,所以即使您拥有最新的Boto3版本,也可能没有最新的botocore。

Botocore支持版本1.4.37以上版本的ECS IAMangular色,所以如果您将环境中的底层botocore更新为至less该版本,则应该能够使用ECS IAMangular色执行任务function。