amazon-web-services - Lambda 无权访问 ECR 镜像

标签 amazon-web-services aws-lambda aws-cloudformation amazon-ecs aws-sam-cli

随着最近发布的用于 Lambda 函数的 Docker 镜像,我决定使用 CloudFormation 尝试此功能。

因此,下面的 lambda 考虑存储在 Elastic Container Registry 中的 docker 镜像,并具有按照 documentation 中的示例访问该镜像的权限。 .

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: lambda-docker-image

Globals:
  Function:
    Timeout: 180

Resources:
  DockerAsImage:
    Type: AWS::Serverless::Function 
    Properties:
      FunctionName: DockerAsImage
      ImageUri: ??????????????.dkr.ecr.us-west-2.amazonaws.com/????:latest
      PackageType: Image
      Policies: 
        - Version: '2012-10-17' 
          Statement:
            - Effect: Allow
              Action: 
                - ecr:*
                - ecr-public:*
                - sts:GetServiceBearerToken
              Resource: "*"
      Events:
        HelloWorld:
          Type: Api
          Properties:
            Path: /hello
            Method: post

我正在使用 samus-west-2 中部署模板

sam deploy -t template.yaml --capabilities "CAPABILITY_NAMED_IAM" --region "us-west-2" --stack-name "lambda-docker-example" --s3-bucket "my-bucket" --s3-prefix "sam_templates/lambda-docker-example" --force-upload  --no-confirm-changeset

但是,在成功创建 IAM 角色后,Lambda 函数创建失败,并出现以下错误

Lambda does not have permission to access the ECR image. Check the ECR permissions. (Service: AWSLambdaInternal; Status Code: 403; Error Code: AccessDeniedException;

即使该角色有权访问任何 ecs 资源。我尝试过的另一种方法是创建一个单独的角色并通过 Role: !GetAtt Role.Arn 将其分配给 lambda,这种方法也不起作用。

最佳答案

基于评论。

要使用基于图像的 lambda,需要 ECR 权限的是 IAM 用户/角色,而不是函数本身。来自 docs :

Make sure that the permissions for the AWS Identity and Access Management (IAM) user or role that creates the function contain the AWS managed policies GetRepositoryPolicy and SetRepositoryPolicy.

除了上面列出的两个权限外,还需要ecr: InitiateLayerUpload

关于amazon-web-services - Lambda 无权访问 ECR 镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65137639/

相关文章:

php - 创建 docker 镜像的作业失败

c# - 如何轻松地为多个 Lambda 函数添​​加环境变量?

aws-lambda - 如何最小化 lambda 函数的权重?

amazon-web-services - 在 AWS CloudFormation 中创建堆栈时如何解决 "Resource did not stabilize"?

amazon-web-services - 当模板获取 AWS::EC2::EIP(弹性 IP)的 AllocationID 时,AWS CloudFormation 出现错误

amazon-web-services - 从 cloudwatch 日志组到 lambda 的 aws 传入数据已损坏

python - 使用 Elastic Beanstalk 上的 Supervisor 在后台运行 Huey 任务队列

amazon-web-services - 如何通过 AWS CDK 设置 DataDog 警报?

aws-lambda - 使用 AWS lambda 和 middy 修复未知的 http 事件格式?

amazon-web-services - Cloudformation 堆栈长时间处于 UPDATE_COMPLETE_CLEANUP_IN_PROGRESS 状态