AWS ECS Docker容器Boto3 IAM权限

mic*_*_65 7 amazon-web-services amazon-iam docker boto3

我试图使用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.

任何想法,将不胜感激.

lou*_*ola 15

看起来Boto现在支持IAM任务角色,但无论如何,当Boto客户端尝试发出请求时,这将是一个问题,而不是在尝试启动任务时.

此处的问题在错误消息中定义.或者:

1)您的用户没有为任务角色定义的iam:PassRole权限.这可以通过编辑用户的策略来添加,使其具有类似于以下内容的语句:

{
  "Effect": "Allow",
  "Action": "iam:PassRole",
  "Resource": "arn:aws:iam::<account>:role/<role name>"
}
Run Code Online (Sandbox Code Playgroud)

2)您尝试分配给任务的任务角色没有正确的信任关系.将以下信任策略添加到ECS任务角色,以确保该任务可以承担它.

{
  "Version": "2008-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "ecs-tasks.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)


Eri*_*c N 3

Boto3使用botocore库来实现其API函数,并且它允许每个Boto3版本有一系列的botocore版本,因此即使您拥有最新的Boto3版本,您也可能没有最新的botocore。

自版本1.4.37起,Botocore 支持任务的 ECS IAM 角色,因此,如果您将环境中的底层 botocore 至少更新到该版本,您应该能够使用任务的 ECS IAM 角色功能。