AWS SAM 无法从 Lambda 调用 Rekognition 和访问 S3

AWS SAM Unable to call Rekognition and access S3 from Lambda

我正在尝试从 Rekognition 框架调用 detectText 方法,但未能调用 S3 存储桶。我不确定如何在 SAM 模板中赋予角色。下面是我的 SAM 模板

GetTextFunction:
Type: AWS::Serverless::Function
Properties:
  CodeUri: gettextfn/
  Handler: text.handler
  Runtime: nodejs12.x
  Timeout: 3
  MemorySize: 128
  Environment:
    Variables:
        imagebucket: !Ref s3bucket
  Events:
    TextApiEvent:
      Type: HttpApi
      Properties:
        Path: /gettext
        Method: get
        ApiId: !Ref myapi

看起来您的 lambda 需要 RekognitionDetectOnlyPolicy,并且看起来您也错过了对来自 S3 存储桶的 read/write 数据的策略。看看下面 Policies: 添加在 Environment:

之后
Environment:
    Variables:
      imagebucket: !Ref s3bucket
  Policies:
    - S3ReadPolicy:
        BucketName: !Ref s3bucket
    - RekognitionDetectOnlyPolicy: {}
  Events:

您可以在此处参考 AWS SAM 策略模板的完整列表https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-policy-templates.html

另请查看此处的示例模板 https://github.com/rollendxavier/serverless_computing/blob/main/template.yaml